0% found this document useful (0 votes)
4 views62 pages

Chapter 4

Chapter Four discusses Syntax-Directed Translation, focusing on how grammar symbols are associated with attributes to represent programming language constructs. It introduces Syntax-Directed Definitions and Translation Schemes, explaining how semantic rules are evaluated to generate intermediate codes, perform type checking, and more. The chapter also covers annotated parse trees, dependency graphs, evaluation orders, and examples of synthesized and inherited attributes in attribute grammars.

Uploaded by

promoha18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views62 pages

Chapter 4

Chapter Four discusses Syntax-Directed Translation, focusing on how grammar symbols are associated with attributes to represent programming language constructs. It introduces Syntax-Directed Definitions and Translation Schemes, explaining how semantic rules are evaluated to generate intermediate codes, perform type checking, and more. The chapter also covers annotated parse trees, dependency graphs, evaluation orders, and examples of synthesized and inherited attributes in attribute grammars.

Uploaded by

promoha18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CHAPTER FOUR

Syntax-Directed Translation

1
Outline
• Introduction
• Syntax-Directed Definitions and Translation Schemes
• Syntax-Directed Definitions
• Annotated Parse Tree
• Annotating a Parse Tree With Depth-First Traversals
• Dependency Graph
• Evaluation order
• S-Attributed Definitions
• Bottom-Up Evaluation of S-Attributed Definitions
• Top-Down Evaluation of S-Attributed Definitions
• L-Attributed Definitions
• Translation Schemes
2
Introduction
• Grammar symbols are associated with attributes to
associate information with the programming language
constructs that they represent.
• Values of these attributes are evaluated by the semantic
rules associated with the production rules.
• Evaluation of these semantic rules:
– may generate intermediate codes
– may put information into the symbol table
– may perform type checking
– may issue error messages
– may perform some other activities
– in fact, they may perform almost any activities.

3
Syntax-Directed Definitions and Translation
Schemes
• When we associate semantic rules with productions, we use two
notations:
– Syntax-Directed Definitions
– Translation Schemes
• Syntax-Directed Definitions:
– give high-level specifications for translations
– hide many implementation details such as order of evaluation of
semantic actions.
– We associate a production rule with a set of semantic actions, and we do
not say when they will be evaluated.
• Translation Schemes:
– indicate the order of evaluation of semantic actions associated with a
production rule.
– In other words, translation schemes give more information about
implementation details. 4
Syntax-Directed Definitions
• A syntax-directed definition is a generalization of a
context-free grammar in which:
– Each grammar symbol is associated with a set of attributes.
– This set of attributes for a grammar symbol is partitioned into
two subsets called synthesized and inherited attributes of that
grammar symbol.
– Each production rule is associated with a set of semantic rules.
• An attribute can represent anything we choose:
– a string, a number, a type, a memory location, intermediate
program representation etc...
– The value of a synthesized attribute is computed from the values
of attributes at the children of that node in the parse tree.
– The value of an inherited attribute is computed from the values
of attributes at the siblings and parent of that node in the parse
tree.
5
Syntax-Directed Definitions…
• Semantic rules set up dependencies between attributes which
can be represented by a dependency graph.
• This dependency graph determines the evaluation order of
these semantic rules.
• Evaluation of a semantic rule defines the value of an
attribute.
• But a semantic rule may also have some side effects such as
printing a value.
• A depth-first traversal algorithm traverses the parse tree
thereby executing semantic rules to assign attribute values.
• After the traversal is complete the attributes contain the
translated form of the input.

6
Syntax-Directed Definition…
• In a syntax-directed definition, each production A → α is
associated with a set of semantic rules of the form:
b=f(c1,c2,…,cn) where f is a function,
and b can be one of the followings:
➔ b is a synthesized attribute of A and c1,c2,…,cn are
attributes of the grammar symbols in the production
( A → α ). For A → C A.b = C.c
OR
➔ b is an inherited attribute one of the grammar symbols in
α (on the right side of the production),
and c1,c2,…,cn are attributes of the grammar symbols in the
production ( A → α ). For A →C C.c = A.b
7
Attribute Grammar
• So, a semantic rule b=f(c1,c2,…,cn) indicates that the
attribute b depends on attributes c1,c2,…,cn.

