1 / 15

The Functions and Purposes of Translators

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:

jayme-bowen
Download Presentation

The Functions and Purposes of Translators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Functions and Purposes of Translators Syntax (& Semantic) Analysis

  2. Learning Objectives • Describe what happens during syntax analysis, explaining how errors are handled.

  3. Translation Process

  4. 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).

  5. Example – Grammar Rule • <assignment statement> should be: • <variable> <assignment_operator> <expression> • <expression> should be: • <variable> <arithmetic_operator> <variable>

  6. 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.

  7. 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.

  8. 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).

  9. 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.

  10. 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.

  11. Syntax (& Semantic) Analysis • Also: • Determines priorities of arithmetic operators in an expression.

  12. Diagnostic Error Messages • During both lexical and Syntax (& Semantic) analysis if any errors are detected then diagnostic error messages will be reported.

  13. 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

  14. Plenary • Describe what happens during syntax analysis, explaining how errors are handled.

  15. 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

More Related