150 likes | 290 Views
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:
E N D
The Functions and Purposes of Translators Syntax (& Semantic) Analysis
Learning Objectives • Describe what happens during syntax analysis, explaining how errors are handled.
Syntax (& Semantic) Analysis • Semantic analysis: • Check the meaning of each sequence of tokens (each sentence). • 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).
Example – Grammar Rule • <assignment statement> should be: • <variable> <assignment_operator> <expression> • <expression> should be: • <variable> <arithmetic_operator> <variable>
Example – Grammar Check -Valid • sum = sum + number converted to tokens by lexical analysis becomes: • <variable> <assignment_operator> <variable> <arithmetic_operator> <variable> which becomes • <variable> <assignment_operator> <expression> and then • <assignment statement> • which is valid.
Example – Grammar Check - Invalid • sum = sum + + number converted to tokens by lexical analysis becomes: • <variable> <assignment_operator> <variable> <arithmetic_operator> <arithmetic_operator> <variable> • 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 notforce 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 <Variable><Variable> 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). • Flow of control checks • 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. • 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.
Syntax (& Semantic) Analysis • Also: • 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.
Symbol Table Lexical Analysis • The variable identifier. • The kind of variable. • E.g. Simple variable, structured variable such as an array, procedure, keyword etc … • Type of variable. • E.g. Integer, decimal, string etc … • Other Information. • E.g. Bounds of array • The block in which a variable is valid (local / global). Syntax (& Semantic 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