• In a syntax-directed definition, a semantic rule:


– may just evaluate a value of an attribute or
– it may have some side effects such as printing values.

• An attribute grammar is a syntax-directed definition in which


the functions in the semantic rules cannot have side effects
(they can only evaluate values of attributes).

8
Annotated Parse Tree

• A parse tree showing the values of attributes at


each node is called an annotated parse tree.
• The process of computing the attributes values at
the nodes is called annotating (or decorating) of
the parse tree.
• Of course, the order of these computations
depends on the dependency graph induced by
the semantic rules.

9
Annotating a Parse Tree With
Depth-First Traversals

procedure visit(n : node);


Begin
for each child m of n, from left to right do
visit(m);
evaluate semantic rules at node n
end

10
Example: Synthesized Attributed grammar that calculate the
value of expression

Production Semantic Rules


L→En print([Link])
E → E1 + T [Link] = [Link] + [Link]
E→T [Link] = [Link]
T → T1 * F [Link] = [Link] * [Link]
T→F [Link] = [Link]
F→(E) [Link] = [Link]
F → digit [Link] = [Link]

❑ It specifies a simple calculator that reads an input line


containing an arithmetic expression involving:
❑ digits, parenthesis, the operator + and *, followed by a new line
character n, and
❑ prints the value of expression. 11
Example: Synthesized Attributed grammar that
calculate the value of expression
Production Semantic Rules
L→En print([Link])
E → E1 + T [Link] = [Link] + [Link]
E→T [Link] = [Link]
T → T1 * F [Link] = [Link] * [Link]
T→F [Link] = [Link]
F→(E) [Link] = [Link]
F → digit [Link] = [Link]
Input: 9+5+2n
• Symbols E, T, and F are associated with a synthesized
attribute val.
• The token digit has a synthesized attribute lexval
(it is assumed that it is evaluated by the lexical analyzer).
12
Depth-First Traversals: Example
Input: 9+5+2n
L print (16)

[Link]=16

[Link]=14 [Link]=2

[Link]=9 [Link]=2
[Link]=5
Note: all attributes
[Link]=9 in this example
[Link]=5
are of synthesized
[Link]=9 attributes

9 + 5 + 2 n 13
Annotated Parse Tree: Example
Input: 5+3*4 L (print 17)

[Link]=17 n

[Link]=5 + [Link]=12

[Link]=5 [Link]=3 * [Link]=4

[Link]=5 [Link]=3 [Link]=4

[Link]=5 [Link]=3

14
Quiz: Synthesized Attributed grammar that
calculate the value of expression

❑Given the expression 5+3*4 followed by new line, the


program prints 17.
❑ Draw the decorated parse tree for input: 1*2n

❑ Draw the annotated parse tree for input: 5*3+4n

❑ Draw the annotated parse tree for input: 5*(3+4)n

15
Exercises : Synthesized Attributed grammar
that calculate the value of expression
• By making use of SDD of slide 11: give annotated parse
trees for the following expressions:

a) (3+4) * (5+6)n
b) 7*5*9*(4+5)n
c) (9+8*(7+6)+5)*4n

16
Syntax-Directed Definition: Exercises
Production Semantic Rules
N → L1.L2 N.v = L1.v + L2.v / (2L2.l)
L1 → L2 B L1.v = 2 * L2.v + B.v
L1.l = L2.l + 1
L→B L.v = B.v
L1.l = 1
B→0 B.v = 0
B→1 B.v = 1

❑ Draw the decorated parse tree for input:


a - 1011.01
b – 11.1
c – 1001.001
17
Dependency Graphs for Attributed Parse
Trees
• Annotated parse tree shows the values of attributes.
• Dependency graph helps us to determine how those
values can be computed.
• The attributes should be evaluated in a given order
because they depend on one another.
• The dependency of the attributes is represented by a
dependency graph.
• b(j) -----D()----> a(i) if and only if there exists a
semantic action such as a (i) := f (... b (j) ...)
• Algorithm for the construction of the dependency graph
18
Dependency graph : Algorithm

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, c2, ... ck)
associated with the production used at n do
for i:= 1 to k do
construct an edge from the node for ci to the node for b;

