Title: SyntaxDirected Translation
1Syntax-Directed Translation
Section 2.3 Chapter 5
- Attributes
- Semantic Rules
- Evaluation Order
Token Stream
Parser
Intermediate Code
Well associate code with productions to create
an intermediate code.
2Syntax-directed Translation
Syntax-directed Translation is done by attaching
rules or program fragments to productions in a
grammar.
Well use subscripts to disambiguate.
E ? E1 T compute E1 compute T Es value is
E1s value Ts value
Note that the parse tree would have E1 and T
under this production
3Attributes
Attributes are values associated with terminals
or non-terminals. The most basic attribute The
value of a terminal.
e ? e t t t ? t f f f ? ( e ) id num
f.val is the value of the symbol or value of the
number for f ? id num f.val is the value of the
computed expression for f ? ( e )
We can name attributes whatever we want. Some
names are common .lexval for the string from
lexical analysis.
4Syntax-directed definition (SDD)
A style for specifying the computation of an
attributed grammar.
The subscripts allow us to differentiate
occurrences of the same symbol in a production.
5Visualizing an SDDs translation
3 5
e
e1
t
t
f
f
num
num
A parse tree - No big surprise here.
6Annotated parse tree
3 5
e.val 8
e1.val 3
t.val 5
t.val 3
f.val 5
f.val 3
num.lexval 5
num.lexval 3
We added the annotations to the parse tree.
TT
7Question
3 5
e.val 8
e1.val 3
t.val 5
t.val 3
f.val 5
f.val 3
num.lexval 5
num.lexval 3
In what order should this parse tree be evaluated?
8One possible order (are there others?)
3 5
e.val 8
e1.val 3
t.val 5
t.val 3
f.val 5
f.val 3
num.lexval 5
num.lexval 3
Order is pretty easy for this particular parse
tree.
9How about this grammar?
D ? T L T ? int T ? float L ? L1 , id L ? id
What does this grammar do?
10How about this grammar?
D ? T L T ? int T ? float L ? L1 , id L ? id
What will the semantic rules be for each
production?
11How about this grammar?
D ? T L T ? int T ? float L ? L1 , id L ? id
Where do we get the type from?
What will the semantic rules be for each
production?
12Types of attributes
Synthesized attributes Computed based only on
the node and its children. Inherited attributes
Computed by the parent of a node.
13Inherited attribute example
D ? T L T ? int T ? float L ? L1 , id L ? id
Inherited attribute
Note that the rule L ? id is dependent on its
parent.
14What about order of evaluation now?
D ? T L T ? int T ? float L ? L1 , id L ? id
float x
D
D.inh float
T
L
L.inh float
T.type float
float
id
float
createVariable(id.lexval, float)
Parse tree
Annotated parse tree
What is a valid evaluation order for this tree?
TT
15Order of evaluation
D ? T L T ? int T ? float L ? L1 , id L ? id
float x
D.inh float
L.inh float
T.type float
float
createVariable(id.lexval, float)
How can we determine this?
16Inherited attribute example
D ? T L T ? int T ? float L ? L1 , id L ? id
Inherited attribute
TT
17Dependency Graphs
float x
D.inh float
L.inh float
T.type float
float
createVariable(id.lexval, float)
What is dependent on what here?
18Dependency Graphs
float x
D.inh float
L.inh float
T.type float
float
createVariable(id.lexval, float)
How do I determine the dependencies?
The arrow is the order the nodes must be
evaluated.
19Dependency Graphs
float x
D.inh float
L.inh float
T.type float
float
createVariable(id.lexval, float)
Heres how it works.
20Dependencies
If we use a childs synthesized attribute, were
dependent on the child. If we use a parents
inherited attribute, were dependent on the
parent.
These should be pretty obvious
21What about this case?
A
B
22What about this case?
A
B
There are some syntax directed definitions for
which no evaluation order exists.
23Classes of SDDs
- An SDD is S-attributed if every attribute is
synthesized. - An SDD is L-attributed if each attribute is
either - 1. Synthesized
- Inherited but with this limiting rule Suppose
there is a production A ? X1X2...Xn and there is
an inherited attribute Xi.a computed by a rule
associated with the production. Then the rule
may only use -
- a) Inherited attributes associated with the head
A. - b) Inherited or synthesized attributes to the
left of Xi - c) Inherited or synthesized attributes
associated with Xi itself, but only if no cycles
in the dependency graph are formed.
TT
24Building Syntax Trees
Suppose I want to build a syntax tree? How would
I go about doing it?
25Building Syntax Trees
TT
26Moving these ideas to Lex/Yacc
/ Lex file for expression.y / include
ltstdio.hgt include "y.tab.h" 0-9
yylval atoi(yytext) return NUM \t
\n return '\n' . return
yytext0 int yywrap() return 1
This should look relatively familiar
27Yacc
/ Expression evaluator / include
"stdio.h" define YYSTYPE int token
NUM S E printf("answer d\n", 1) S
S '\n' S S '\n' E printf("answer2
d\n", 3) E E '' T 1 3 E T
1 T T '' NUM 1 3 T
NUM 1
Production
Semantic Rule
What does this grammar do (why the S rules?)