370 likes | 1.17k Views
Syntax Directed Translation. Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT 06269-3155. aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias. Syntax-Directed Definitions, I.
E N D
Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT 06269-3155 aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias
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,
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”…
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.
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.
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
Draw the Tree Example 3*5+4n
Draw the Tree Example 3*5+4n L Print(19) E val=19 T val=15 T val=4 T val=3 F val=3 F val=5 F val=4 digit lexval =3 digit lexval =4 digit lexval =5 * + n
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 LL.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)
Draw the Tree Example real id1, id2 , id3
Draw the Tree Example real id1, id2 , id3 D addtype(id3,real) L in=real addtype(id2,real) L in=real T type=real addtype(id1,real) L in=real id entry=id3 id entry=id1 id entry=id2 , , real
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.
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 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
Example I L Print(19) E val=19 T val=15 T val=4 T val=3 F val=3 F val=5 F val=4 digit lexval =3 digit lexval =4 digit lexval =5 * + n
Example I dummy val=19 val=15 val=4 val=3 val=3 val=5 val=4 digit lexval =3 digit lexval =4 digit lexval =5
Example II D addtype(id3,real) L in=real addtype(id2,real) L in=real T type=real addtype(id1,real) L in=real id entry=id3 id entry=id1 id entry=id2 , , real
Example II in=real dummy in=real dummy T type=real in=real dummy id entry=id3 id entry=id1 id entry=id2
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.
Example I dummy val=19 val=15 val=4 val=3 val=3 val=5 val=4 digit lexval =3 digit lexval =4 digit lexval =5
Example I as a DAG A topological sort 6,8,7,5,4,3,11,10,9,2,1 1 2 3 9 4 5 7 10 6 8 11
Example II in=real dummy in=real dummy T type=real in=real dummy id entry=id3 id entry=id1 id entry=id2
Example II as a DAG A topological sort: 8,10,3,1,2,4,9,5,6,7 1 2 4 5 3 6 7 10 9 8
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)
Draw the Tree a-4+c
Draw The Tree a-4+c + - E nptr c E nptr 4 a T nptr E nptr T nptr T nptr id lexval =a id lexval =c num val =4 - +
Using DAGs for expressions • Consider the expression: • a+a*(b-c)+(b-c)*d
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
Question • How should we modify mkleaf and mknode so that they produce the DAG instead?
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.
DAG representation input i := i + 10 DAG form Array Representation: := + i 10
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)
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!
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 AXYZ 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)]
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
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