0% found this document useful (0 votes)
7 views25 pages

cd-unit-3-detailed-notes

The document provides detailed notes on Syntax Directed Translation (SDT) in Compiler Design, explaining its principles, implementation, and examples. It covers the use of semantic rules, parse trees, syntax trees, and various forms of intermediate code such as three-address code, quadruples, and triples. Additionally, it discusses the importance of intermediate code in compiler design and provides examples of arithmetic operations and their corresponding representations.

Uploaded by

st809845
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)
7 views25 pages

cd-unit-3-detailed-notes

The document provides detailed notes on Syntax Directed Translation (SDT) in Compiler Design, explaining its principles, implementation, and examples. It covers the use of semantic rules, parse trees, syntax trees, and various forms of intermediate code such as three-address code, quadruples, and triples. Additionally, it discusses the importance of intermediate code in compiler design and provides examples of arithmetic operations and their corresponding representations.

Uploaded by

st809845
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
You are on page 1/ 25

lOMoARcPSD|50704817

CD Unit 3 - detailed notes

Compiler Design (Dr. A.P.J. Abdul Kalam Technical University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Shivani Tyagi ([email protected])
lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
UNIT 3
SYNTAX DIRECTED TRANSLATION
I. SYNTAX DIRECTED TRANSLATION

o In syntax directed translation, along with the grammar we associate some informal
notations and these notations are called as semantic rules.
So we can say that:

Grammar + semantic rule = SDT (syntax directed translation)

o In syntax directed translation, every non-terminal can get one or more than one
attribute or sometimes 0 attribute depending on the type of the attribute. The value of
these attributes is evaluated by the semantic rules associated with the production rule.
o In the semantic rule, attribute is VAL and an attribute may hold anything like a string,
a number, a memory location and a complex record
o In Syntax directed translation, whenever a construct encounters in the programming
language then it is translated according to the semantic rules define in that particular
programming language.
o The general approach to SDT is to construct a parse tree or a syntax tree and compute
the values of attributes at the nodes of the tree by visiting them in some order. In
many cases translation can be done during parsing without building an explicit tree.

EXAMPLE:

Production Semantic Rules


E→E+T E.val := E.val + T.val
E→T E.val := T.val
T→T*F T.val := T.val + F.val
T→F T.val := F.val
F → (F) F.val := F.val
F → num F.val := num.lexval

E.val is one of the attributes of E.

num.lexval is the attribute returned by the lexical analyzer.

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
II. SYNTAX DIRECTED TRANSLATION SCHEME

o The Syntax directed translation scheme is a context -free grammar.


o The syntax directed translation scheme is used to evaluate the order of semantic rules.
o In translation scheme, the semantic rules are embedded within the right side of the
productions.
o The position at which an action is to be executed is shown by enclosed between
braces. It is written within the right side of the production.

Production Semantic Rules


S→E$ { printE.VAL }
E→E+E {E.VAL := E.VAL + E.VAL }
E→E*E {E.VAL := E.VAL * E.VAL }
E → (E) {E.VAL := E.VAL }
E→I {E.VAL := I.VAL }
I → I digit {I.VAL := 10 * I.VAL + LEXVAL }
I → digit { I.VAL:= LEXVAL}

III. IMPLEMENTATION OF SYNTAX DIRECTED TRANSLATION

o Syntax direct translation is implemented by constructing a parse tree and performing


the actions in a left to right depth first order.
o SDT is implementing by parse the input and produce a parse tree as a result.

EXAMPLE:
1. Construct a Parse Tree for the input 2+3*4 by using the grammar and its
corresponding semantic rules given below:

Production Semantic Rules


E→E+T {E.VAL := E.VAL + T.VAL }
E→T {E.VAL := T.VAL }
T → T* F {T.VAL := T.VAL * F.VAL }
T→F {T.VAL := F.VAL }
F → num {F.VAL := num.LEXVAL }

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
Step(i): Parse Tree corresponding to the grammar

Step(ii): Tree Traversal using semantic rules

IV. DESK CALCULATOR

o Desk Calculator is a simple calculator to find out the arithmetic operations.


o Desk Calculator is an example of Syntax Directed Translation (SDT).

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
EXAMPLE: Implementation of Desk Calculator for Addition & Multiplication using input
string 23*5+4.

Production Semantic Rules

S→E$ { printE.VAL }

E→E+E {E.VAL := E.VAL + E.VAL }

E→E*E {E.VAL := E.VAL * E.VAL }

E → (E) {E.VAL := E.VAL }

E→I {E.VAL := I.VAL }

I → I digit {I.VAL := 10 * I.VAL + LEXVAL }

I → digit { I.VAL:= LEXVAL}

Tree Traversal for the grammar & semantic rules corresponding to the grammar:

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
V. INTERMEDIATE CODE

Intermediate code is used to translate the source code into the machine code.
Intermediate code lies between the high-level language and the machine language.

Fig: Position of intermediate code generator

o If the compiler directly translates source code into the machine code without generating
intermediate code, then a full native compiler is required for each new machine.
o The intermediate code keeps the analysis portion same for all the compilers that's why
it doesn't need a full compiler for every unique machine.
o Intermediate code generator receives input from its predecessor phase and semantic
analyzer phase. It takes input in the form of an annotated syntax tree.
o Using the intermediate code, the second phase of the compiler synthesis phase is
changed according to the target machine.
o It simplifies the task of generation of optimal target code.
o It involves using at most one operation or one test in each statement.
o It introduces symbols to stand for temporary quantities.
o Intermediate representation:
Intermediate code can be represented in two ways:
(i) High Level intermediate code:
High level intermediate code can be represented as source code. To enhance
performance of source code, we can easily apply code modification. But to
optimize the target machine, it is less preferred.
(ii) Low Level intermediate code
Low level intermediate code is close to the target machine, which makes it suitable
for register and memory allocation etc. it is used for machine-dependent
optimizations.

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
o Four kinds of intermediate codes are:
(i) Postfix Notation
(ii) Syntax Tree
(iii) Quadruples
(iv) Triples

VI. POSTFIX NOTATION

o Postfix notation is the useful form of intermediate code if the given language is
expressions.
o Postfix notation is also called as 'suffix notation' and 'reverse polish'.
o Postfix notation is a linear representation of a syntax tree.
o In the postfix notation, any expression can be written unambiguously without
parentheses.
o The ordinary (infix) way of writing the sum of x and y is with operator in the middle:
x * y. But in the postfix notation, we place the operator at the right end as xy *.
o In postfix notation, the operator follows the operand.

VII. PARSE TREE AND SYNTAX TREE

o When you create a parse tree then it contains more details than actually needed. So, it
is very difficult to compiler to parse the parse tree. Take the following parse tree as an
example:

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
o In the parse tree, most of the leaf nodes are single child to their parent nodes.
o In the syntax tree, we can eliminate this extra information.
o Syntax tree is a variant of parse tree. In the syntax tree, interior nodes are operators
and leaves are operands.
o Syntax tree is usually used when represent a program in a tree structure.

A sentence id + id * id would have the following syntax tree:

VIII. THREE ADDRESS CODE


o Three-address code is an intermediate code. It is used by the optimizing compilers.
o In three-address code, the given expression is broken down into several separate
instructions. These instructions can easily translate into assembly language.
o Each Three address code instruction has at most three operands. It is a combination of
assignment and a binary operator.
o The three address code can be represented in three forms: quadruples, triples and
indirect triples.
o The commonly used representations for implementing Three Address Code are-

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
EXAMPLE:

1. Write a Three Address Code for the given expression:


a := (-c * b) + (-c * d)

Solution:
t1 := -c

t2 := b*t1

t3 := -c

t4 := d * t3

t5 := t2 + t4

a := t5

t is used as registers in the target program.

2. Write Three Address Code for the following expression-

-(a x b) + (c + d) – (a + b + c + d)

Solution:

Three Address Code for the given expression is-

(1) T1 = a x b

(2) T2 = uminus T1

(3) T3 = c + d

(4) T4 = T2 + T3

(5) T5 = a + b

(6) T6 = T3 + T5

(7) T7 = T4 – T6

3. Write Three Address Code for the following expression-

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
If A < B then 1 else 0

Solution:

Three Address Code for the given expression is-

(1) If (A < B) goto (4)

(2) T1 = 0

(3) goto (5)

(4) T1 = 1

(5) Stop

4. Write Three Address Code for the following expression-

If A < B and C < D then t = 1 else t = 0

Solution:

Three Address Code for the given expression is-

(1) If (A < B) goto (3)

(2) goto (4)

(3) If (C < D) goto (6)

(4) t = 0

(5) goto (7)

(6) t = 1

(7) Stop

IX. QUADRUPLES
The quadruples have four fields to implement the three address code. The field of
quadruples contains the name of the operator, the first source operand, the second
source operand and the result respectively.

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------

EXAMPLE:

1. Write a Three Address Code for the given expression and construct a
quadruples table corresponding to that three address code.

a := -b * c + d

Solution:

Three Address Code:


t1 := -b
t2 := c + d
t3 := t1 * t2
a := t3
These statements are represented by quadruples as follows:

Operator Source 1 Source 2 Destination

(0) uminus b - t1

(1) + c d t2

(2) * t1 t2 t3

(3) := t3 - a

X. TRIPLES

The triples have three fields to implement the three address code. The field of triples
contains the name of the operator, the first source operand and the second source
operand.

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------

In triples, the results of respective sub-expressions are denoted by the position of


expression. Triple is equivalent to DAG while representing expressions.

EXAMPLES:

1. Write a Three Address Code for the given expression and construct a quadruples
table corresponding to that three address code.

a := -b * c + d
Solution:
Three address code is as follows:
t1 := -b
t2 := c + d
t3 := t1 * t2
a := t3
These statements are represented by triples as follows:

Operator Source 1 Source 2

(0) minus b -

(1) + c d

(2) * (0) (1)

(3) := (2) -

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
XI. INDIRECT TRIPLES
o This representation is an enhancement over triples representation.
o It uses an additional instruction array to list the pointers to the triples in the desired
order.
o Thus, instead of position, pointers are used to store the results.
o It allows the optimizers to easily re-position the sub-expression for producing the
optimized code.

PRACTICE PROBLEMS BASED ON QUADRUPLES, TRIPLES &


INDIRECT TRIPLES-
1. Translate the following expression to quadruple, triple and indirect triple-
a+bxc/e↑f+bxc
Solution:
Three Address Code for the given expression is-
(1) T1 = e ↑ f
(2) T2 = b x c
(3) T3 = T2 / T1
(4) T4 = b x a
(5) T5 = a + T3
(6) T6 = T5 + T4
Now, we write the required representations-

Quadruple Representation-

Location Op Arg1 Arg2 Result

(0) ↑ e f T1

(1) x b c T2

(2) / T2 T1 T3

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------

(3) x b a T4

(4) + a T3 T5

(5) + T5 T4 T6

Triple Representation-

Location Op Arg1 Arg2

(0) ↑ e f

(1) x b c

(2) / (1) (0)

(3) x b a

(4) + a (2)

(5) + (4) (3)

Indirect Triple Representation-

Statement

35 (0)

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------

36 (1)

37 (2)

38 (3)

39 (4)

40 (5)

Location Op Arg1 Arg2

(0) ↑ e f

(1) x b e

(2) / (1) (0)

(3) x b a

(4) + a (2)

(5) + (4) (3)

2. Translate the following expression to quadruple, triple and indirect triple-


a=bx–c+bx–c
Solution:
Three Address Code for the given expression is-

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------

(1) T1 = uminus c
(2) T2 = b x T1
(3) T3 = uminus c
(4) T4 = b x T3
(5) T5 = T2 + T4
(6) a = T5
Now, we write the required representations-

Quadruple Representation-

Location Op Arg1 Arg2 Result

(1) uminus c T1

(2) x b T1 T2

(3) uminus c T3

(4) x b T3 T4

(5) + T2 T4 T5

(6) = T5 a

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
Triple Representation-

Location Op Arg1 Arg2

(1) uminus c

(2) x b (1)

(3) uminus c

(4) x b (3)

(5) + (2) (4)

(6) = a (5)

Indirect Triple Representation-

Statement

35 (1)

36 (2)

37 (3)

38 (4)

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------

39 (5)

40 (6)

Location Op Arg1 Arg2

(1) uminus c

(2) x b (1)

(3) uminus c

(4) x b (3)

(5) + (2) (4)

(6) = a (5)

XII. TRANSLATION OF ASSIGNMENT STATEMENTS

In the syntax directed translation, assignment statement is mainly deals with


expressions. The expression can be of type real, integer, array and records.
EXAMPLE:
Consider the grammar
S → id := E
E → E1 + E2
E → E1 * E2
E → (E1)
E → id

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
The translation scheme of above grammar is given below:

Production rule Semantic actions

S → id :=E {p = look_up(id.name);
If p ≠ nil then
Emit(p = E.place)
Else
Error;
}

E → E1 + E2 {E.place = newtemp();
Emit(E.place = E1.place '+' E2.place)
}

E → E1 * E2 {E.place = newtemp();
Emit (E.place = E1.place '*' E2.place)
}

E → (E1) {E.place = E1.place}

E → id {p = look_up(id.name);
If p ≠ nil then
Emit (p = E.place)
Else
Error;
}

o The p returns the entry for id.name in the symbol table.


o The Emit function is used for appending the three address code to the output file.
Otherwise it will report an error.
o The newtemp() is a function used to generate new temporary variables.
o E.place holds the value of E.

XIII. TRANSLATION OF BOOLEAN EXPRESSIONS

Boolean expressions have two primary purposes. They are used for computing the
logical values. They are also used as conditional expression using if-then-else or while-
do.

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
EXAMPLE:
1. Consider the grammar
E → E OR E
E → E AND E
E → NOT E
E → (E)
E → id relop id
E → TRUE
E → FALSE
The relop is denoted by <, >, <, >.
The AND and OR are left associated. NOT has the higher precedence then AND
and lastly OR.

Production rule Semantic actions

E → E1 OR E2 {E.place = newtemp();
Emit (E.place ':=' E1.place 'OR' E2.place)
}

E → E1 + E2 {E.place = newtemp();
Emit (E.place ':=' E1.place 'AND' E2.place)
}

E → NOT E1 {E.place = newtemp();


Emit (E.place ':=' 'NOT' E1.place)
}

E → (E1) {E.place = E1.place}

E → id relop id2 {E.place = newtemp();


Emit ('if' id1.place relop.op id2.place 'goto'
nextstar + 3);
EMIT (E.place ':=' '0')
EMIT ('goto' nextstat + 2)
EMIT (E.place ':=' '1')
}

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------

E → TRUE {E.place := newtemp();


Emit (E.place ':=' '1')
}

E → FALSE {E.place := newtemp();


Emit (E.place ':=' '0')
}

o The EMIT function is used to generate the three address code and the newtemp( )
function is used to generate the temporary variables.
o The E → id relop id2 contains the next_state and it gives the index of next three
address statements in the output sequence.
o Here is the example which generates the three address code using the above translation
scheme:
p>q AND r<s OR u>r
100: if p>q goto 103
101: t1:=0
102: goto 104
103: t1:=1
104: if r>s goto 107
105: t2:=0
106: goto 108
107: t2:=1
108: if u>v goto 111
109: t3:=0
110: goto 112
111: t3:= 1
112: t4:= t1 AND t2
113: t5:= t4 OR t3

XIV. STATEMENTS THAT ALTER THE FLOW CONTROL


The goto statement alters the flow of control. If we implement goto statements then we
need to define a LABEL for a statement. A production can be added for this purpose:
S→ LABEL : S
LABEL → id

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
In this production system, semantic action is attached to record the LABEL and its
value in the symbol table.
Following grammar used to incorporate structure flow-of-control constructs:
S → if E then S
S → if E then S else S
S→ while E do S
S→ begin L end
S→ A
L→ L;S
L→ S
Here, S is a statement, L is a statement-list, A is an assignment statement and E is a
Boolean-valued expression.

Translation scheme for statement that alters flow of control

o We introduce the marker non-terminal M as in case of grammar for Boolean


expression.
o This M is put before statement in both if then else. In case of while-do, we need to
put M before E as we need to come back to it after executing S.
o In case of if-then-else, if we evaluate E to be true, first S will be executed.
o After this we should ensure that instead of second S, the code after the if-then else
will be executed. Then we place another non-terminal marker N after first S.
The grammar is as follows:
S → if E then M S
S→ if E then M S else M S
S→ while M E do M S
S→ begin L end
S→ A
L→ L;MS
L→ S
M→ ∈
N→ ∈

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
The translation scheme for this grammar is as follows:

Production rule Semantic actions

S → if E then M S1 BACKPATCH (E.TRUE, M.QUAD)


S.NEXT = MERGE (E.FALSE, S1.NEXT)

S → if E then M1 S1 else BACKPATCH (E.TRUE, M1.QUAD)


M2 S2 BACKPATCH (E.FALSE, M2.QUAD)
S.NEXT = MERGE (S1.NEXT, N.NEXT, S2.NEXT)

S → while M1 E do M2 S1 BACKPATCH (S1,NEXT, M1.QUAD)


BACKPATCH (E.TRUE, M2.QUAD)
S.NEXT = E.FALSE
GEN (goto M1.QUAD)

S → begin L end S.NEXT = L.NEXT

S→A S.NEXT = MAKELIST ()

L→L;MS BACKPATHCH (L1.NEXT, M.QUAD)


L.NEXT = S.NEXT

L→S L.NEXT = S.NEXT

M→∈ M.QUAD = NEXTQUAD

N→ ∈ N.NEXT = MAKELIST (NEXTQUAD)


GEN (goto_)

XV. TRANSLATION OF AN ARRAY


Elements of arrays can be accessed quickly if the elements are stored in a block of
consecutive location. Array can be one dimensional or two dimensional.
For one dimensional array:
A: array[low..high] of the ith elements is at:
base + (i-low)*width → i*width + (base - low*width)

Multi-dimensional arrays:
Row major or column major forms
Row major: a[1,1], a[1,2], a[1,3], a[2,1], a[2,2], a[2,3]
Column major: a[1,1], a[2,1], a[1, 2], a[2, 2],a[1, 3],a[2,3]
-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
In row major form, the address of a[i1, i2] is
Base+((i1-low1)*(high2-low2+1)+i2-low2)*width

Translation scheme for array elements:

Limit(array, j) returns nj=highj-lowj+1


place: the temporaryor variables

S → L := E
E → E+E
E → (E)
E → L
L → Elist ]
L → id
Elist → Elist, E
Elist → id[E

A suitable transition scheme for array elements would be:

Production Rule Semantic Action

S → L := E {if L.offset = null then emit(L.place ':=' E.place)


else EMIT (L.place'['L.offset ']' ':=' E.place);
}

E → E+E {E.place := newtemp;


EMIT (E.place ':=' E1.place '+' E2.place);
}

E → (E) {E.place := E1.place;}

E→L {if L.offset = null then E.place = L.place


else {E.place = newtemp;
EMIT (E.place ':=' L.place '[' L.offset ']');
}
}

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])


lOMoARcPSD|50704817

Subject: Compiler Design Prachi Jain


Subject Code: KCS 502 Assistant Professor (CSE Dept.)
------------------------------------------------------------------------------------------------------------------------------------------
L → Elist ] {L.place = newtemp; L.offset = newtemp;
EMIT (L.place ':=' c(Elist.array));
EMIT (L.offset ':=' Elist.place '*' width(Elist.array);
}

L → id {L.place = lookup(id.name);
L.offset = null;
}

Elist → Elist, E {t := newtemp;


m := Elist1.ndim + 1;
EMIT (t ':=' Elist1.place '*' limit(Elist1.array, m));
EMIT (t, ':=' t '+' E.place);
Elist.array = Elist1.array;
Elist.place := t;
Elist.ndim := m;
}

Elist → id[E {Elist.array := lookup(id.name);


Elist.place := E.place
Elist.ndim := 1;
}

Where:
o ndim denotes the number of dimensions.
o limit(array, i) function returns the upper limit along with the dimension of array
o width(array) returns the number of byte for one element of array.

-------------------------------------------------------------------------------------------------------------------------------------
I. T. S. Engineering College, Greater Noida

Downloaded by Shivani Tyagi ([email protected])

You might also like