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

Syntax Directed Translation

Uploaded by

Rupal Das
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Syntax Directed Translation

Uploaded by

Rupal Das
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Syntax Directed Translation

Syntax Directed Translation


– Guided by context-free grammar
(Translating when parsing )
– Each grammar symbol has associated attributes
– The grammar is augmented by rules (semantic
actions) that specify how the values of attributes
are computed from other attributes, which is
called semantic rule.
– The process of using semantic actions to evaluate
attributes is called syntax-directed translation
Syntax Directed Translation
There are two notations for associating semantic rules with productions
1. Syntax-directed definitions
• High-level specifications for translations
• Hide implementation details
• No need to specify explicitly the order in which translation
take place
EE1+E2 {E.val=E1.val+E2.val}
2. Translation schemes
• Indicate the order in which semantic rules are to be
evaluated.
• Allow some implementation details to be shown
S{B.ps=10}B{S.ht=B.ht}
Conceptual view of syntax-directed
translation
• The input token stream is parsed
• the parse tree is constructed
• next it is traversed in order to execute the corresponding
semantic actions
• evaluation of the semantic actions could be code generation ,
storing symbol table information, or printing error messages

Input Parse Dependency Evaluation order


string tree graph for semantic rules
Synthesized and Inherited Attributes
• Both syntax-directed definitions and translation schemes has
associated sets of attributes with each grammar symbol
• An attribute can represent various things, such as: a string, a
number, a memory address, etc..
There are two kinds of attributes: synthesized and inherited.
• The value of a synthesized attribute is computed from the
values of attributes at the children nodes of the current in the
parse tree.
• The value of an inherited attribute is computed from the
values of attributes at the parent nodes of the current in the
parse tree.
syntax-directed definition Example
SDD of a language for implementing an arithmetic calculator

Production Semantic rule


L→ E n L.val = E.val
E →E1 + T E.val = E1.val + T.val
E →T E.val = T.val
T →T1 * F T.val = T1.val * F.val
T→F T.val = F.val
F→ (E) F.val = E.val
• Symbols E, T, and F are associated with a synthesized
F → digit F.val = digit.lexval
attribute val.
• n is an end marker
• The token digit has a synthesized attribute lexval (it is
assumed that it is evaluated by the lexical analyzer).
Annotated parse tree
For the expression A parse tree showing the values of attributes
3*5+4n L at each node is called Annotated parse tree.
n Notes: The process of computing the
attribute values at the nodes is called
E.val=19 annotating or decorating the parse tree.
E.val=15 + T.val=4

T.val=15 F.val= 4

T.val=3 * F.val= 5 digit.lexval=4

F.val= 3 digit.lexval=5

digit.lexval=3

S-attributed definition:
A syntax-directed definition that uses synthesized
attributes exclusively
inherited attributes
Inherited attributes are values that are computed at a node N in a parse
tree from attribute values of the parent of N, the siblings of N, and N
itself

Production Semantic rule


D→T L L.in := T.type
T →int T.type := integer
T →real T.type := real
L →L1 , D L1.in := L.in
addtype( id.entry, L.in )
L → id addtype( id.entry, L.in )
• Symbol T is associated with a synthesized attribute type.
• Symbol L is associated with an inherited attribute in.
• The addtype() insert the type of the identifier in the symbol
table
inherited attributes
L.In is obtained value from T.type
T.type is lexical value obtained as int or real or char or doube

T.type=real L.in=real

real L.in=real , id3

L.in=real , id2

id1
inherited attributes
Example 2: SDD based on a grammar suitable for top down parsing

Production Semantic Rules


T->FT’ T’.inh=f.val
T.val=T’.syn
T’->*FT’ T 1’.inh = T’.inh * F.val
T’.syn=T 1’.syn
T’->Є T’.syn=T’.inh
F->digit F.val=digit.lexval

• Symbol T and F are associated with a synthesized attribute val.


• Symbol T’ is associated with an inherited attribute inh and an
synthesized attribute syn.
• Here the left operand of * operator is inherited
inherited attributes

T.val=15

F.val=3 T’.inh=3
T’.syn=15

digit.lexval=3 F.val=5 T 1’.inh=15


