0% found this document useful (0 votes)
60 views

CD Unit3

This document discusses syntax directed translation, which is a framework for generating intermediate code from a source program. It attaches semantic actions to productions in a context-free grammar to define translations. Translations can be synthesized from right-hand sides or inherited from left-hand sides. An example shows generating postfix code for a desk calculator by associating actions with productions to compute values at parse tree nodes. Postfix notation, evaluation using a stack, and control flow are also described.

Uploaded by

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

CD Unit3

This document discusses syntax directed translation, which is a framework for generating intermediate code from a source program. It attaches semantic actions to productions in a context-free grammar to define translations. Translations can be synthesized from right-hand sides or inherited from left-hand sides. An example shows generating postfix code for a desk calculator by associating actions with productions to compute values at parse tree nodes. Postfix notation, evaluation using a stack, and control flow are also described.

Uploaded by

gnana jothi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

UNIT 3

SYNTAX DIRECTED TRANSLATION


Syntax directed translation scheme:
 It is a framework for Intermediate code generation
 Extension of CFG
 It allows subroutines or semantic actions to be attached to the productions of CFG
 These subroutines generates intermediate code
 Intermediate code may be in Postfix form, syntax tree, quadruples. Triples.
7.1 Syntax directed translation schemes
 Syntax directed translation scheme is a context free grammar in which a program fragment called
an output action is associated with each production
 The value associated with the grammar symbol is called translation
 We should denote the translation fields of the grammar symbol X as X.VAL, X.TRUE etc.
 When the symbol appears more than one in production, then subscripts are used to distinguish
between them. E(1) +E(2)
 Eg. EE(1)+E(2) {E.VAL:=E(1).VAL+E(2).VAL}
| |
Production semantic action (is enclosed in parenthesis, appears after production)
In this eg, the semantic action is a formula, which states that the translation E.VAL on the left side of
the production is determined by adding the values of E on the right side.
2 types of translation
 Synthesized translation
 Inherited translation
Synthesized translation
The translation of nonterminal on the left side of the production is defined in terms of translations of
nonterminals on the right side.
Inherited translation
The translation of nonterminal on the right side of the production is defined in terms of translations
of nonterminals on the left side.
Translations on the parse tree
Production Semantic action
(1) (2)
EE +E {E.VAL:=E(1).VAL+E(2).VAL}
Edigit {E.VAL:=digit}
For eg, there is a input string 1+2+3. Parse tree for the string.
Complete parse tree with computed translations
E E.VAL=6
E

E E.VAL=3 E E.VAL=3
E E +
+

3 3
E E E E.VAL=1 + E E.VAL=2
+

1 2 1 2
7.2 Implementation of Syntax directed translators
A syntax directed translation scheme provides a method for describing input output mapping
To calculate the translation at node A associated with the production AXYZ, we need to know the values
of the translations at X, Y and Z.
These nodes X, Y and Z will become the children of node A after reduction.
Top is a pointer associated with the top of the stack
Before XYZ is reduced to A, the value of translation of Z is in VAL[TOP]
The value of Y is in VAL[TOP+1]
The value of Z is in VAL[TOP+2]
After reduction, top is incremented by 2 and the value of A.VAL appears in VAL[TOP]
Stack before reduction
State val

Z Z.VAL
Y Y.VAL
X X.VAL

Implementation of desk calculator using syntax directed translation


 The desk calculator evaluates arithmetic expressions involving integer operands and
integer operators + and *.
 The input expression is terminated by $ sign
 The output is the numerical value of arithmetic expression