19
Dependency Graphs for
Attributed Parse Trees Direction of value
dependence
Synthesized A.a
attributes A.a= (X.x, Y.y)
X.x Y.y

A.a
A → XY X.x= (A.a, Y.y)
X.x Y.y

Inherited A.a
attributes
X.x Y.y Y.x= (A.a, X.x)

20
Annotated Parse Tree: Example
Input: 5+3*4 L (print 17)

[Link]=17 n

[Link]=5 + [Link]=12

[Link]=5 [Link]=3 * [Link]=4

[Link]=5 [Link]=3 [Link]=4

[Link]=5 [Link]=3

21
Dependency Graph
Input: 5+3*4 L (print 17)

[Link]=17

[Link]=5 [Link]=12

[Link]=5 [Link]=3 [Link]=4

[Link]=5 [Link]=3 [Link]=4

[Link]=5 [Link]=3

val is synthesized attribute


22
Syntax-Directed Definition: Inherited
Attributes Inherited
Production Semantic Rules
D→TL [Link] = [Link]
T → int [Link] = integer
T → real [Link] = real synthesized
L → L1 , id [Link] = [Link],
addtype([Link],[Link])
L → id addtype([Link],[Link])

• Symbol T is associated with a synthesized attribute


type.
• Symbol L is associated with an inherited attribute inh.
Input: real id1,id2,id3
23
A Dependency Graph – Inherited Attributes
Input: real id1,id2,id3

[Link] =real [Link] = real

[Link] = real [Link]


real ,
Annotated
Parse tree
[Link] = real , [Link]

[Link]
24
A Dependency Graph – Inherited Attributes
Input: real id1,id2,id3
D
1-10 represents
nodes of [Link] =real 4 5 [Link] = real 6
Dependency
graph 3
real 7 [Link] = real 8 [Link]
,

[Link] = real [Link] 2


9 10 ,
[Link]
1
25
SDD based on a grammar suitable for top-down
parsing
Production Semantic Rules
T → FT’ T’.inh = [Link]
[Link] = T’.syn
T’ → *FT1’ T1’.inh = T’.inh X [Link]
T’.syn = T1’.syn
T’ → ε T’.syn = T’.inh
F → digit [Link] = [Link]

• The SDD above computes terms like 3 * 5 and 3 * 5 * 7.


• Each of the non-terminals T and F has a synthesized attribute val;
• The terminal digit has a synthesized attribute lexval.
• The non-terminal T’ has two attributes:
• an inherited attribute inh and
• a synthesized attribute syn. 26
Annotated parse tree for 3*5

[Link] = 15

[Link] = 3 T’.inh = 3
T’.syn = 15

[Link] = 3 T1’.inh = 15
* [Link] = 5 T1’.syn = 15

[Link] = 5 ε

27
Dependency graph for the annotated parse tree
of 3*5
[Link] = 15 9

[Link] = 3 5 T’.inh = 3
3 T’.syn = 15 8

[Link] = 3 4 6 T1’.inh = 15
* [Link] = 5 T1’.syn = 15
1 7

2
Synthesized attribute [Link] = 5 ε
Inherited attribute
28
Dependency graph: Exercises
Production Semantic Rules
N → L1.L2 N.v = L1.v + L2.v / (2L2.l)
L1 → L2 B L1.v = 2 * L2.v + B.v
L1.l = L2.l + 1
L→B L.v = B.v
L1.l = 1
B→0 B.v = 0
B→1 B.v = 1

❑ Draw the decorated parse tree and


❑ Draw the dependency graph for input:
a - 1011.01
b – 11.1
c – 1001.001 29
Evaluation Order
• A topological sort of a directed acyclic graph (DAG) is
any ordering m1, m2, …, mn of the nodes of the graph,
such that:
if mi → mj is an edge, then mi appears before mj

• Any topological sort of a dependency graph gives a


valid evaluation order of the semantic rules.

