Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syntax Directed Translation

Similar presentations


Presentation on theme: "Syntax Directed Translation"— Presentation transcript:

1 Syntax Directed Translation
Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT

2 Syntax-Directed Definitions, I
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,

3 Syntax-Directed Defitions, II
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”…

4 Annotated Parse-Trees
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.

5 Evaluating Attributes.
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.

6 Example of a Syntax-Directed Definition
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  E n 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

7 Draw the Tree Example 3*5+4n

8 Draw the Tree Example 3*5+4n L Print(19) E val=19 T val=15 T val=4
F val=3 F val=5 F val=4 digit lexval =3 digit lexval =5 digit lexval =4 * + n

9 Example with Inherited Attributes
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  T L 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)

10 Draw the Tree Example real id1, id2 , id3

11 Draw the Tree Example real id1, id2 , id3 D , , L in=real L in=real T
addtype(id3,real) L in=real addtype(id2,real) L in=real T type=real addtype(id1,real) L in=real id entry=id1 id entry=id2 id entry=id3 , , real

12 Dependency Graph 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  E n print(E.val) Becomes: dummy = print(E.val) Etc.

13 Dependency Graph Construction
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 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

14 Example I L Print(19) E val=19 T val=15 T val=4 T val=3 F val=3
digit lexval =3 digit lexval =5 digit lexval =4 * + n

15 Example I dummy val=19 val=15 val=4 val=3 val=3 val=5 val=4 digit
lexval =3 digit lexval =5 digit lexval =4

16 Example II D , , L in=real L in=real T type=real L in=real id
addtype(id3,real) L in=real addtype(id2,real) L in=real T type=real addtype(id1,real) L in=real id entry=id1 id entry=id2 id entry=id3 , , real

17 Example II in=real dummy in=real dummy T type=real in=real dummy id
entry=id1 id entry=id2 id entry=id3

18 Evaluating Attributes
Notion: Directed Acyclic Graph Dependency graph should be a DAG (why?) Any topological sort of the directed acyclic graph can be used as a “guide” for attribute evaluation.

19 Example I dummy val=19 val=15 val=4 val=3 val=3 val=5 val=4 digit
lexval =3 digit lexval =5 digit lexval =4

20 Example I as a DAG A topological sort 6,8,7,5,4,3,11,10,9,2,1 1 2 3 9

21 Example II in=real dummy in=real dummy T type=real in=real dummy id
entry=id1 id entry=id2 id entry=id3

22 Example II as a DAG A topological sort: 8,10,3,1,2,4,9,5,6,7 1 2 4 5 3

23 Syntax Trees Decoupling Translation from Parsing-Trees.
Syntax-Tree: an intermediate representation of the compiler’s input. Example Procedures: mknode, mkleaf (create a labeled node – return pointer) Employment of the synthesized attribute nptr (type:pointer) PRODUCTION SEMANTIC RULE E  E1 + T E.nptr = mknode(“+”,E1.nptr ,T.nptr) E  E1 - T E.nptr = mknode(“-”,E1.nptr ,T.nptr) E  T E.nptr = T.nptr T  (E) T.nptr = E.nptr T  id T.nptr = mkleaf(id, id.lexval) T  num T.nptr = mkleaf(num, num.val)

24 Draw the Tree a-4+c

25 Draw The Tree a-4+c + - E nptr c 4 E nptr a T nptr E nptr T nptr
id lexval =a num val =4 id lexval =c - +

26 Using DAGs for expressions
Consider the expression: a+a*(b-c)+(b-c)*d

27 Using DAGs for expressions
Consider the expression: a+a*(b-c)+(b-c)*d + + + + * * * * - - - a a b c b c d a b c d

28 Question How should we modify mkleaf and mknode so that they produce the DAG instead?

29 Question How should we modify mkleaf and mknode so that they produce the DAG instead? mknode and mkleaf create new nodes only when necessary. First they search whether one node with the same exact properties (label + pointers to children if any) has already been defined. If yes node is reused If no, new node is created.

30 DAG representation + i 10 input i := i + 10 DAG form
Array Representation: := + i 10 1 id pointer to i 2 num 10 3 + 1,2 4 := 1,3

31 Implementing the DAG representation
For each node with a signature <op,l,r> Search whether it exists in the list of nodes and if yes, return its value number (e.g., row in the array). most basic implementation: linear search. More sophisticated implementations: arrange nodes in a “array of lists” according to HASH(op || l || r). HASH(op || l || r) = bucket the node with signature <op, l, r> must be placed. Search time drops to O(bucket-length_search + hash-computation)

32 Bottom Up Evaluation of S-Attributed Definitions
A syntax-directed definition is S-Attributed if all attributes are synthesized. Dependency graph is a (directed) tree “pointing upwards” Can be evaluated in bottom up fashion. => Consistent with (bottom-up) Shift/Reduce parsing. Possible to do the evaluation of the attributes using the stack at the time of “reduce” operations!

33 Using the Stack to compute the attributes
Suppose the stack of a general Shift/Reduce parser is equal to $...XYZ Suppose the grammar symbols A, X, Y, Z have the attributes a,x,y,z respectively. The augmented stack with the attributes is as follows: $...[X,X.x=v1][Y,Y.y=v2][Z, Z.z=v3] If we reduce by the production AXYZ that has the semantic action A.a = f(X.x, Y.y, Z.z) we will modify the stack as follows: $...[A,A.a=f (v1,v2,v3)]

34 Recall the S-Attributed Definition
PRODUCTION SEMANTIC RULE L  E n 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

35 Semantic Rules Using the Stack of the S/R parser
PRODUCTION SEMANTIC RULE L  E n print(val[top]) E  E1 + T val[ntop]=val[top-2]+val[top] E  T val[ntop]=val[top] T  T1 * F val[ntop]=val[top-2]*val[top] T  F val[ntop]=val[top] F  (E) val[ntop]=val[top-1] F  digit val[ntop]=val[top] top = top of the stack ntop = top of the stack after popping right hand side of production. val[…] = attribute value of stack contents

36 A trace of a Shift Reduce Parser with Attribute Evaluation
STACK INPUT ACTION

37 A trace of a Shift Reduce Parser with Attribute Evaluation
$ 3*5+4n$ shift $[digit,3] *5+4n$ reduce Fdigit $[F,3] reduce TF $[T,3] $[T,3][*,.] 5+4n$ $[T,3][*,.][digit,5] +4n$ $[T,3][*,.][F,5] reduce TT*F $[T,15] reduce ET $[E,15] $[E,15][+,.] 4n$ $[E,15][+,.][digit,4] n$ $[E,15][+,.][F,4] $[E,15][+,.][T,4] reduce EE*T $[E,19] $[E,19][n,.] reduce LEn ; print(19) $[L,.] ACCEPT


Download ppt "Syntax Directed Translation"

Similar presentations


Ads by Google