Input : 23*5+4$
Output : 119
First write a grammar for describing inputs
The productions are :
SE$
EE+E
EE*E
E(E)
EI
II digit
Idigit
Nonterminals( S,E,I)
Terminals( $, +,*, Paranthesis, digit)
Now associate semantic actions to productions E.VAL, I.VAL
Associate the translation LEXVAL to the terminal digit
Syntax directed translation scheme for desk calculator
Production Semantic action
SE$ {print E.VAL}
EE(1)+E(2) { E.VAL:=E(1).VAL+E(2).VAL}
EE(1)*E(2) { E.VAL:=E(1).VAL*E(2).VAL}
E(E(1)) { E.VAL:=E(1).VAL}
EI { E.VAL:=I.VAL}
II(1)digit { I.VAL:=10*I(1).VAL+LEXVAL}
Idigit { I.VAL:= LEXVAL}
Implementation of desktop calculator
Production Semantic action
SE$ print VAL[TOP]
EE(1)+E(2) VAL[TOP]:= VAL[TOP]+ VAL[TOP-2]
EE(1)*E(2) VAL[TOP]:= VAL[TOP]* VAL[TOP-2]
E(E(1)) VAL[TOP]:= VAL[TOP – 1]
EI none
II(1)digit VAL[TOP]:= 10* VAL[TOP]+ LEXVAL
Idigit VAL[TOP]:=LEXVAL

Sequence of moves made by the parser on the input 23*5+4$


Input State val productions used
(1) 23*5+4$ - -
(2) 3*5+4$ 2 -
(3) 3*5+4$ I 2 Idigit
(4) *5+4$ I3 2-
(5) *5+4$ I (23) IIdigit
(6) *5+4$ E (23) EI
(7) 5+4$ E* (23)-
(8) +4$ E*5 (23)--
(9) +4$ E*I (23)-5 Idigit
(10) +4$ E*E (23)-5 EI
(11) +4$ E (115) EE*E
(12) 4$ E+ (115)-
(13) $ E+4 (115)--
(14) $ E+I (115)-4 Idigit
(15) $ E+E (115)-4 EI
(16) $ E (119) EE+E
(17) - E$ (119)-
(18) - $ ---- SE$
7.3 Intermediate code
Source code is translated into language whose complexity level is in between high level language and
machine language.
That language is called intermediate code or intermediate text.
7.4 Postfix notation
Infix expression : The operator is placed in between the operands. (ordinary way of writing expns)
Prefix expression : The operator is placed before the operands
Postfix expression:
 The operator is placed after the operands
 The operator is placed at the right end eg : ab+
 If e1 and e2 are any two postfix expressions, and ϴ is an binary operator, then the postfix notation
is e1 e2 ϴ
 Parenthesis is not needed in postfix notation.
Egs:
a+b  ab+
a*b+c  ab*c+
a*(b+c)  abc+*
(a+b)*c ab+c*
a+b*c  abc*+
(a+b)*(c+d)  ab+cd+*
3ary operator(ternary operator)—conditional expression
If e then x else y (if e≠0, value is x, if e=0, value is y)
Using ternary postfix operator?, the expn is exy?
Represent in postfix form
if a then if c-d then a+c else a*c else a+b
if a then if c-d then a+c else a*c else a+b - : a truepart elsepart ?
a+b ab+, a+cac+, a*cac*, c-dcd-

if c-d then a+c else a*c  cd-ac+ac*?


if a then if c-d then a+c else a*c else a+b  a cd-ac+ac* ab+ ?
Evaluation of Postfix expression:
Postfix expression can be evaluated using stack
Scan the postfix expression from left to right
Push each operand onto the stack
If an operator is encountered, , the top 2 values on the stack are popped
The operands are evaluated with the operator.
The result is pushed back to the stack
Eg: ab+c*
a=1, b=3 and c=5
evaluate 13+5*
stack 1
stack 3
when the operator + is encountered, pop top 2 operands, 3 and 1 , add and then push 4 to the stack
stack 5
when the operator * is encountered, pop top 2 operands, 5 and 4 , multiply and then push 20 to the stack
The value on the top of the stack is the value of expression
Control flow in postfix code
Conditional and unconditional jumps and labels can be inserted into post fix code
Operands are represented by pointers to the symbol table and operators are represented by integer codes.
Unconditional jump : jump
Conditional jump : jlt (jumped less than), jeqz (jump if equal to 0)
Postfix expression
l jump Transfer to label l (goto l)
e1 e2 l jlt jump to label l if the postfix expression e1 has a value less than the post fix expression e2.
(if e1 < e2 go to l)
e l jeqz  jump to label l if the postfix expression e has a value equal to 0
(if e = 0 go to l)

