Presentation is loading. Please wait.

Presentation is loading. Please wait.

CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.

Similar presentations


Presentation on theme: "CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit."— Presentation transcript:

1 CH4.1 CSE244 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

2 CH4.2 CSE244General  Syntax-directed Definitions.  Translation Schemes  “Programming Language Translation will be guided by the syntax”  Use Attributes to implement translation.  Any symbol of the grammar might have several attributes associated with it.  Translation is specified by presenting the dependencies of the attributes.  This is achieved by the employment of Semantic Rules.  Execution of Semantic Rules obeying the order specified by Attribute interrelations produces the translation.

3 CH4.3 CSE244 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 have the form:  Semantic Rules for a production A   have the form:  b = f (c 1,…,c n )where  (b is synthesized) b is an attribute of A and c 1 …c n are attributes of symbols in .  (b is inherited) b is an attribute of some symbol in  and c 1 …c n are attributes of symbols in A, 

4 CH4.4 CSE244 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. Side-effects… PRODUCTIONSEMANTIC RULE L  E nprint(E.val) E  E 1 + TE.val = E 1.val + T.val E  TE.val = T.val T  T 1 * FT.val = T 1.val * F.val T  FT.val = F.val F  (E)F.val = E.val F  digitF.val = digit.lexval

5 CH4.5 CSE244 Annotated Parse-Trees  Parse-tree that also shows the values of the attributes at each node.  Attributes in the leaves of the annotated parse-tree are determined by the lexical analyzer.  Values of Attributes in inner nodes of annotated parse-tree are 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.

6 CH4.6 CSE244 Draw the Tree Example 3*5+4n

7 CH4.7 CSE244 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  …below in is an inherited attribute of L PRODUCTIONSEMANTIC RULE D  T LL.in = T.type T  int T.type = integer T  real T.type = real L  L 1, idL 1.in = L.in addtype(id.entry, L.in) L  id addtype(id.entry, L.in)

8 CH4.8 CSE244 Draw the Tree Example real id 1, id 2, id 3

9 CH4.9 CSE244 Syntax Trees  Decoupling Translation from Parsing-Trees.  Syntax-Tree: an intermediate representation of the compiler’s input.  Example Procedures: mknode, mkleaf  Employment of the synthesized attribute nptr (pointer) PRODUCTIONSEMANTIC RULE E  E 1 + TE.nptr = mknode(“+”,E 1.nptr,T.nptr) E  E 1 - TE.nptr = mknode(“-”,E 1.nptr,T.nptr) E  TE.nptr = T.nptr T  (E) T.nptr = E.nptr T  idT.nptr = mkleaf(id, id.lexval) T  numT.nptr = mkleaf(num, num.val)

10 CH4.10 CSE244 Draw the Syntax Tree a-4+c

11 CH4.11 CSE244 Bottom-Up Evaluation of S-Attributed Definitions  S-attributed Definition: Syntax-Directed Definition using only Synthesized attributes.  Stack of a LR(1) parser contains states.  Recall that each state corresponds to some grammar symbol (and many different states might correspond to the same grammar symbol)  Extend stack entries to include the attribute(s).  Modify stack contents as productions are selected. StateVal XX.x YY.y ZZ.z Top A  XYZ A.a = f(X.x,Y.y,Z.z) =f(val[top-2],val[top-1],val[top]) StateVal AA.a New Top

12 CH4.12 CSE244Example PRODUCTIONSEMANTIC RULE L  E nprint(E.val) E  E 1 + TE.val = E 1.val + T.val E  TE.val = T.val T  T 1 * FT.val = T 1.val * F.val T  FT.val = F.val F  (E)F.val = E.val F  digitF.val = digit.lexval PRODUCTIONSEMANTIC RULE L  E nprint(val[top]) E  E 1 + Tval[ntop] = val[top-2]+val[top] E  T T  T 1 * Fval[ntop] = val[top-2]*val[top] T  F F  (E)val[ntop] = val[top-1] F  digit

13 CH4.13 CSE244Execution Example: 3*5+4n


Download ppt "CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit."

Similar presentations


Ads by Google