Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.

Similar presentations


Presentation on theme: "Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003."— Presentation transcript:

1 Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003

2 Syntax-Directed Translations Translation of languages guided by CFGs Information associated with programming language constructs –Attributes attached to grammar symbols –Values of attributes computed by “semantic rules” associated with grammar productions Two notations for associating semantic rules –Syntax-directed definitions –Translation schemes

3 Semantic Rules Semantic rules perform various activities: –Generation of code –Save information in a symbol table –Issue error messages –Other activities Output of semantic rules is the translation of the token stream

4 Conceptual View Implementations do not need to follow outline literally Many “special cases” can be implemented in a single pass

5 Attributes Each grammar symbol (node in parse tree) has attributes Synthesized attributes based on children Inherited attributes based on siblings and parent A dependency graph represents dependencies between attributes A parse tree showing the values of attributes at each node is an annotated parse tree

6 Semantic Rules Each semantic rule for production A -> α has the form b := f(c 1, c 2, …, c k ) –f is a function –b may be a synthesized attribute of A –b may be an inherited attribute of a grammar symbol on the right side of the production –c 1, c 2, …, c k are attributes belonging to grammar symbols of production An attribute grammar is one in which the functions do not have side effects

7 S-attributed Definitions Synthesized attributes are used extensively in practice S-attributed definition: A syntax-directed definition using only synthesized attributes Parse tree can be annotated by evaluation nodes during a single bottom up pass

8 S-attributed Definition Example ProductionSemantic Rules 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

9 Annotated Parse Tree Example

10 Inherited Attributes Inherited Attributes: –Value at a node in a parse tree depends on attributes of parent and/or siblings –Convenient for expressing dependencies of programming language constructs on context It is always possible to avoid inherited attributes, but they are often convenient

11 Inherited Attributes Example ProductionSemantic Rules D  T LL.in := T.type T  intT.type := integer T  realT.type := real L  L 1, id L 1.in := L.in addtype(id.entry, L.in) L  idaddtype(id.entry, L.in)

12 Annotated Inherited Attributes

13 Dependency Graphs Dependency graph: –Depicts interdependencies among synthesized and inherited attributes –Includes dummy nodes for procedure calls Numbered with a topological sort –If m i  m j is an edge from m i to m j, then m i appears before m j in the ordering –Gives valid order to evaluate semantic rules

14 Creating a Dependency Graph for each node n in parse tree for each attribute a of grammar symbol at n construct a node in dependency graph for a for each node n in parse tree for each semantic rule b := f(c 1, c 2, …, c k ) associated with production used at n for i := 1 to k construct edge from node for c i to node for b

15 Dependency Graph Example

16 Syntax Trees (Abstract) Syntax Trees –Condensed form of parse tree –Useful for representing language constructs –Operators and keywords appear as internal nodes Syntax-directed translation can be based on syntax trees as well as parse trees

17 Syntax Tree Examples

18 Implementing Syntax Trees Each node can be represented by a record with several fields Example: node representing an operator used in an expression: –One field indicates the operator and others point to records for nodes representing operands –The operator is referred to as the “label” of the node If being used for translation, records can have additional fields for attributes

19 Syntax Trees for Expressions Functions will create nodes for the syntax tree –mknode (op, left, right) – creates an operator node with label op and pointers left and right which point to operand nodes –mkleaf(id, entry) – creates an identifier node with label id and a pointer to the appropriate symbol table entry –Mkleaf(num, val) – creates a number node with label num and value val Each function returns pointer to created node

20 Example: a - 4 + c p 1 := mkleaf(id, p a ); P 2 := mkleaf(num, 4); p 3 := mknode('-', p 1, p 2 ); p 4 := mkleaf(id, p c ); p 5 := mknode('+', p 3, p 4 );

21 Constructing Trees for Expressions ProductionSemantic Rules E  E 1 + TE.np := mknode('+', E 1.np, T.np) E  E 1 – TE.np := mknode('-', E 1.np, T.np) E  TE.np := T.np T  (E)T.np := E.np T  idT.np := mkleaf(id, id.entry) T  numT.np := mkleaf(num, value)

22 Directed Acyclic Graphs Called a dag for short Convenient for representing expressions As with syntax trees: –Every subexpression will be represented by a node –Interior nodes represent operators, children represent operands Unlike syntax trees, nodes may have more than one parent Can be created automatically (discussed in textbook)

23 Example: a + a * (b – c) + (b – c) * d

