Академический Документы
Профессиональный Документы
Культура Документы
• PA1
– Due today at midnight
Error Handling – README, test case
Syntax-Directed Translation – Your name(s)!
Recursive Descent Parsing • WA1
– Due today at 5pm
Lecture 6
• PA2
– Assigned today
• WA2
– Assigned Tuesday
• Good error handling is not easy to achieve • Not all are supported by all parser generators
1
Error Recovery: Panic Mode Syntax Error Recovery: Panic Mode (Cont.)
Syntax Error Recovery: Error Productions Error Recovery: Local and Global Correction
• Idea: specify in the grammar known common mistakes • Idea: find a correct “nearby” program
– Try token insertions and deletions
• Essentially promotes common errors to alternative
– Exhaustive search
syntax
• Example: • Disadvantages:
– Write 5 x instead of 5 * x
– Hard to implement
– Add the production E … | E E
– Slows down parsing of correct programs
• Disadvantage – “Nearby” is not necessarily “the intended” program
– Complicates the grammar – Not all tools support it
2
Abstract Syntax Tree. (Cont.) Example of Parse Tree
PLUS
• Each grammar symbol may have attributes
– For terminal symbols (lexical tokens) attributes can
5 2 3 be calculated by the lexer
3
Semantic Actions: Notes Dependency Graph
4
A Line Calculator Attributes for the Line Calculator
P 0 0 L 5
• And many other things as well
• prev inherited
– Also used for type checking, code generation, …
+ E3 5 =
0
• All can be • Process is called syntax-directed translation
E4 +
2 E5 3 computed in – Substantial generalization over CFGs
depth-first
int2 2 int3 3 order
5
Constructing An AST Constructing a Parse Tree
• We first define the AST data type • We define a synthesized attribute ast
– Supplied by us for the project – Values of ast values are ASTs
• Consider an abstract tree type with two – We assume that int.lexval is the value of the
constructors: integer lexeme
– Computed using semantic actions
mkleaf(n) = n
• Consider the string int5 ‘+’ ‘(‘ int2 ‘+’ int3 ‘)’ • We can specify language syntax using CFG
• A bottom-up evaluation of the ast attribute:
E.ast = mkplus(mkleaf(5), • A parser will answer whether s L(G)
mkplus(mkleaf(2), mkleaf(3)) – … and will build a parse tree
– … which we convert to an AST
PLUS
– … and pass on to the rest of the compiler
PLUS
5 2 3
Prof. Aiken CS 143 Lecture 6 33 Prof. Aiken CS 143 Lecture 6 34
6
Recursive Descent Parsing Recursive Descent Parsing
E T |T + E E T |T + E
T int | int * T | ( E ) T int | int * T | ( E )
E E
( int5 ) ( int5 )
Prof. Aiken CS 143 Lecture 6 37 Prof. Aiken CS 143 Lecture 6 38
E T |T + E E T |T + E
T int | int * T | ( E ) T int | int * T | ( E )
E E
T T
Mismatch: int is not ( !
int Backtrack …
( int5 ) ( int5 )
Prof. Aiken CS 143 Lecture 6 39 Prof. Aiken CS 143 Lecture 6 40
E T |T + E E T |T + E
T int | int * T | ( E ) T int | int * T | ( E )
E E
T T
Mismatch: int is not ( !
int * T Backtrack …
( int5 ) ( int5 )
Prof. Aiken CS 143 Lecture 6 41 Prof. Aiken CS 143 Lecture 6 42
7
Recursive Descent Parsing Recursive Descent Parsing
E T |T + E E T |T + E
T int | int * T | ( E ) T int | int * T | ( E )
E E
T T
Match! Advance input.
( E ) ( E )
( int5 ) ( int5 )
Prof. Aiken CS 143 Lecture 6 43 Prof. Aiken CS 143 Lecture 6 44
E T |T + E E T |T + E
T int | int * T | ( E ) T int | int * T | ( E )
E E
T T
Match! Advance input.
( E ) ( E )
( int5 ) T ( int5 ) T
45 46
int
Prof. Aiken CS 143 Lecture 6 Prof. Aiken CS 143 Lecture 6
E T |T + E E T |T + E
T int | int * T | ( E ) T int | int * T | ( E )
E E
T T
Match! Advance input. End of input, accept.
( E ) ( E )
( int5 ) T ( int5 ) T
47 48
int int
Prof. Aiken CS 143 Lecture 6 Prof. Aiken CS 143 Lecture 6
8
A Recursive Descent Parser. Preliminaries A Recursive Descent Parser (2)
• Let TOKEN be the type of tokens • Define boolean functions that check the token
– Special tokens INT, OPEN, CLOSE, PLUS, TIMES string for a match of
– A given token terminal
• Let the global next point to the next token bool term(TOKEN tok) { return *next++ == tok; }
– The nth production of S:
bool Sn() { … }
– Try all productions of S:
bool S() { … }
9
When Recursive Descent Does Not Work Elimination of Left Recursion
10