0% found this document useful (0 votes)
39 views

Syntax Directed Translation

The document discusses syntax-directed translation using attribute grammars. It describes: 1) Attribute grammars associate attributes with grammar symbols and semantic rules to compute attribute values as parses are built. This allows tracking relevant information like types during translation. 2) Syntax-directed definitions specify attributes and semantic rules for each production. Synthesized attributes compute values bottom-up while inherited attributes depend on parent and sibling values. 3) There may be dependencies between attributes that require evaluation order to follow the dependencies from earlier to later attributes. Translation schemes are CFGs with attributes and actions to implement semantic rules during parsing.

Uploaded by

Omkar Basnet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Syntax Directed Translation

The document discusses syntax-directed translation using attribute grammars. It describes: 1) Attribute grammars associate attributes with grammar symbols and semantic rules to compute attribute values as parses are built. This allows tracking relevant information like types during translation. 2) Syntax-directed definitions specify attributes and semantic rules for each production. Synthesized attributes compute values bottom-up while inherited attributes depend on parent and sibling values. 3) There may be dependencies between attributes that require evaluation order to follow the dependencies from earlier to later attributes. Translation schemes are CFGs with attributes and actions to implement semantic rules during parsing.

Uploaded by

Omkar Basnet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Syntax Directed

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

e ::= n | e+e | e−e | e * e | e / e

Attributes for expressions:


type of value: int, float, double, char, string,…
type of construct: variable, constant, operations, …
Attributes for constants: values
Attributes for variables: name, scope
Attributes for operations: arity, operands, operator,…

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

e ::= n | e+e | e−e | e * e | e / e

Parse tree for 5 + 15 * 20: Annotated parse tree:


e e.val=305
e + e
+ e.val=300
e.val=5
5 e * e *
e.val=15 e.val=20
15 20 5
15 20

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

e ::= n | e+e | e−e | e * e | e / e

production Semantic rules


e.val=305
e ::= n e.val = n.val
+ e.val=300 e ::= e1 + e2 e.val = e1.val [+] e2.val
e.val=5
* e ::= e1 - e2 e.val = e1.val [-] e2.val
e.val=15 e.val=20
5 e ::= e1 * e2 e.val = e1.val [*] e2.val
15 20 e ::= e1 / e2 e.val = e1.val [/] e2.val

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

D T::= int T.Type:=integer


L.in=real
T.type=real
T::=real T.type:=real
L.in=real , id3
real L::=L1 ,id L1.in := L.in
Addtype(id.entry,L.in)
L.in=real , id2 L::=id Addtype(id.entry,L.in)

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

e(val=305) e’(inh=s e ::= n e’ e’.inh=n.val;


+

5 yn=305) e.val = e’.syn


n.val=15 e’(inh=15, ε e’ ::= + e e’1 e’1.inh = e’.inh [+] e.val
syn=300) e’.syn = e’1.syn
15
* e.val=20 e’(inh= e’ ::= * e e’1 e’1.inh = e’.inh [*] e.val
syn=300)
e’.syn = e’1.syn
n.val=20 e’(inh=
ε e’ ::= ε e’.syn = e’.inh
syn=20)
20
ε
cs4713 7
Dependences in semantic
evaluation
 If value of attribute b depends on attribute c,
 Semantic rule for b must be evaluated after semantic
rule for c
 There is a dependence from c to b
Annotated parse tree: Dependency graph:

real T.type L3.in L3.addentry


D
L3.in=real id3.entry
T.type=real
L2.in L2.addentry
L2.in=real , id3
real L1.in id2.entry
L1.in=real , id2 L1.addentry

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

real T.type L3.in L3.addentry 6


4 5
L2.in id3.entry
7 L2.addentry 3
8
L1.in
9 L1.addentry 10 id2.entry
2
id1.entry
1
cs4713 9
Evaluation of semantic rules
 Parse-tree methods (compile time)
 Build a parse tree for each input
 Build a dependency graph from the parse tree
 Obtain evaluation order from a topological order of the
dependency graph
 Rule-based methods (compiler-construction time)
 Predetermine the order of attribute evaluation for each
production
 Oblivious methods
 Evaluation order is independent of semantic rules
 Evaluation order forced by parsing methods
 Restrictive in acceptable attribute definitions
cs4713 10
Bottom-up evaluation of
attributes
 S-attributed definitions
 Syntax-directed definitions with only synthesized attributes
 Can be evaluated through post-order traversal of parse tree
 Synthesized attributes and bottom-up parsing
 Keep attribute values of grammar symbols in stack
 Evaluate attribute values at each reduction
 In top-down parsing, the return value of each parsing routine

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

Parse tree for 9+5 with actions


Translation scheme:
E
E ::= T R
R ::= ‘+’ T {print(‘+’)} R1 T R

T ::= num {print(num.val)} 9 print(‘9’) + T print(‘+’) R

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)

 Every attribute value must be available when referenced


 S-attribute of left-hand symbol computed at end of production
 I-attribute of right-hand symbol computed before the symbol
 S-attribute of right-hand symbol referenced after the symbol
D::=T { L.in:=T.type} L
T::= int {T.Type:=integer}
T::=real { T.type:=real}
L::= id , {Addtype(id.entry,L.in) } {L1.in := L.in} L1
L::=id {Addtype(id.entry,L.in)}
cs4713 15
Top-down translation
void parseD()
{ Type t = parseT(); }
parseL(t);
}
Type parseT
{ switch (currentToken()) {
case INT: return TYPE_INT;
case REAL: return TYPE_REAL;
}
}
void parseL(Type in)
{
SymEntry e = parseID();
AddType(e, in);
if (currentToken() == COMMA) {
parseTerminal(COMMA);
parseL(in)
}
}
cs4713 16
Top-down translation
 For each non-terminal A, construct a function that
 Has a formal parameter for each inherited attribute of A
 Returns the values of the synthesized attributes of A
 The code associated with each production does the
following
 Save the s-attribute of each token X into a variable X.x
 Generate an assignment B.s=parseB(B.i1,B.i2,…,B.ik) for each
non-terminal B, where B.i1,…,B.ik are values for the L-
attributes of B and B.s is a variable to store s-attributes of B.
 Copy the code for each action, replacing references to
attributes by the corresponding variables

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

You might also like