If e then x else y  e l1 jeqz x l2 jump


l1:y l2: ;

if a then if c-d then a+c else a*c else a+b


if a=0, evaluate a+b
if a=1, evaluate ( if c-d =1 evaluate a+c otherwise evaluate a*c)
a l1 jeqz cd- l2 jeqz ac+ l3 jump
l2: ac* l3 jump
l1: ab+ l3: ;

syntax directed translation to postfix code:


The following eg shows the syntax directed translation scheme for infix –postfix translation
Production Semantic action
EE(1) op E(2) E.CODE := E(1).CODE || E(2).CODE || OP

E(E(1)) E.CODE:=E(1).CODE
Eid E.CODE = id
The value of the translation E.CODE is the concatenation of 2 translations E (1).CODE and E(2).CODE
The translation of parenthesized expression is same as that of the un-parenthesized expression
The translation of any identifier is identifier itself
The following eg shows the implementation of infix-postfix translation
Production Program fragment
EE(1) op E(2) {print op}
E(E(1)) {}
Eid { print id}
Evaluate a+b*c using syntax directed infix –postfix translation
1. Shift a
2. Reduce by Eid , print a
3. Shift +
4. Shift b
5. Reduce by Eid , print b
6. Shift *
7. Shift c
8. Reduce by Eid , print c
9. Reduce by EE op E , print *
10. Reduce by EE op E , print +
7.5 Parse trees and syntax trees

Parset ree contain redundant information. It has to be eliminated.


Syntax tree is a variant of parse tree.
Syntax tree is called as abstract tree
Each leaf represents an operand and each interior node represents an operator

Syntax tree a*(b+c)/d syntax tree if a=b then a:=c+d else b:=c-d

/
If then else

* d :=
=
:=

a + -
a b + b
a
b c c d
c d

Syntax directed construction of syntax tree