24 Bottom-Up Evaluation Synthesized attributes can be evaluated with single bottom-up pass Parser keeps values of synthesized attributes on stack With each reduction, new synthesized attributes computed based on those at top of stack

25 Bottom-Up Evaluation Example (1) ProductionCode Fragment (1) L  E \nval[ntop] := val[top-1] (2) E  E 1 + tval[ntop] := val[top-2] + val[top] (3) E  T (4) T  T 1 * Fval[ntop] := val[top-2] * val[top] (5) T  F (6) F  (E)val[ntop] := val[top-1] (7) F  digit

26 Bottom-Up Evaluation Example (2) InputStateValRule 3*5+4\n--- *5+4\n33 F3(7) *5+4\nT3(5) 5+4\nT*3_ +4\nT*53_5 +4\nT*F3_5(7) +4\nT3_5(4) … InputStateValRule … +4\nE15(3) 4\nE+15_ \nE+415_4 \nE+F15_4(7) \nE+T15_4(5) \nE19(2) E\n19_ L19(1)

27 Evaluating Attributes Possible evaluation orders depend on order that nodes are created by parser Depth-first search is very common evaluation order L-attributed definitions use this technique Information appears to flow left-to-right Can handle all synthesized and some inherited attributes

28 Depth-First Evaluation procedure dfvisit(n: node); begin for each child m of n, from left to right begin evaluate inherited attributes of m dfvisit(m) end; evaluate synthesized attributes of n end

29 L-attributed Definitions A syntax-directed definition is L-attributed: –If each inherited attribute of X j, for production A  X 1 X 2 …X n (1 <= j <= n), depends on: X 1, X 2, …, X j-1 in the production The inherited attributes of A –Any synthesized attribute is OK All S-attributed definitions are, by this definition, L-attributed

30 Non-L-Attributed Example ProductionSemantic Rule A  L M L.i := l(A.i) M.i := m(L.s) A.s := f(M.s) A  Q R R.i := r(A.i) Q.i := q(R.s) A.s := f(q.s)

31 Translation Schemes Semantic actions are inserted within the right side of productions Placement indicates order of evaluation If we are dealing with both inherited and synthesized attributes: –Each inherited attribute must be computed by action before symbol appears on right side of production –No action may refer to a synthesized attribute of a symbol to the right of the action –Any synthesized attribute of nonterminal on left must be computed after computing all referenced attributes

32 Typesetting Example (1) ProductionSemantic Rules S  B B.ps := 10 S.ht := B.ht B  B 1 B 2 B 1.ps := B.ps B 2.ps := B.ps B.ht := max(B 1.ht, B 2.ht) B  B 1 sub B 2 B 1.ps := B.ps B 2.ps := shrink(B.ps) B.ht := disp(B 1.ht, B 2.ht) B  textB.ht := text.h * B.ps

33 Typesetting Example (2) S  {B.ps := 10} B{S.ht := B.ht} B  {B 1.ps := B.ps} B 1 {B 2.ps := B.ps} B 2 {B.ht := max(B 1.ht, B 2.ht)} B  {B 1.ps := B.ps} B 1 sub{B 2.ps := shrink(B.ps)} B 2 {B.ht := disp(B 1.ht, B 2.ht)} B  text{B.ht := text.h * B.ps}

