Você está na página 1de 15

 Name-Abhinay Baweja

 Branch-Computer Science
 Roll Number-0800310001
 Subject-Compiler Design
 Topic-Syntax directed translation
 Faculty-Mr.Mighty Abrol
 It is a CFG grammar augmented with:
 “Attributes” (assigned to each grammar symbol).
 Semantic Rules (associated to each production
involving the Attributes of the symbols in the
production).
 Attributes can be synthesized or inherited.
 Semantic Rules for a production A   have
the form:
 b = f (c1,…,cn) where
 (b is synthesized) b is an attribute of A and c1…cn are
attributes of symbols in .
 (b is inherited) b is an attribute of some symbol in 
and c1…cn are attributes of symbols in A,
 Terminals have only synthesized attributes
whose values are provided by the lexical
analyzer.
 The start non-terminal typically has no inherited
attributes.
 [we may allow function calls as semantic-rules
also; these are “Side-effects”…
 Parse-tree that also shows the values of the
attributes at each node.
 Values of Attributes in nodes of annotated
parse-tree are either,
 initialized to constant values or by the lexical
analyzer.
 determined by the semantic-rules.
 If a syntax-directed definition employs only
Synthesized attributes the evaluation of all
attributes can be done in a bottom-up fashion.

 Inherited attributes would require more


arbitrary “traversals” of the annotated parse-
tree.

 A dependency graph suggests possible evaluation


orders for an annotated parse-tree.
Grammar symbols: L, E, T, F, n , + , * ,( , ) , digit
Non-terminals E, T, F have an attribute called val
Terminal digit has an attribute called lexval
The value for lexval is provided by the lexical analyzer.

PRODUCTION SEMANTIC RULE


LEn print(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
F  digit F.val = digit .lexval
Example 3*5+4n
Example 3*5+4n

Print(19)
E val=19

T val=15

T val=3 T val=4

F val=3 F val=5 F val=4

digit digit digit


lexval =3 * lexval =5
+ lexval =4 n
 Even though inherited can be simulated by
synthesized it is more natural to write Syntax-
Directed Definitions using inherited.
 …below in is an inherited attribute of L

PRODUCTION SEMANTIC RULE


DTL L.in = T.type
T  int T.type = integer
T  real T.type = real
L  L1 , id L1.in = L.in
addtype(id.entry, L.in)
L  id addtype(id.entry, L.in)
Example real id1, id2 , id3
Example real id1, id2 , id3 D
addtype(id3,real)
L
in=real
addtype(id2,real)

L
T in=real
type=real
addtype(id1,real)

L
in=real

id id id
real entry=id1
, entry=id2
, entry=id3
 Directed Graph
 Shows interdependencies between attributes.

 Construction:
 Put each semantic rule into the form b=f(c1,…,ck) by
introducing dummy synthesized attribute b for
every semantic rule that consists of a procedure
call.
 E.g.,
 LEn print(E.val)
 Becomes: dummy = print(E.val)
 Etc.
for each node n in the parse tree do
for each attribute a of the grammar symbol at n do
construct a node in the dependency graph for a

for each node n in the parse tree do


for each semantic rule b = f(c1,…,cn)
associated with the production used at n do
for i= 1 to n do
construct an edge from
the node for ci to the node for b
L

Print(19)
E val=19

T val=15

T val=3 T val=4

F val=3 F val=5 F val=4

digit digit digit


lexval =3 * lexval =5
+ lexval =4 n
dummy

val=19

val=15

val=3 val=4

val=3 val=5 val=4

digit digit digit


lexval =3 lexval =5 lexval =4

Você também pode gostar