A Parse tree or a syntax tree can be represented using Syntax directed translation schemes
Syntax directed translation schemes to construct syntax tree
Production Semantic action
1. EE(1) op E(2) {E.VAL := NODE(OP,E(1).VAL, E(2).VAL}
2. E(E(1)) {E.VAL:=E(1).VAL}
3. E- E(1) {E.VAL:=UNARY(-,E(1).VAL)}
4. Eid {E.VAL = LEAF(ID)}

1. The function NODE takes 3 arguments. NODE(op, left,right)


op is the name of the operator, left and right are pointers to roots of subtrees.
This function creates a new mode labelled by op and makes 2nd and 3rd argument , the left and right
children of the new mode and it returns a pointer to the created node
2. The function UNARY(op, CHILD) creates a new mode labelled op and makes CHILD as it's child.
It also returns a pointer to the created node.
3. The function LEAD(ID) creates a new node labelled by ID and returns a pointer to the node.
7.6 Three address code, Quadruples, Triples
Three address code
 It is a form of Intermediate code
 3 address code is a sequence of statements of the form A:=B op C where A,B,C are programmer
defined names or constants or compiler generated temporary names.
 op is any fixed or floating point operator, or a logical operator or a Boolean valued data.
 It is called as 3 address code because, Each statement contains 3 addresses
 (2 for the operands and 1 for the result)
 Only one operator per statement is allowed
The expn. X+Y*Z can be written as,
T1:=Y*Z
T2:=X+T1 where T1 and T2 are compiler generated temporary names
Common types of 3 address statements:
1. Assignment statement of the form A:=B op C, op may be arithmetic or logical operator
2. Assignment statement of the form A:=op B, op may be unary minus or shift operations
3. Unconditional jump goto L
4. Conditional jump if A relop B goto L
5. Param A and call P,n These instructions are used to implement a procedure call
When the procedure P(A1,A2,A3,…An) is called, a sequence of following 3 addr stats
are generated
Param A1
Param A2
Param A3
.
.
Param An
Call P,n
6. Indexed Assignment statements of the form A:=B[I], A[I]:=B
7. Address and Pointer assignment statements of the form A:= addr B, A=*B, *A=B

Quadruples:
 Representation of 3 address statements using 4 fields is called quadruples
 We have 4 fields - op, arg1, arg2, result

 A 3 address statement A:=B op C puts B in arg1, C in arg2, and A in result


 Statement with unary operators, A:=-Bor A:=B do not use arg2.
 Statements like param neither use arg2 nor use result
 Conditional jumps and unconditional jumps put the target label in result
An assignment statement A= -B*(C+D) is translated into 3 address statements
T1: -B
T2:=C+D
T3:=T1*T2
A:=T3

Quadruple representation:
op Arg1 Arg2 Result
(0) uminus B -- T1
(1) + C D T2
(2) * T1 T2 T3
(3) := T3 -- A

Here the contents of arg1,arg2,result are pointers to the symbol table entries
Temporary names should also be entered in the symbol table.

Triples:
Types
Direct triples
Indirect triples

Representation of three address code using 3 fields is called triples


3 fields are op, arg1 and arg2
where arg1 and arg2 and op are pointers to the symbol table or pointers to the structure itself.

The method of entering temporary names in the symbol table is avoided in this method.
(Temporary names are avoided)
The contents are either pointers to the symbol table or pointers to the structure itself.
Since three fields are used, the intermediate representation is called triples.
3 address code implemented in triple form
Direct triples
op Arg1 Arg2
(0 uminus B --
)
(1 + C D
)
(2 * (0) (1)
)
(3 := A (2)
)

Indirect triples
Indirect triples have a separate listing of pointers to triples.
An array statement is used to list pointers to triples in desired order

statement
(0) 14
(1) 15
(2) 16
(3) 17
op Arg Arg2
1
1 - B ----
4
1 + C D
5
1 * (14) (15)
6
In triples notation, if a 1 := A (16) stmt containing temporary vales are moved, then we
have to change all the 7 pointers to that statement in arg1, arg2 arrays.
In indirect triples, if a statement is moved, we simply reorder the statement list.
7.7 Translation of Assignment statements
Assignment statement with integer types :
Consider simple assignment statement involving integer quantities.
The following grammar is a form of assignment statement.
Aid:=E
EE+E/ E*E / -E /(E) id
Translation of E
It is a structure with 2 fields
E.PLACEused to store the value of expression
E.CODE sequence of 3 address statements
Translation of A
A.CODE Three address code to execute the assignment stmt.
Translation of id
id.PLACE denote the name corresponding to the instance of the token id
Newtemp() is a function used to create new temporary names T1, T2 etc.
Abstract translation schemes:
Production Semantic action
Aid:=E {A.CODE:=E.CODE ||id.PLACE || ‘:=’ || E.PLACE}
EE(1)+E(2) {T:=NEWTEP();
E.PLACE:=T;
E.CODE:=E(1).CODE || E(2).CODE ||E.PLACE ||
‘:=’ || E(1).PLACE || ‘+’ || E(2).PLACE }
EE(1)*E(2) {T:=NEWTEP();
E.PLACE:=T;
E.CODE:=E(1).CODE || E(2).CODE ||E.PLACE ||
‘:=’ || E(1).PLACE || ‘*’ || E(2).PLACE }

E - E(1) {T:=NEWTEP();
E.PLACE:=T;
E.CODE:=E(1).CODE || E.PLACE ||
‘:=’ || E(1).PLACE }
E ( E(1)) { E.PLACE:= E(1).PLACE;
E.CODE:=E(1).CODE }

E id { E.PLACE:=id. .PLACE;
E.CODE:=null }

Assignment statement with mixed mode types :


In general, all the identifiers may not be of the same type.
There are many different type of constants and variables
Consider the grammar for the same assignment statements, but assume that there are 2 modes.
Real and integer with integers converted to reals whenever needed.
In the translation, an additional field is used E.MODE whose value is either real or integer
E.MODE= real / integer
The semantic rule associated with E.MODE for the production EE+E
EE+E {if E(1).MODE = INTEGER and E(2).MODE = INTEGER
then E.MODE = INTEGER
else E.MODE = REAL}
Three address statements of the form A:=inttoreal B is used to convert the integer value to real
Eg:
X:=Y+I*J
Assume that X and Y are REAL and I and J are INTEGER
The output would be :
T1:=I int* J
T2:= Inttoreal T1
T3:=Y real+ T2
X:=T1.

Semantic rule for EE(1) op E(2)


This rule uses 2 translation fields E.PLACE and E.MODE for the nonterminal E

T:=NEWTEMP()
if E(1).MODE = INTEGER and E(2).MODE = INTEGER then
begin
GEN(T:=E(1).PLACE int op E(2).PLACE);
E.MODE = INTEGER
end
else if E(1).MODE = REAL and E(2).MODE = REAL then
begin
GEN(T:=E(1).PLACE real op E(2).PLACE);
E.MODE = REAL
end
else if E(1).MODE = INTEGER /* and E(2).MODE = REAL*/ then
begin
U:=NEWTEMP();
GEN(U:= Inttoreal E(1).PLACE);
GEN(T:= U real op E(2).PLACE);
E.MODE = REAL
end
else /* E(1).MODE = REAL and E(2).MODE = INTEGER */
begin
U:=NEWTEMP();
GEN(U:= Inttoreal E(2).PLACE);
GEN(T:= E(1).PLACE real op U);
E.MODE = REAL
end;
E.PLACE :=T

7.8 Boolean expressions


Boolean expressions are composed of Boolean operators (and, or, not) and Boolean variables or
relational expressions. The relational expressions are of the form E1 relop E2 where E1, E2 are
arithmetic expressions.
Consider the grammar:

E E or E | E and E | not E | (E) | id | id relop id

Or, and  left associative, not  right associative


Order of precedence: not –>highest precedence, then and , then or has the lowest precedence
Methods of translating Boolean expressions
There are 2 methods of representing the value of Boolean expression
 In the first method, true and false are encoded numerically to evaluate a Boolean expression
1 is used to denote true and 0 is used to denote false
 In the second method, the Boolean expression is implemented by flow of control. ie the value of
Boolean expression is represented by a position reached in the program. This method is used in if
then and while do statements.

Numerical representation;
The translation of A or B and C is the following 3 address sequence
T1:=B and C
T2:= A or T1

A relational expression A<B is equivalent to the conditional statement if A<B then 1 else 0.

The stmt. if A<B then 1 else 0 can be translated in to 3 address sequence


(1) if A<B goto (4)
(2) T:=0
(3) Goto(5)
(4) T:=1

Now the Boolean expression, A<B or C can be translated in to 3 address sequence


(1) if A<B goto (4)
(2) T:=0
(3) Goto(5)
(4) T:=1
(5) T2:=t1 or C

The semantic rule for the 2 productions, EE or E and E id relop id are as follows

Production Semantic rule


EE(1) or E(2) { T:=NEWTEMP( );
E.PLACE:=T;
GEN(T:=E(1).PLACE or E(2).PLACE}

Eid(1) relop id(2) {T:=NEWTEMP( );


E. PLACE := T;
GEN(if id(1).PLACE relop id(2).PLACE
Goto NEXTQUAD +3);
GEN(T:=0)
GEN(goto NEXTQUAD+2);
GEN(T:=1)}
In these rules, we assume that the quadruples are being generated.
NEXTQUAD indicates the next available entry in the quadruple array.

Control flow representation


If we evaluate expression by program position, we can avoid evaluating the entire expression.
Given A or B, if A is evaluated as true then we can conclude that the entire expression is true, without
evaluating B. The semantic definition of the programming language, determines whether all parts of the
Boolean expression must be evaluated
Consider an expression of the form E(1) or E(2)
If E(1) is true, the we immediately know the expn E itself is true. We can make E(1) as true and E as true.
If E(1) is false, then we must make E(1) as false and evaluate E(2)
If E(2) is true, then the expn E is true. We can make E(2) as true and E as true.
Otherwise make E(2) as false and E as false
Consider the translation of Boolean expression
There are 2 kinds of exits with the Boolean expression
True exit to the statement TRUE
False exit to the statement FALSE
Eg:
If E then S(1) else S(2)
The figure shows how the control flows in If statement.

Code for
E

T he boolean expression E has jump out of it to the first statement


of Code S(1) if E is true. It jumps to the first statement of S(2) if E
is TRUE : for S(1) false

Code
FALSE : for S(2)

While E do S

Code for E

The boolean expression E has jump out of it when E is false.


( TRUE : to a place after the while stmt.)
Code for S
When E is true, then it jumps to the first statement of S.
goto

FALSE:
Consider a syntax directed translation scheme that generates quadruples for Boolean expression.
Since bottom up parsing is used, we may not have generated actual quadruples to which the jump has to be
made.(at the time of generating jump statements). So the target of the branching statements are temporarily
left unspecified. Each such quadruple will be filled in when the proper location is found. The subsequent
filling in of quadrupled is called backpatching.
To manipulate the list of quadrupled 3 functions are used.
MAKELIST(i): Creates a new list containing only i. It returns a pointer to the list that it has created.
MERGE(p1,p2): Takes the lists p1 and p2 and concatenate them and returns a pointer to the concatenated
list
BACKPATCH(p,i): takes the quadruple pointed by p, and make i as it's target

There are 2 translations E.TRUE, E.FALSE


They are pointers to already generated quadruples, that must be filled in with the target location.
We can replace E E and E by 2 productions
EEAND E
EAND E and

create a marker nonterminal, m with the production Mℇ


the translation for this is {M.QUA:= NEXTQUAD}
the revised grammar is
(1) E E(1) or M E (2)
(2) E(1) and M E(2)
(3) not E(1)
(4) (E(1))
(5) id
(6) id(1) relop id(2)
(7)Mℇ
Syntax directed translation scheme
1) EE(1) or M E(2)
{ BACKPATCH(E(1). FALSE,M.QUAD);
E.TRUE:=MERGE(E(1).TRUE, E(2).TRUE);
E.FALSE := E(2)FALSE }

2) EE(1) and M E(2)


