Syntax Directed Translation
1
Syntax Directed Translation
Translate a sequence of tokens
into a sequence of actions
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
Different nonterms can Rules can have conditionals
have different types
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
• Language of expressions that use the three
operators: +, &&, == using the terminal
symbols PLUS, AND , EQUALS, respectively.
• Integer literals are represented by INTLITERAL token
and the tokens TRUE and FALSE represent the
literals true and false.
• A syntax-directed translation that type checks
these expressions; i.e., for type-correct
expressions, the translation will be the type of the
expression (either int or bool), and for expressions
that involve type errors, the translation will be the
special value error 8
SDT Example 3 Type Checking
Cont.
We'll use the following type rules:
• Both operands of the + operator must be of
type int.
• Both operands of the && operator must be of
type bool.
• Both operands of the == operator must have
the same (non-error) type.
9
SDT Example 3 Type Checking Cont.
10
SDT Example 3 Type Checking Cont.
Annotated parse tree for (2+2)==4
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)
Factor intlit (8)
add int
(8) ( Expr )
add
int int Expr + Term
(5) (2)
Term Factor int
(2)
int Factor intlit (2)
(5)
intlit (5)
14
Exercise
Expr -> Expr + Term
• Show the AST for: | Term
(1 + 2) * (3 + 4) * 5 + 6 Term -> Term * Factor
| Factor
Factor -> intlit
| ( Expr )
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
Question: Why do we even need an AST?
– More of a “logical” view of the program
– Generally easier to work with
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
• In our parser, we’ll
define classes for each type of nonterminal
Have a field within the class for each nonterminal attribute
• In the above rule we might represent DList as
public class DList{
public String trans;
}
• When we execute an SDT rule
– we create a new node object for the RHS of SDT rule
– Note the same rule can be executed multiple times during parsing and an
object corresponding to the RHS of SDT rule gets created in each of these
times
22
– propagate its fields with the fields of the LHS node
AST In Code Cont.
Define a class for each AST node:
class ExpNode { } //root class
class IntNode extends ExpNode {
int value;
public IntLitNode(int val) {...}
}
class PlusNode extends ExpNode {
ExpNode left;
ExpNode right;
public PlusNode( ExpNode e1, ExpNode e2 ) { ... }
}
class TimesNode extends ExpNode {
ExpNode left;
ExpNode right;
public TimesNode( ExpNode e1, ExpNode e2 ) { ... }
23
}
Implementing ASTs for Expressions
CFG Translation Rules
Expr -> Expr + Term Expr1.trans = new PlusNode(Expr2.trans, Term.trans)
| Term Expr.trans = Term.trans
Term -> Term * Factor Term1.trans = new TimesNode(Term2.trans, Factor.trans)
| Factor Term.trans = Factor.trans
Factor -> intlit Factor.trans = new IntNode(intlit.value)
| ( Expr ) Factor.trans = Expr.trans
Example: 1 + 2
Expr
PlusNode
ExpNode left:
Expr plus Term ExpNode right:
Term Factor
Factor intlit IntNode IntNode
2 value: 1 value: 2
intlit
1
24
An AST for a code snippet
void foo(int x, int y){
if (x == y){
return;
}
while ( x < y){
cout << “hello”;
FuncBody x = x + 1;
}
}
if while return
== return < print =
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