Syntax Directed Translation and Abstract Syntax Tree
Syntax Directed Translation and Abstract Syntax Tree
1
Syntax Directed Translation
2
Syntax Directed Translation
• Augment CFG rules with translation rules (at least 1 per
production)
• Define translation of LHS nonterminal as function of
Constants
the right-hand-side nonterminals' translations
the right-hand-side tokens' values (e.g. the integer value associated
with INTLITERAL token, or the String value associated with an ID
token)
• To translate an input string
Build the parse tree.
Use the translation rules to compute the translation of each
nonterminal in the tree, working bottom up.
3
SDT Example 1
CFG Rules Input string
B -> 0 B.trans = 0 10110
| 1 B.trans = 1
| B 0 B.trans = B2.trans * 2
| B 1 B.trans = B2.trans * 2 + 1 22 B
11 B 0
Translation is 5 B 1
the value of
the binary 2 B 1
input in
decimal 1 B 0
4
SDT Example 2: Declarations Translation is a
String of ids
CFG Rules
DList → ε DList.trans = “”
| DList Decl DList.trans = Decl.trans + “ “ + DList2.trans
Decl → Type id Decl.trans = id.value
Type → int
| bool
“yy xx ” DList
Input string
int xx; “xx ” DList “yy” Decl
bool yy;
Type id
“” DList “xx” Decl
Type id bool
ε
int 5
Exercise
Only add declarations of type int to the output String.
Augment the previous grammar:
CFG Rules
DList → ε DList.trans = “”
| DList Decl DList.trans = Decl.trans + “ “ +
DList2.trans
Decl → Type id ; Decl.trans = id.value
Type → int
| bool
6
SDT Example 2b: ints only Translation is a
String of int ids
only
CFG Rules
DList → ε DList.trans = “”
| Decl DList DList.trans = Decl.trans + “ “ + DList2.trans
Decl → Type id ; if (Type.trans) {Decl.trans = id.value} else {Decl.trans =
“”}
Type → int Type.trans = true
| bool Type.trans = false
Input string
“ xx ” DList
int xx;
bool yy; “xx ” DList “” Decl
false Type id
Different nonterms can “” DList “xx” Decl
have different types
ε
true Type id bool
Rules can have conditionals
int 7
SDT Example 3: Type Checking
9
SDT Example 3 Type Checking Cont.
10
SDT Example 3 Type Checking Cont.
11
SDT for Parsing
In the previous examples, the SDT process
assigned different types to the translation:
– Example 1: tokenized stream to an integer value
– Example 2: tokenized stream to a (java) String
– Example 3: tokenized stream to {int, bool, error}
Next, we’ll go from tokens to an Abstract-Syntax
Tree (AST)
12
Abstract Syntax tree (AST)
• A condensed form of the parse tree
• Operators appear at internal nodes instead of
at leaves.
• "Chains" of single productions are collapsed.
• Lists are "flattened".
• Syntactic details (e.g., parentheses, commas,
semi-colons) are omitted.
• AST is a better structure for later stages
13
Abstract Syntax Tree Example 1
Parse Tree Expr
mult
Example: (5+2)*8
Term
int
Term * Factor
mult (8)
15
AST
• For constructs other than expressions
– Lists of declarations, lists of statements, lists of
parameters, … should be flattened,
– Operators (e.g., "assign", "while", "if") go at
internal nodes, not at leaves
– Syntactic details are omitted
16
AST Example 2
17
AST Example 2 Cont.
18
AST Example 2 Cont.
19
AST for Parsing
In previous slides we did our translation in two steps
– Structure the stream of tokens into a parse tree
– Use the parse tree to build an abstract syntax tree, throw away the
parse tree
In practice, we will combine these into 1 step
20
AST Implementation
How do we actually represent an AST in code?
21
AST in Code
• Note that we’ve assumed a field-like structure in our SDT actions:
DList.trans = Decl.trans + “ “ + DList2.trans
Example: 1 + 2
Expr
PlusNode
ExpNode left:
Expr plus Term ExpNode right:
Term Factor
if while return
x y x y “hello” x +
x 1 25
Note
To implement general AST more classes are
needed:
class ifNode{…}
class whileNode{…}
class lessThanNode{…}
class returnNode{…}
class greaterThanNod{…}
…
26