{ BACKPATCH(E(1). TRUE ,M.QUAD);
E.TRUE:= E(2).TRUE ;
E.FALSE := MERGE(E(1).FALSE, E(2).FALSE) }

3) E not E(1)
{ E.TRUE E(1).FALSE;
E.FALSE E(1).TRUE}

4) E( E(1))
{ E.TRUE E(1).TRUE;
E.FALSE E(1).FALSE}

5) Eid
{E.TRUE:=MAKELIST(NEXTQUAD);
E.FALSE:=MAKELIST(NEXTQUAD+1);
GEN(if id.PLACE goto-);
GEN(goto -)}

6) Eid(1) relop id(2)


{E.TRUE:=MAKELIST(NEXTQUAD);
E.FALSE:=MAKELIST(NEXTQUAD+1);
GEN(if id(1) .PLACE relop id(2) .PLACE goto-);
GEN(goto -)}

7) Mℇ
{M.QUAD:=NEXTQUAD}

Semantic actions 5 and 6 have generated 2 quadruples a conditional goto and unconditional goto
They don’t have their targets filled
The index of first generated quadruple is made into a list and E.TRUE is given a pointer.
The index of the second generated quadruple goto-, is also made into a list and E.FALSE is given a pointer
Fig : Parse tree for P<Q or R<S and T<U