• Note: It is not possible to find such an order for an


acyclic graph.

30
Example Parse Tree with Topologically Sorted
Actions
Topological sort:
1. Get [Link] D
2. Get [Link]
3. Get [Link]
4. [Link]=real [Link] =real 4 5 [Link] = real 6
5. [Link]=[Link]
6. addtype([Link],
3
[Link])
real 7 [Link] = real 8 [Link]
7. [Link]=[Link]
8. addtype([Link], ,
[Link])
9. [Link]=[Link] L .in = real Id .entry 2
9 3 10 , 2
10. addtype([Link],
[Link])
[Link]
1
31
Syntax-Directed Definition – Example
Production Semantic Rules
E → E1 + T [Link]=newtemp(), [Link] = [Link] || [Link] || add [Link],[Link],[Link]
E→T [Link] = [Link], [Link]=[Link]
T → T1 * F [Link]=newtemp(), [Link] = [Link] || [Link] || mult [Link],[Link],[Link]
T→F [Link] = [Link], [Link]=[Link]
F→(E) [Link] = [Link], [Link]=[Link]
F → id [Link] = [Link], [Link]=“”

• Symbols E, T, and F are associated with synthesized attributes


loc and code.
• The token id has a synthesized attribute name (it is assumed
that it is evaluated by the lexical analyzer).
• It is assumed that || is the string concatenation operator.

32
S-Attributed Definitions
• Syntax-directed definitions are used to specify syntax-directed
translations that guarantee an evaluation order.
• We would like to evaluate the semantic rules during parsing
(i.e. in a single pass, we will parse and we will also evaluate
semantic rules during the parsing).
• We will look at two sub-classes of the syntax-directed
definitions:
– S-Attributed Definitions: only synthesized attributes used
in the syntax-directed definitions.
– L-Attributed Definitions: in addition to synthesized
attributes, we may also use inherited attributes.

• These classes of SDD can be implemented efficiently in


connection with top-down and bottom-up parsing.

33
S-Attributed Definitions
• A syntax-directed definition that uses synthesized
attributes exclusively is called an S-attributed
definition (or S-attributed grammar)

• A parse tree of an S-attributed definition is annotated


with a single bottom-up traversal.
• Bottom-up parser uses depth first traversal.
• A new stack is maintained to store the values of the
attributes as in the following example.

• Yacc/Bison only support S-attributed definitions

34
Example: Attribute Grammar in Yacc

%{
#include <stdio.h>
void yyerror(char *);
%}
%token INTEGER
%%
program:
program expr '\n' { printf("%d\n", $2); }
|
;
expr:
INTEGER { $$=$1;}
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
; Synthesized
%% attribute of
parent node expr
35
Bottom-Up Evaluation of S-Attributed
Definitions
• We put the values of the synthesized attributes of the grammar symbols into
a parallel stack.
– When an entry of the parser stack holds a grammar symbol X (terminal
or non-terminal), the corresponding entry in the parallel stack will hold
the synthesized attribute(s) of the symbol X.
• We evaluate the values of the attributes during reductions.

A → XYZ A.a=f(X.x,Y.y,Z.z) where all attributes are synthesized.

stack parallel-stack
top → Z Z.z
Y Y.y
X X.x top → A A.a
. . . .
36
Bottom-Up Eval. of S-Attributed Definitions…
Production Semantic Rules
L→En print(val[top-1])
E → E1 + T val[ntop] = val[top-2] + val[top]
E→T $$ = $1 + $3; in yacc
T → T1 * F val[ntop] = val[top-2] * val[top]
T→F
F→(E) val[ntop] = val[top-1]
F → digit
• At each shift of digit, we also push [Link] into val-stack.
• At all other shifts, we do not put anything into val-stack
because other terminals do not have attributes (but we
increment the stack pointer for val-stack).

37
Canonical LR(0) Collection for The Grammar
I0: L’→ ..L
L I1: L’→L . I7: L →En . I11: E →E+T. *
L→
E→ ..En
E I2: L →E n ..
n
+ .
I8: E →E+ T
T
F
.
T →T *F
9

