What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax...
-
Upload
johnathan-fields -
Category
Documents
-
view
214 -
download
0
Transcript of What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax...
What am I?
while b != 0if a > ba := a − belseb := b − areturn a
AST ==AbstractSyntaxTree
Translators
Translators – Module Knowledge Areas• Types of translators and their use
• Lexical analysis
•Syntax analysis• Code generation and optimisation
• Library routines
Translators – Module Knowledge Areas
•Syntax analysis• describe what happens during syntax analysis
• explaining how errors are handled
So, you need to be able to:
• State what syntax analysis is• Describe the process of syntax analysis• Explain the position of syntax analysis in the compilation process. You may
do this through a combination of diagrammatic and written evidence.
• Discuss the link between lexical analysis and syntax analysis with particular reference to the passing of lexical tokens to the syntax analyser
• Explain the part that syntax analysis plays in identifying errors in source code
Translators – Syntax Analysis
IF A>5 THEN A = A * 2ELSE A = A * 3A = A + 2
Explain the relationship between the table and the code (M)
Translators – Syntax Analysis
Explain the relationship between the table and the numbers
1 2 3 4 5 2 6 2 7 8 9 2 6 2 7 10 2 6 2 11 8
What is the significance of ‘2’ in the above sequence? (M)
Translators – Syntax Analysis
The journey
Phase Input Output
Lexer Sequence of characters
Sequence of tokens
Parser Sequence of tokens
Parse tree
Translators – Syntax Analysis
The Parser - Overview
• Performs syntax analysis• Imposes syntactic structure on a sentence
• Parse trees are used to expose the structure
• Parsers, accepts a string of tokens and builds a parse tree representing the program
Translators – Syntax Analysis
Symbol Table - Overview
• The symbol table is a data structure used by all phases of the compiler to keep track of user defined symbols and keywords
• During early phases (lexical and syntax analysis) symbols are discovered and put into the symbol table
• During later phases symbols are looked up to validate their usage
Translators – Syntax Analysis
Symbol Table - Actions
• add a new name• add information for a name• access information for a name• determine if a name is present in the table• remove a name• revert to a previous usage for a name (close
a scope)
Translators – AST
AST - Why
The syntax tree is a structure which can be guaranteed syntactically correct and only readable in one way - there are no issues with precedence of operations in how the program works
Translators – AST
AST - How
What is happening here?
Simple operation of 2 * 3 in a symbolic layout
Translators – AST
AST - How
Which are the operators and which are the operands?The operator is root and the operands are descendants of the root
Translators – AST
AST - How
Consider the program statement 2 * 3 + 4
Create an AST to represent this
Translators – AST
AST - How
Which AST is correct for the statement
2 * 3 + 4?
Why? (M)