Eg:
Consider the expression P<Q or R<S and T<U
Consider the semantic actions on the numbered nodes as bottom up parsing is done.

Corresponding to node1, there are 2 quadruples


100: if P<Q goto –
101: goto –

Then node 2 records the value of NEXTQUAD 102

Node 3 generates 2 quadruples


102: if R<S goto 104
103: goto -

Then node 4 records the value of NEXTQUAD 104

Node 6 corresponds to the reduction EE(1) and M E(2)

The corresponding semantic routine calls BACKPATCH[(102),104]


This Backpatch fills 104 in quadruple 102.

Six quadruples generated so far are:


100: if P<Q goto –
101: goto –
102: if R<S goto 104
103: goto -
104: if T<U goto –
105: goto -

Node 7 corresponds to the reductionE(1) or M E(2)


The corresponding semantic routine calls BACKPATCH[(101),102]
This Backpatch fills 102 in quadruple 101.

100: if P<Q goto –


101: goto 102
102: if R<S goto 104
103: goto -
104: if T<U goto –
105: goto -

From the fig,


the entire expression is true if and only if the gotos of quadruples 100 or 104 are reached
the entire expression is false if and only if the gotos of quadruples 103 or 105 are reached

The targets of these statements will be filled later in compilation

7.10 Postfix translations

