The Functions and Purposes of Translators Syntax (& Semantic) Analysis
Learning Objectives Describe what happens during syntax analysis, explaining how errors are handled.
Translation Process
Syntax (& Semantic) Analysis Semantic analysis: Check the meaning of each sequence of tokens (each sentence). Check the meaning of each sequence of tokens (each sentence). A Parser is the component of a compiler that carries out this task. A Parser is the component of a compiler that carries out this task. The grammar of programming languages is defined by means of BNF notation or syntax diagrams (see later). The grammar of programming languages is defined by means of BNF notation or syntax diagrams (see later).
Example – Grammar Rule should be: <expression> should be:
Example – Grammar Check -Valid sum = sum + number converted to tokens by lexical analysis becomes: which becomes and then which is valid.
Example – Grammar Check - Invalid sum = sum + + number converted to tokens by lexical analysis becomes: This does not represent a valid statement hence an error message will be returned. Another example could be an invalid number of brackets.
If the high-level language does require variables to be declared. Lexical (& Syntax) Analysis - Including syntax analysis e.g. misspelling of reserved words – PRT instead of PRINT Semantic Analysis - Check the meaning of each sequence of tokens (each sentence).
If the high-level language does not force declaration of variables. Lexical Analysis - Does not include syntax analysis as all words which are not recognised would be assumed to be variables. e.g. PRT would be assumed to be a variable. Syntax (& Semantic) Analysis -Check the meaning of each sequence of tokens (each sentence). - Incorrectly spelt keywords would be picked up here as the tokens would not fit the rules. e.g. PRT X would have tokenised to but its lack of sense would be noticed at this stage.
Syntax (& Semantic) Analysis Label checks Checking to see if a procedure or function which is being called or used actually exists (only relevant if the language does not force the programmer to declare variables & write procedures before using them). Checking to see if a procedure or function which is being called or used actually exists (only relevant if the language does not force the programmer to declare variables & write procedures before using them). Flow of control checks Certain control constructs can only be placed in certain parts of a program. Certain control constructs can only be placed in certain parts of a program. E.g. In Visual Basic you can only Exit Sub inside a procedure, Exit Function inside a Function or matching End Ifs with corresponding Ifs etc… Declaration checks. Check that variables have not been assigned illegal values. Check that variables have not been assigned illegal values. Contents of variables must be of a specific type so an error will be created by the attempted use of anything else. E.g. letters to an integer. E.g. letters to an integer.
Syntax (& Semantic) Analysis Also: Determines priorities of arithmetic operators in an expression. Determines priorities of arithmetic operators in an expression.
Diagnostic Error Messages During both lexical and Syntax (& Semantic) analysis if any errors are detected then diagnostic error messages will be reported.
The variable identifier. The kind of variable. E.g. Simple variable, structured variable such as an array, procedure, keyword etc … E.g. Simple variable, structured variable such as an array, procedure, keyword etc … Type of variable. E.g. Integer, decimal, string etc … E.g. Integer, decimal, string etc … Other Information. E.g. Bounds of array E.g. Bounds of array The block in which a variable is valid (local / global). Symbol Table Syntax (& Semantic Analysis Lexical Analysis
Plenary Describe what happens during syntax analysis, explaining how errors are handled.
Plenary (Tokens) are analysed to check for grammatical correctness (form valid sentences) (Code)/reserved word checked against rules Invalid number of brackets found Determine priorities of arithmetic operators in an expression Produce intermediate code Diagnostic error messages are reported Label checks Flow of control checks Declaration checks