*
T 1’.syn=15

digit.lexval=5 Є

Annotated parse tree for 3*5


Dependency Graphs

• The interdependencies among the inherited and synthesized attributes at the


nodes in a parse tree can be depicted by a directed graph called a
dependency graph
• If an attribute b at a node in a parse tree depends on an attribute c, then the
semantic rule for b at that node must be evaluated after the semantic rule
that defines c
• If a production A has associated with it a set of semantic rules of the form
b:= f(c1, c2,……..,ck)
where c1, c2,……..,ck are attributes belonging to the grammar symbols
of the production
and b is a synthesized attribute of A or b is an inherited attribute
• In either case, we say that the attribute b depends on attributes c1, c2,……..,ck.
Dependency Graphs

• Suppose A.a:= f(X.x,Y.y) is a semantic rule for the production


AXY
• synthesized attribute A.a that depends on the attributes X.x
and Y.y
• The dependancy among node is the solid lines
• The parse tree is represented by dotted lines

A.a

X.x Y.y
Dependency Graphs
real id1,id2,id3 D

T.type=real entry
L.in=real 6
4 5

real 7
L.in
, id3 3 entry
8 entry

9
L.in
, id2 2 entry
10
entry

id1
1 entry
Dependency Graphs

T.val 9

F.val 3 T’.inh
5 8
T’.syn

1
digit.lexval F.val 4 T ’.inh
* 6 1
T 1’.syn 7

2
digit.lexval Є
Dependency Graphs
• Numbers 1 through 10 represent the nodes of the
dependency graph
• Nodes 1, 2, and 3 represent the attribute entry associated
with each of the leaves labeled id
• Nodes 6, 8, and 10 are the dummy attributes that represent
the application of the function addType to a type and one of
these entry values
• Node 4 represents the attribute T.type, and is actually where
attribute evaluation begins and passed to nodes 5, 7, and 9
Evaluation Order
of Dependency Graph
• Evaluation Order of semantic rules is obtained by a
topological sort of a directed acyclic graph
• In the topological sort, the dependent attributes c1, c2,……..,ck
in a semantic rule b:= f(c1, c2,……..,ck) are available at a node
before f is evaluated
• Evaluation of the semantic rules in this order yields the
translation of the input string
Evaluation Order
of Dependency Graph
• From this topological sort, we obtain the following
program.(ai for the attribute associated with the
node numbered i in the dependency graph.)
– a4=real;
– a5=a4;
– Addtype (id3.entry, a5);
– a7=a5;
– Addtype (id2.entry, a7);
– a9=a7;
– Addtype (id1.entry, a9);
Methods for evaluating semantic rules
• Parse tree methods
I. obtain an evaluation order from a topological sort of the dependency
graph
II. fail to find an order if the dependency graph for the particular parse
tree has a cycle
• Rule based methods
I. semantic rules associated with productions are analyzed, either by
hand, or by specialized tool
II. the order is predetermined at compiler construction time
• Oblivious methods
I. evaluation order is chosen without considering the semantic rules
II. order of evaluation is forced by the parsing method, independent of
the semantic rules
III. restricts the class of syntax directed definitions that can be
Notes: implemented
Rule-based and oblivious methods need not explicitly construct the dependency graph at
compiler time, so they can be more efficient in their use of compiler time and space
L-attributed Definition
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
– 1)The attributes of the symbols X1,X2,…Xj-1 to the left of Xj in the
production and
– 2)The inherited attributes of A.
Note: 1) L-attributes can always be evaluated in depth-first order
2) “L” means “left”, because L-attribute information appears to
flow from left to right
3)Every S-attributed definition is L-attributed, because the
restrictions in the definition apply only to inherited attributes.
Dependency Graphs
real id1,id2,id3 D

1 entry
T.type=real L.in=real
5 6
4

real 7
L.in
, id3 3 entry
8 entry

9
L.in
, id2 2 entry
10
entry

id1
1 entry
Dependency Graphs

1
T.val 9

F.val 3 T’.inh
5 8
T’.syn

1
digit.lexval F.val 4 T ’.inh
* 6 1
T 1’.syn 7

2
digit.lexval Є
Next
Code Optimization

You might also like