34 Eliminating Left Recursion Have seen simple and general solution for CFGs Now we must take semantic actions and attributes into account as well First we will examine synthesized attributes A  A 1 Y {A.a := g(A 1.a, Y.y) A  X {A.a := f(X.x)} A  X {R.i := f(X.x)} R {A.a := R.s} R  Y {R 1.i := g(R.i, Y.y)} R 1 {R.s := R 1.s} | ε {R.s := R.i}

35 Evaluating Expressions Example E  E 1 + T {E.val := E 1.val + T.val} E  E 1 – T {E.val := E 1.val – T.val} E  T {E.val := T.val} T  (E) {T.val := E.val} T  num {T.val := num.val} E  T {R.i := T.val} R {E.val := R.s} R  + T {R 1.i := R.i + T.val} R 1 {R.s := R 1.s} | - T {R 1.i := R.i + T.val} R 1 {R.s := R 1.s} | ε {R.s := R.i} T  (E) {T.val := E.val} T  num {T.val := num.val}

36 Creating Syntax Tree Example E  E 1 + T {E.np := mknode('+', E 1.np, T.np)} E  E 1 – T {E.np := mknode('-', E 1.np, T.np)} E  T {E.np := T.np} T  (E) {T.np := E.np} T  id {T.np := mkleaf(id, id.entry)} T  num {T.np := mkleaf(num, value)} E  T {R.i := T.np} R {E.np R.S} R  + T {R 1.i := mknode('+', R.i, T.np)} R 1 {R.s := R 1.s} R  - T {R 1.i := mknode(‘-', R.i, T.np)} R 1 {R.s := R 1.s} R  ε {R.s := R.i} T  (E) {T.np := E.np} T  id {T.np := mkleaf(id, id.entry)} T  num {T.np := mkleaf(num, value)}

37 Designing a Predictive Parser LL(1) Grammars can be implemented using relatively simple top-down parsing techniques For each nonterminal A, construct function: –Parameter for each inherited attribute –Returns synthesized attribute (or attributes) Code decides which production to use based on next input symbol Right side of production considered left to right: –For token X with synthesized attribute x, store X.x –For nonterminal B, generate c := B(b 1,b 2,…,b k ) with call to function for B –Copy other actions into the parser

38 Syntax Tree Code Example function R(i:↑syntax_tree_node):↑syntax_tree_node; var np, i1, s1, s: ↑syntax_tree_node; begin if lookahead = '+' then begin /* Case for R  + T R */ match('+'); np := T; i1 := mknode('+', i, np); s1 := R(il) s := s1; end else if lookahead = '-' then begin /* Case for R  - T R */ … else s := i; /* Case for R  ε */ return s end

39 Generalized Bottom-Up Evaluation Can handle: –All synthesized attributes –All L-attributed definitions based on an LL(1) grammar Can handle some L-attributed definitions based on LR(1) grammars Relies on use of copy rules and markers

40 Copy Rules Consider reduction: A  X Y Suppose X has synthesized attribute X.s X.s will already be on stack before any reductions take place in subtree below Y Therefore, this value can be inherited by Y Define attribute Y.i using a copy rule: Y.i = X.s

41 Copy Rule Example (1) D  T {L.in := T.type} L T  int {T.type := integer} T  real {T.type := real} L  {L 1.in := L.in} L 1, id {addtype(id.entry, L.in)} L  id {addtype(id.entry, L.in)}

42 Copy Rule Example (2) InputStateProduction Used real p, q, r--- p, q, rreal p, q, rTT  real,q, rT id,q, rT LL  id q, rT L,, rT L, id, rT LL  L, id rT L, T L, id T LL  L, id DD  T L

43 Copy Rule Example (3) ProductionCode Fragment D  T L ; T  intval[ntop] := integer T  realval[ntop] := real L  L, idaddtype(val[top], val[top-3]) L  idaddtype(val[top], val[top-1])

44 Limitation of Copy Rules Reaching into stack for an attribute value only works if the position of the value is predictable Here, C inherits synthesized attribute A.s –There may or may not be a B between A and C –C.i may be in either val[top-1] or val[top-2] ProductionSemantic Rules S  aACC.i := A.s S  bABCC.i := A.s C  cC.s := g(C.i)

45 Markers Marker nonterminals generating ε are inserted into the grammar Each embedded action is replaced by a marker with the action attached Actions in the transformed translation scheme terminate productions Markers can often be used to move all actions to the right side of productions

46 Markers Example E  T R R  + T {print('+')} R | - T {print('-')} R | ε T  num {print(num.val)} E  T R R  + T M R | - T N R | ε M  ε {print('+')} N  ε {print('+')} T  num {print(num.val)}

47 Using Markers and Copy Rules ProductionSemantic Rules S  aACC.i := A.s S  bABCC.i := A.s C  cC.s := g(C.i) ProductionSemantic Rules S  aACC.i := A.s S  bABMCM.i := A.s; C.i := M.s M  εM.s := M.i C  cC.s := g(C.i)

48 Using Markers for Other Rules ProductionSemantic Rules S  aACC.i := f(A.s) ProductionSemantic Rules S  aANCN.i := A.s; C.i := N.s N  εN.s := f(N.i)

49 Avoiding Inherited Attributes It is sometimes possible to avoid inherited attributes by rewriting the underlying grammar The goal is to replace inherited attributes with synthesized attributes D  L : T T  integer | real L  L, id | id D  id L L , id L | : T T  integer | real


Download ppt "Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003."

Similar presentations


Ads by Google