Syntax Directed Translation
Syntax Directed Translation
Translation
Attribute grammar and
translation schemes
cs4713 1
Typical implementation of
languages
Source
Lexical Analyzer Program input
Program
Tokens Syntax Analyzer
Parse tree /
Abstract syntax tree Semantic Analyzer Results interpreters
compilers
Attributed AST Intermediate Code
Generator
Code Optimizer
Code Generator
Target
Program
cs4713 2
Syntax-directed translation
Compilers translate language constructs
Need to keep track of relevant information
Attributes: relevant information associated with a construct
cs4713 3
Syntax directed definition
Associate a set of attributes with each grammar symbol
Associate a set of semantic rules with each production
Specify how to compute attribute values of symbols
cs4713 4
Synthesized attribute definition
An attribute is synthesized if
The attribute value of parent is determined from attribute
values of children in the parse tree
cs4713 5
Inherited attribute definition
An attribute is inherited if
The attribute value of a parse-tree node is determined from
attribute values of its parent and siblings
D::=T L
Production Semantic rules
T::= int | real
L ::= L , id | id D::=T L L.in:=T.type
id1
cs4713 6
Synthesized and inherited attributes
Sometimes both synthesized and inherited attributes are
required to evaluate necessary information
e ::= n e’
e (val=305)
e’ ::= +ee’ | *ee’ | ε
n(val=5) e’(inh=5;syn=305) production Semantic rules
id1
id1.entry
cs4713 8
Evaluation order of semantics
Topological order of the dependence graph
Edges go from nodes earlier in the ordering to later nodes
No cycles are allowed in dependence graph
Input Parse Dependency Evaluation order for
string tree graph Semantic rules
Configuration of LR parser:
(s0X1s1X2s2…Xmsm, aiai+1…an$, v1v2…vm)
states inputs values
Right-sentential form: X1X2…Xmaiai+1…an$
Automata states: s0s1s2…sm
Grammar symbols in stack: X1X2…Xm
Synthesized attribute values of Xi vi
cs4713 11
Implementing S-attributed
definitions
Implementation of a desk calculator with an LR parser
(when a number is shifted onto symbol stack,
its value is shifted onto val stack)
production Code fragment
E’ ::= E Print(val[top])
E ::= E1 + T v=val[top-2]+val[top]; top-=2; val[top]=v;
E ::= T
T ::= T1 * F v=val[top-2]*val[top]; top-=2; val[top]=v;
T ::= F
F ::= (E) v=val[top-1]; top-=2; val[top]=v
F ::= n
cs4713 12
L-attributed definitions
A syntax-directed definition is L-attributed if each inherited
attribute of Xj, 1<=j<=n, on the right side of A::=X1X2…Xn,
depends only on
the attributes of X1,X2,…,Xj-1 to the left of Xj in the production
the inherited attributes of A
L-attributed definition Non L-attributed definition
Production Semantic rules
Production Semantic rules
D::=T L L.in:=T.type
A::=L M L.i = A.i
T::= int T.Type:=integer
M.i = L.s
T::=real T.type:=real A.s = M.s
L::=L1 ,id L1.in := L.in A ::= Q R R.i = A.i
Addtype(id.entry,L.in) Q.i = R.s
L::=id Addtype(id.entry,L.in) A.s = Q.s
cs4713 13
Translation schemes
A translation scheme is a CFG where
Attributes are associated with grammar symbols and
Semantic actions are inserted within right sides of productions
Notation for specifying translation during parsing
5 print(‘5’) ε
Treat actions as though they are terminal symbols.
cs4713 14
Designing translation schemes
How to compute attribute values at each production?
D::=T L L.in:=T.type
T::= int T.Type:=integer
T::=real T.type:=real
L::= id, L1 L1.in := L.in; Addtype(id.entry,L.in)
L::=id Addtype(id.entry,L.in)
cs4713 17
Bottom-up translation in Yacc
D::=T { L.in:=T.type} L
T::= int {T.Type:=integer}
T::=real { T.type:=real}
L::= {L1.in := L.in} L1,id {Addtype(id.entry,L.in) }
L::=id {Addtype(id.entry,L.in)}
D : T {$$ = $1; } L
T : INT { $$ = integer; } | REAL { $$ = real; }
L : L COMMA ID { Addtype($3, $0); }
| ID { Addtype($1,$0); }
cs4713 18