E→
T→ ..
E+T
T E →E +T ..
T → T*F
T→ F
(
4
5

T→
F→..
T*F
F
(E)
T I3: E →T ..
T →T *F
..
F → (E)
F→ d
d
6

F→ d
F I4: T →F . *

.
( I5: F → . I9: T →T* F
..
F → (E)
F
I12: T →T*F .
E→
E→
.. ( E)
E+T E
F → id
(
d
5

T→ .. T
T*F
..
6

T→
F→.. F
(E)
T
3
I10: F →(E )
E →E +T
)
+
I13: F →(E) .
F→ d F
4 8
(
d I6: F →d . d
5
6

38
Bottom-Up Evaluation of S-Attributed
Definitions in Yacc: Example 1
• At each shift of digit, we also push [Link] into val-stack.
stack val-stack input action semantic rule
0 5+3*4n s6 [Link](5) into val-stack
0d6 5 +3*4n F→d [Link]=[Link] – do nothing
0F4 5 +3*4n T→F [Link]=[Link] – do nothing
0T3 5 +3*4n E→T [Link]=[Link] – do nothing
0E2 5 +3*4n s8 push empty slot into val-stack
0E2+8 5- 3*4n s6 [Link](3) into val-stack
0E2+8d6 5-3 *4n F→d [Link]=[Link] – do nothing
0E2+8F4 5-3 *4n T→F [Link]=[Link] – do nothing
0E2+8T11 5-3 *4n s9 push empty slot into val-stack
0E2+8T11*9 5-3- 4n s6 [Link](4) into val-stack
0E2+8T11*9d6 5-3-4 n F→d [Link]=[Link] – do nothing
0E2+8T11*9F12 5-3-4 n T→T*F [Link]=[Link]*[Link]
0E2+8T11 5-12 n E→E+T [Link]=[Link]*[Link]
0E2 17 n s7 push empty slot into val-stack
0E2n7 17- $ L→En print(17), pop empty slot from val-stack
0L1 17 $ acc
39
Bottom-Up Evaluation of S-Attributed
Definitions in Yacc: Example 2

40
Top-Down Evaluation of S-Attributed Definitions

Productions Semantic Rules


A→B print(B.n0), print(B.n1)
B → 0 B1 B.n0=B1.n0+1, B.n1=B1.n1
B → 1 B1 B.n0=B1.n0, B.n1=B1.n1+1
B→ B.n0=0, B.n1=0

where B has two synthesized attributes (n0 and n1).

41
Top-Down Evaluation of S-Attributed Definitions
• In a recursive predictive parser, each non-terminal corresponds
to a procedure.

procedure A() {
call B(); A→B
}
procedure B() {
if (currtoken=0) { consume 0; call B(); } B→0B
else if (currtoken=1) { consume 1; call B(); } B→1B
else if (currtoken=$) {} // $ is end-marker B→
else error(“unexpected token”);
}

42
Top-Down Evaluation of S-Attributed Definitions
procedure A() {
int n0,n1; Synthesized attributes of non-terminal B
call B(&n0,&n1); are the output parameters of procedure B.
print(n0); print(n1);
} All the semantic rules can be evaluated
procedure B(int *n0, int *n1) { at the end of parsing of production rules
if (currtoken=0)
{int a,b; consume 0; call B(&a,&b); *n0=a+1; *n1=b;}
else if (currtoken=1)
{ int a,b; consume 1; call B(&a,&b); *n0=a; *n1=b+1; }
else if (currtoken=$) {*n0=0; *n1=0; } // $ is end-marker
else error(“unexpected token”);
}

43
L-Attributed Definitions
• S-Attributed Definitions can be efficiently implemented.
• We are looking for a larger (larger than S-Attributed
Definitions) subset of syntax-directed definitions which
can be efficiently evaluated.

L-Attributed Definitions

• L-Attributed Definitions can always be evaluated by the


depth first visit of the parse tree.
• This means that they can also be evaluated during the
parsing.

44
L-Attributed Definitions
• A syntax-directed definition is L-attributed if each
inherited attribute of Xj, where 1jn, on the right side
of A → X1X2...Xn depends only on:
1. The attributes of the symbols X1,...,Xj-1 to the left of Xj in the
production and
2. the inherited attribute of A A.a

A → X1X2
Dependency of X2.x
inherited attributes X1.x

45
L-Attributed Definitions
• L-attributed definitions allow for a natural order of
evaluating attributes: depth-first and left to right.
A→ XY
X.i=A.i A.s=Y.s
X.i = A.i A
A.s= Y.s
Y.i= X.s
X Y.i=X.s Y

Every S-attributed definition is L-attributed, the restrictions


only apply to the inherited attributes (not to synthesized
attributes). 46
A Definition which is NOT L-Attributed
Productions Semantic Rules
A→LM [Link]=l(A.i), [Link]=m(L.s), A.s=f(M.s)
A→QR [Link]=r([Link]), [Link]=q(R.s), A.s=f(Q.s)

• This syntax-directed definition is not L-attributed because the


semantic rule [Link]=q(R.s) violates the restrictions of L-
attributed definitions.
• When [Link] must be evaluated before we enter to Q because it is
an inherited attribute.
• But the value of [Link] depends on R.s which will be available
after we return from R.
• So, we are not be able to evaluate the value of [Link] before we
enter to Q.
47
Example: L-attributed definition
Production Semantic Rules
T → FT’ T’.inh = [Link]
[Link] = T’.syn
T’ → *FT1’ T1’.inh = T’.inh X [Link]
T’.syn = T1’.syn
T’ → ε T’.syn = T’.inh
F → digit [Link] = [Link]

• The SDD above computes terms like 3 * 5 and 3 * 5 * 7.


• Each of the non-terminals T and F has a synthesized attribute val;
• The terminal digit has a synthesized attribute lexval.
• The non-terminal T’ has two attributes:
• an inherited attribute inh and
• a synthesized attribute syn. 48
Exercises
• Suppose that we have a production A → BCD. Each of the
four non-terminals A, B, C, and D have two attributes:
– s is a synthesized attribute, and
– i is an inherited attribute.
• For each of the sets of rules below, tell whether:
(i) the rules are consistent with an S-attributed definition
(ii) The rules are consistent with an L-attributed definition,
and
(iii) whether the rules are consistent with any evaluation
order at all?

49
Exercises…

1) A.s = B.i + C.c


2) A.s = B.i + C.c and D.i = A.i + B.s
3) A.s = B.s + D.s
4) A.s = D.i, B.i = A.s + C.s, C.i = B.s, and D.i = B.i + C.i