Postfix translations can be implemented by emitting the tail as each production is recognized.

To achieve postfix form, the productions are factorized.


Factorizing is
Take a production of the form AX1X2……Xn
Introduce a new nonterminal B
Replace A X1X2……Xn by pair of productions
AB Xi+1X2+2……Xn and B X1X2……Xi
A marker nonterminal M is used.
The purpose is to record the translation M.QUAD as NEXTQUAD at a critical time.
Eg: Factoring for while loop
Consider Swhile M(1)E do M(2)S
Rewrite the production as
SC S
CW E do
Wwhile
C and W are nonterminals
A syntax directed translation scheme is:
Wwhile {W.QUAD:=NEXTQUAD}
CW E do {C.QUAD:=W.QUAD;
BACKPATCH(E.TRUE,NEXTQUAD);
C.FALSE=E.FALSE}
SC S(1) {BACKPATCH S(1).NEXT.C.QUAD);
S.NEXT:=C.FALSE;
GEN(goto C.QUAD) }

Factoring for for loop:


For L := E(1) step E(2) to E(3) do S
Where L is the index, E(1) , E(2) are E(3) expressions for initial , increment and final value
Eg: for I:= 1 step 1 until N do A[I]:=0
Semantics of for loop
begin
INDEX:=addr(L);
*INDEX:=E(1);
INCR:=E(2);
LIMIT:=E(3);
While *INDEX<= LIMIT do
begin
code for statement S;
*INDEX:=*INDEX + INCR
end
end
factor the for statement into productions
(1) F for L
(2) TF:= E(1) step E(2) to E(3) do
(3) ST S(1)

A syntax directed translation scheme is:


(1) F for L
{F.INDEX:=L.INDEX}
(2) TF:= E(1) step E(2) to E(3) do
{ GEN(*F.INDEX:=E(1).PLACE);
INCR:=NEWTEMP( );
LIMIT := NEWTEMP( );
GEN(INCR :=E(2). PLACE);
GEN(LIMIT :=E(3). PLACE);
T.QUAD := NEXTQUAD;
T.NEXT := MAKELIST (NEXTQUAD);
GEN(if *F.INDEX> LIMIT goto -);
T.INDEX :=F.INDEX;
T.INCR:=INCR
}

(3) ST S(1)


{BACKPATCH(S(1).NEXT,NEXTQUAD);
GEN(*T.INDEX:=*T.INDEX + T.INCR);
GEN(goto T.QUAD);
S.NEXT := T.NEXT}

You might also like