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
213 -
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...
![Page 1: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/1.jpg)
What am I?
while b != 0if a > ba := a − belseb := b − areturn a
AST ==AbstractSyntaxTree
![Page 2: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/2.jpg)
Translators
![Page 3: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/3.jpg)
Translators – Module Knowledge Areas• Types of translators and their use
• Lexical analysis
•Syntax analysis• Code generation and optimisation
• Library routines
![Page 4: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/4.jpg)
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
![Page 5: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/5.jpg)
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)
![Page 6: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/6.jpg)
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)
![Page 7: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/7.jpg)
Translators – Syntax Analysis
The journey
Phase Input Output
Lexer Sequence of characters
Sequence of tokens
Parser Sequence of tokens
Parse tree
![Page 8: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/8.jpg)
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
![Page 9: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/9.jpg)
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
![Page 10: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/10.jpg)
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)
![Page 11: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/11.jpg)
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
![Page 12: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/12.jpg)
Translators – AST
AST - How
What is happening here?
Simple operation of 2 * 3 in a symbolic layout
![Page 13: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/13.jpg)
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
![Page 14: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/14.jpg)
Translators – AST
AST - How
Consider the program statement 2 * 3 + 4
Create an AST to represent this
![Page 15: What am I? while b != 0 if a > b a := a − b else b := b − a return a AST == Abstract Syntax Tree.](https://reader036.fdocuments.in/reader036/viewer/2022062805/5697c01c1a28abf838ccfd54/html5/thumbnails/15.jpg)
Translators – AST
AST - How
Which AST is correct for the statement
2 * 3 + 4?
Why? (M)