50
Translation Schemes
• In a syntax-directed definition, we do not say anything about
the evaluation times of the semantic rules (when the semantic
rules associated with a production should be evaluated?).

• A translation scheme is a context-free grammar in which:


– attributes are associated with the grammar symbols and
– semantic actions enclosed between braces { } are inserted
within the right sides of productions.

• Ex: A → { ... } X { ... } Y { ... }

Semantic Actions
51
Translation Schemes
• When designing a translation scheme, some restrictions should
be observed to ensure that an attribute value is available when
a semantic action refers to that attribute.
• These restrictions (motivated by L-attributed definitions)
ensure that a semantic action does not refer to an attribute
that has not yet computed.
• In translation schemes, we use semantic action terminology
instead of semantic rule terminology used in syntax-directed
definitions.
• The position of the semantic action on the right side indicates
when that semantic action will be evaluated.

52
Translation Schemes for S-attributed Definitions
• If our syntax-directed definition is S-attributed, the
construction of the corresponding translation scheme will be
simple.
• Each associated semantic rule in a S-attributed syntax-directed
definition will be inserted as a semantic action into the end of
the right side of the associated production.
Production Semantic Rule
E → E1 + T [Link] = [Link] + [Link] a production of
a syntax directed definition

E → E1 + T { [Link] = [Link] + [Link] } the production of the corresponding


translation scheme

53
A Translation Scheme Example
• A simple translation scheme that converts infix expressions to
the corresponding postfix expressions.

E→TR
R → + T { print(“+”) } R1
R→
T → id { print([Link]) }

a+b+c ab+c+

infix expression postfix expression

54
A Translation Scheme: Example…
E

T R

id {print(“a”)} + T {print(“+”)} R

id {print(“b”)} + T {print(“+”)} R

id {print(“c”)} 

The depth first traversal of the parse tree (executing the semantic actions in
that order) will produce the postfix representation of the infix expression.
55
Eliminating Left Recursion from
Translation Scheme
A → A1 Y { A.a = g(A1.a,Y.y) } a left recursive grammar with
A → X { A.a=f(X.x) } synthesized attributes (a,y,x).

eliminate left recursion


inherited attribute of the new non-terminal
synthesized attribute of the new non-terminal

A → X { [Link]=f(X.x) } R { A.a=[Link] }
R → Y { [Link]=g([Link],Y.y) } R1 { [Link] = [Link]}
R →  { [Link] = [Link] }
56
Evaluating attributes
A parse tree of left recursive grammar

A Y A.a=g(f(X.x),Y.y)
parse tree of non-left-recursive grammar
X X.x=f(X.x) A

X [Link]=f(X.x) R A.a=g(f(X.x,Y.y)

Y [Link]=g(f(X.x),Y.y) R1 [Link]=g(f(X.x),Y.y)

 [Link]=g(f(X.x),Y.y)

57
Eliminating Left Recursion from Translation
Scheme
• A translation scheme with a left recursive grammar.

E → E1 + T { [Link] = [Link] + [Link] }


E → E1 - T { [Link] = [Link] - [Link] }
E→T { [Link] = [Link] }
T → T1 * F { [Link] = [Link] * [Link] }
T→F { [Link] = [Link] }
F → ( E ) { [Link] = [Link] }
F → digit { [Link] = [Link] }

• When we eliminate the left recursion from the grammar (to get
a suitable grammar for the top-down parsing) we also have to
change semantic actions
58
Eliminating Left Recursion (cont.)
inherited attribute synthesized attribute

E → T { [Link]=[Link] } A { [Link]=[Link] }
A → + T { [Link]=[Link]+[Link] } A1 { [Link] = [Link]}
A → - T { [Link]=[Link] } A1 { [Link] = [Link]}
A →  { [Link] = [Link] }
T → F { [Link]=[Link] } B { [Link]=[Link] }
B → * F { [Link]=[Link]*[Link] } B1 { [Link] = [Link]}
B →  { [Link] = [Link] }
F → ( E ) { [Link] = [Link] }
F → digit { [Link] = [Link] }

59
s-attributed versus l-attributed
• S-attributed SDT
• An S-attributed SDT exclusively uses synthesized attributes, which are
values computed from child nodes in the parse tree.
• This means that the attributes of a non-terminal can only depend on the
attributes of its children.
• S-attributed SDTs are evaluated using a bottom-up parsing approach,
where the semantic actions are placed at the rightmost position of the
production rules.
• For example, in a production like A→BC, the attributes of A can only
be derived from the attributes of B and C

60
L-attributed SDT
• In contrast, an L-attributed SDT utilizes both synthesized and inherited
attributes.
• Inherited attributes can derive their values from the parent node or from
left siblings, but not from right siblings.
• This allows for more complex relationships in the attribute evaluation.
• L-attributed SDTs are evaluated using a depth-first and left-to-
right parsing strategy, with semantic actions being placed anywhere in
the production rules.
• For instance, in the production S→ABC, the attribute of B can be
influenced by S (its parent) and A (its left sibling), but not by C (its right
sibling)

61
Key Differences
• Attribute Types:
– S-attributed: Only synthesized attributes.
– L-attributed: Both synthesized and inherited attributes.
• Evaluation Order:
– S-attributed: Evaluated in a bottom-up manner.
– L-attributed: Evaluated in a depth-first, left-to-right manner.
• Semantic Actions:
– S-attributed: Actions are placed at the rightmost position.
– L-attributed: Actions can be placed anywhere within the production.
• Sibling Dependency:
– S-attributed: No sibling dependency.
– L-attributed: Can inherit values from left siblings only.

62

You might also like