You are on page 1of 26

6

Compilers
Solutions of Examples for Practice

Example 6.2.3
Solution :

Input processing

Output

a : = b + c*4

Lexical analyzer
id1 : = id2 + id3 * id4

Token stream

Syntax analyzer
:=
*

id1

Syntax tree

+
id2

id4
id3

Semantic analyzer
:=
*

id1

+
id2

int to float
id3

Semantic tree

id4

Intermediate code generator


a
b
c

t1 : = int to float (4)


t2 : = id3
t3 : = id2 + t2

Intermediate code

t4 : = t3 * t1
id1 : = t4
Code optimizer
t1 : = id3
t2 : = id2 + t1
t3 : = t2 * 4.0
id1 : = t3

Optimized code

Code generator
MOVF id3, R2
MOVF id2, R1
ADDF R1, R2
MULF # 4.0, R2
MOVF R2, id1
TM

Machine code

TECHNICAL PUBLICATIONS
(6 - -1)An up thrust for knowledge

SPCC

6-2

Compilers

Example 6.2.4
Solution : Process of compilation - Refer section 6.2.
Input processing

Output

a = (b+c) * (b+c) * 2
Token stream

Lexical analyzer
id1 :=(id2+id3) * (id4+id5) * 2
Syntax analyzer
:=
*

id1

+
id2

Syntax tree

id3 +
id4

2
id5

Semantic analyzer
:=

+
id2

Semantic tree

id1

id3 +
id4

inttofloat
id5

Intermediate code generator


t1 := inttofloat (2)
t2 := id4 + id5
t3 := t2* t1
t4 := id2 + id3

Intermediate
code

t5 := t3 * t4
id1 := t5
Code optimizer
t1 := id2 + id3
t2 := t1* 2.0

Optimized
code

t3 := t1* t2
id1:= t3
Code generator
MOVF
ADDF
MOVF
MULF
MULF
MOVF

id2,R2
id3, R2
R2, R1
#2.0, R2
R2, R1
R1, id1

Machine
code

qqq
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

Lexical Analysis

Solution of Selected Example for Practice


Example 7.7.5
Solution :

Let us assume

R1 = (a + b) *
R2 = a
Then given equation R = R1 R2 R1
The NFA for R1 is
e
a
e

e
e

b
e

The NFA for R2 is


e

Now the combined NFA can be drawn from above given figures.
e

q0

q2

q1
e

q4

e
a

q3

e
q6

q5

q7

q8 a

q9

q10

e
q11
e

q12

q14

q13

e
q16

q15

q17

qqq
TM

TECHNICAL PUBLICATIONS
(7 - -1)An up thrust for knowledge

Syntax Analysis

Solutions of Examples for Practice


Example 8.7.10
Solution : As the given grammar needs to be left factored. Also the ambiguity of this
grammar needs to be removed. The modified grammar will be as follows.

Eliminating ambiguity
E E or T
E T
T T and F
T F
F not G
F G
G ( E)|0|1

Eliminating Left Recursion


E TE
E or TE |e
T FT
T and FT |e
F not G
FG
G ( E|0|1
)
The FIRST and FOLLOW for above grammar is FIRST (E) = {not, (,0,1}
FIRST (E) = {or, e}
FIRST (T) = {not, (, 0, 1}
FIRST (T) = {and, e}
FIRST (F) = {not, (, 0, 1}
(8 - 1)
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8-2

Syntax Analysis

FIRST (G) = {(, 0, 1}


FOLLOW( E) = {), $ }

{ ), $ }
FOLLOW( T ) = {or, ), $}
FOLLOW( T ) = {or, ), $}
FOLLOW( F ) = {and, or, ), $}
FOLLOW( G ) = {or, and, ), $}
FOLLOW( E ) =

The predictive parsing table will be


or

and

not
E->TE

E
E'

(
E->TE

E'->orTE

E->TE

E->TE

E->e

T'->andFT

T->FT
T ->e

T'->e
F->not G

F
G

E->e
T->FT

T->FT

T->FT

T
T'

F->G

F->G

F->G

G->(E)

G->0

G->1

Example 8.7.11
Solution : In the above grammar the rule for production R is left-recursive. And for the
third rule i.e. for non-terminal Q there is a common prefix. Hence we have to left factor
the grammar. Then the grammar becomes
Now let us compute FIRST and FOLLOW
P Ra | Qba
R aba

caba

Q bbc

bc

P Ra | Qba
R aba R' | caba R'
R' bcR' | e

Rbc

Left-factoring

Q bQ'
Q' bc | c

Fig. 8.1

FIRST (P) = {a, b, c}


FIRST (Q) = {b}
FIRST (Q ) = {b, c}
FIRST (R) = {a, c}
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8-3

Syntax Analysis

FIRST (R) = {b, e}


FOLLOW (P) = {$}
FOLLOW (R) = {a}
FOLLOW (R) = {a}
FOLLOW (Q) = {b}
FOLLOW (Q ) = {b}
Now we can build LL(1) parsing table.
a
P

P Ra

R abaR'

R'

R' e

b
P Qba

P Ra
R cabaR'

R' b c R'

Q bQ'

Q'

Q' bc

Q' c

Let us derive the cababca using above built LL(1) parsing table.
State

Input

Actions

$P

cababca$

P Ra

$aR

cababca$

R cabaR'

$aR' aba/c

/cababca$

$aR' ab/a

a/ babca$

$aR' a b/

b/ abca$

$aR' a/

a/ bca$

$aR'

b ca$

$aR' c b/

b/ ca$

$aR' /c

/c a$

$aR'

a$

$ a/

a/ $
$

R' b c R'

R' e

Success

Thus the given string is parsed successfully.

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8-4

Syntax Analysis

Example 8.7.12
Solution :

The FIRST and FOLLOW sets for the non-terminal symbols are as follows :

FIRST (S) = {a, e, c, d, f} = {a, c, d, e, f}


FIRST (P) = {a, e, f, e}
FIRST (Q) = {c, d, e}
FIRST (R) = {e, f}
FOLLOW (S) = {$}
FOLLOW (P) = {c, d, e, f}
FOLLOW (Q) = {e, f}
FOLLOW (R) = {b, $}
Example 8.7.13
Solution : a) To eliminate left recursion we need to modify the grammar. Using following
rule we can eliminate left recursion.
A Aa
A b

A bA
A aA
A e

In the given grammar, only the rule for E is a left recursive. Hence we will eliminate the
left recursion from this rule. The remaining rules for X and T will remain as it is.
E T E
E + T E | e
T id |id [ ] | id [X]
X E, E | E
b) To perform left factoring we will use following rule.
A a b1|a b2

A 2A
A b1|b2

The rules for non-terminal X and T are the only rules which need left factoring to be
performed.
E T E
E + T E | e
T id T
T [T|e
T ]|X]
X EX
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8-5

Syntax Analysis

X , E|e
c) FIRST (X) = FIRST (E) = FIRST (T) = {id}
FIRST (E) = {+, e}

FIRST (T") = { ] , id}

FIRST (T) = {[, e}


FOLLOW (E) = FOLLOW (E) = { $, , , ] }
FOLLOW (T) = FOLLOW (T) = {$, + , ] }
FOLLOW (X) = FOLLOW (X) = {$, ] }
FOLLOW (T") = {+}
Example 8.7.14
Solution : We will first build the FIRST and FOLLOW sets for given grammar.

FIRST (A) = {(, 0}

These non-terminal have some


appearing at first position at R.H.S.

FIRST (B) = {, , e}

These terminals = FIRST.

FIRST (S) = {(, 0}

terminal

symbols

FOLLOW is a set of terminals which are appearing immediately right to corresponding


non-terminal symbol.
FOLLOW (S) = {$, , ,) } $ because S is a start symbol.
FOLOW (A) = { ) }
FOLLOW (B) = { ) }
Let us built the LL(1) parsing table.
0

S 0

S (A)

A SB

A SB

B e

B , SB

The grammar rules are added at the terminals symbol columns which are obtained in the
FIRST set of corresponding non-terminal symbol. If there is e in FIRST set then select
terminal symbols from Follow set of corresponding non-terminal. Add the e rule in the
column of these selected terminals.
As there is only one rule per slot of above LL(1) parsing table, the grammar is LL(1). We
can also prove it by successfully parsing one valid string (0, (0, 0)) using above generated
LL(1) parsing table.

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8-6

Syntax Analysis

Stack

Inupt

Action

$S

( 0 ,(0 , 0 ) ) $

S (A)

$)A(

(0,(0,0))$

( Popped

$)A

0,(0,0))$

A SB

$)BS

0 , ( 0 ,0 ) ) $

S 0

$)B0

0,(0,0))$

0 is popped off

$)B

,(0,0))$

B , SB

$ ) B S,

,(0,0))$

, is popped off

$)BS

(0,0))$

S (A)

$)B)A(

(0, 0))$

) is popped off

$)B) A

0,0))$

A SB

$)B)BS

0,0))$

S 0

$)B)B0

0,0))$

0 is popped off

$)B)B

,0))$

B ,SB

$)B)BS,

,0))$

, is popped

$)B)BS

0))$

S 0

$)B)B0

0))$

0 is popped

$)B)B

))$

B e

$)B)

))$

) is popped

$)B

)$

B e

$)

)$

) is popped

Success

Thus we can parse the valid string successfully.


Example 8.10.8
Solution : We will first build a canonical set of items. Hence a collection of LR(0) items is
as given below.
I0 :

S S

I5 :

S L = R

goto(I0, id)
L id

S R

I6 :
TM

goto(I2, =)

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8-7

Syntax Analysis

L * R

S L = R

L id

R L

R L

L * R

I1 : goto (I0, S)

L id

S S

I7 :

goto (I4, R)

I2 : goto (I0, L)

L * R

S L = R
R L

I8 :

goto (I4, L)
R L

I3 : goto(I0, R)

S R
I4 : goto(I0, *)
L * R

I9 :

goto(I6, R)

S L = R

R L
L * R
L id
Now we will build the parsing table for the above set of items.
State

Action
=

Goto

id

s4

s5

s4

s5

0
1
2

s6/r5

3
4
5
6
7
8
9

As we are getting multiple entries in Action [2, =] = s6 and r5. That means shift and
reduce both a shift/reduce conflict occurs on input symbol =. Hence given grammar is not
SLR(1) grammar.

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8-8

Syntax Analysis

Example 8.10.9
Solution : Let,
S AB | gDa
A ab|c
B dC
C gC|g
D fD|g
We will build the FIRST and FOLLOW components for given grammar.
FIRST (S) = {a, c, g}

FOLLOW (S) = {$}

FIRST (A) = {a, c}

FOLLOW (A) = {d}

FIRST (B) = {d}

FOLLOW (B) = { f }

FIRST (C) = {g}

FOLLOW (C) = { f }

FIRST (D) = {f,g}

FOLLOW (D) = { a }

The LL(1) parse table will be a

S AB

S AB

A ab

A c

S gDa

B dC

C gC
Cg

D FD

Since there are multiple entries in table [C, g] the given grammar is not LL(1).
Example 8.10.10
Solution : Let,
S iCtS | iCtSeS | a
C b
be the grammar which can be written using augmented grammar S S. Hence we will
construct canonical set of items LR(1) as I0 :

S S, $
S iCtS, $
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8-9

Syntax Analysis

S iCtSeS, $
S a, $
For rule S S the corresponding rule will be A a X b, a where a = e, X = S and b = e
and a = $ because its initializing rule.
As after dot S is coming the closure rules on S can be applied.
I1 :

goto (I0, S)
S S , $

I2 :

goto (I0, i)
S i CtS, $
S i CtSeS, $
C b, t

I3 :

goto (I0, a)
S a , $

I4 :

goto (I2, C)
S iC tS, $
S iC tSeS, $

I5 :

goto (I2, b)
C b , t

I6 :

goto (I4, t)
S iCt S, $
S iCt SeS, $
S iCtS, $|e
S iCtSeS, $|e
S a, $|e

I7 :

goto (I6, S)
S iCtS , $
S iCtS es, $

I8 :

goto (I6, i)
S i CtS, $|e
S i CtSeS, $|e
C b, t

I9 :

goto (I6, a)
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8 - 10

Syntax Analysis

S a , $|e
I10 :

goto (I7, e)
S iCtSe S, $
S iCtS, $
S iCtSeS, $
S a, $

I11 :

goto (I8, C)
S iC tS, $|e
S iC tSeS, $|e

I12 :

goto (I10, S)
S iCtSeS , $

I13 :

goto (I11, t)
S iCt S, $ | e
S iCt SeS, $ | e
S iCtS, e|$
S iCtSeS, e|$
S a, e|$

I14 :

goto (I13, S)
S iCtS , $ | e
S iCtS eS, $ | e

I15 :

goto (I14, e)
S iCtSe S, $ | e
S iCtS , $ | e
S iCtSeS , $ | e
S a , $ | e

I16 :

goto (I15, S)
S iCtSeS , $ | e

Now we can merge the states having common first component but with different second
component.
Hence merge 2 and 8,
I28 :

S i CtS , $ | e
S i CtSeS , $ | e
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8 - 11

Syntax Analysis

C b, t
Merge state 3 and 9
I39 :

S a , $ | e

Merge state 4 and 11


I411 :

S iC tS, $ | e
S iC tSeS, $ | e

Merge state 6 and 13


I613 :

S iCt S, $ | e
S iC SeS, $ | e
S iCtS, $ | e
S iCtSeS, $ | e
S a, $|e

Merge state 7 and 14


I714 :

S iCtS , $
S iCtS eS, $

Merge state 10 and 15


I1015 :

S iCtSe S, e|$
S iCtS, e|$
S iCtSeS, e|$
S a , $|e

Merge state 12 and 16


I1216 :

S iCtSeS, $|e

Now, for the obtained canonical set of items we can construct parsing table as State

Action
i

S28

S39

Goto
t

ACCEPT
S5

411

39
411

S
1

1
28

r3

r3

S613

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

8 - 12

5
613

r4
S28

S39

714

714
1015

Syntax Analysis

S28

1216

S1015

r1

r3

r3

r2

r2

1216

qqq

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

10

Intermediate Code Generation


Solutions of Selected Examples for Practice

Example 10.2.1
Solution : We will first build a syntax tree for the given expression i.e. A* (B + C).
*
A

Uminus
+
C

i) Postfix
If we travel the above tree in postorder traversal then we get the postfix notation i.e.
ABC + Uminus *
ii) Three-address code
The tree-address code can be written as t1 = B + C
t2 = t1
t3 = A * t 2
iii) Syntax tree
The syntax tree can be constructed with the help of syntax directed definition as given by
following rules Production

Semantic rules

E E1 + E 2

E ptr := mknode ('+', E1 ptr, E2 ptr)

E E1 * E2

E ptr := mknode ('*', E1 ptr, E2 ptr)

E E1

E ptr := mknode ('Uminus', E1 ptr)

E ( E 1)

E ptr := E1 ptr
TM

TECHNICAL PUBLICATIONS
An up thrust for knowledge
(10 -- 1)

SPCC

10 - 2

Intermediate Code Generation

In Fig. 10.1 (a) the syntax tree is build using the SDD and the nodes are allocated from an
array of records and index of each node serves as the pointer to the node which is as
shown in Fig. 10.1 (b).
*

id

Uminus

id

id

id

id

Uminus

id

(a)

(b)

Fig. 10.1

Example 10.2.2
Solution : (i) Quadruple
Number

Op

Arg1

Arg2

Result

(0)

t1

(1)

t2

(2)

t1

t2

t3

(3)

t4

(4)

t4

t5

(5)

t3

t5

t6

The three address code will be


t1 : = a + b
t2 : = c + d
t3 : = t1 * t2
t4 : = a + b
t5 : = t4 + c
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

10 - 3

Intermediate Code Generation

t6 : = t 3 + t 5
(ii) Triples
Number

Op

Arg1

Arg2

(0)

(1)

(2)

(0)

(1)

(3)

(4)

(3)

(5)

(2)

(4)

(iii) Indirect Triples


Number

Op

Arg1

Arg2

Statement

(11)

(0)

(11)

(12)

(1)

(12)

(13)

(11)

(12)

(2)

(13)

(14)

(3)

(14)

(15)

(14)

(4)

(15)

(16)

(13)

(15)

(5)

(16)

Example 10.6.4
Solution : The three address code will be if (ch ! = 1) goto Label 1
a( )
goto Last
Label 1 :

if (ch ! = 2) goto Label 2


b()
goto Last

Label 2 :

if (ch ! = 3) goto Last


c()

Last :

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

SPCC

10 - 4

Intermediate Code Generation

Example 10.6.5
Solution : We consider the values stored in 2-D array is in row major form. Assuming
4 byte allocation per word a [i] [j] will be at location [addr(a) + (10*i+j)*4]
a [i] [i] = addr (a) + 44 * i
\
The 3 address code then will be i=0
L1:

if i < 10 goto L2
t1 = 44*i
a[t1] = 1
t2 = i+1
i = t2
goto t1

L2 :
Example 10.6.6
Solution :
i) if a > b goto L1
goto L2
L1 :

t1 := inttoreal b
t2 := a real + t1
x := tw

L2 :

t1 := inttoreal b
t2 := a real t1
x := t2

ii)

a := 10
goto test
L1 : c := 1
goto next
L2 : c := 2
goto next
test : if a = 10 goto L1
if a = 20 goto L2
next :

qqq

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

11

Code Generation
Solutions of Selected Examples for Practice

Example 11.4.3
Solution : The code will be
MOV b, R0
ADD c, R0
MOV R0, a
Redundant load it can be removed
MOV a,R0
ADD e, R0
MOV R0, d
Then we get,
MOV b, R0
ADD c, R0
ADD e, R0
MOV R0,d
Example 11.4.4
Solution :

First of all we will write a three address code for given expression

t1 = a + b
t2 = d + e
t3 = c t2
t4 = t1 t3
The generated code using simple code generation algorithm will be i)

MOV d, R0
ADD e, R0
MOV C, R1
SUB R0, R1
MOV a, R0
TM

TECHNICAL PUBLICATIONS
An up thrust for knowledge
(11 -- 1)

SPCC

11 - 2

Code Generation

ADD b, R0
SUB R1, R0
ii)

MOV d, R0
ADD e, R0
MOV R0, t1
MOV c, R0
SUB t1, R0
MOV R0, t2
MOV a, R0
ADD b, R0
SUB t2, R0

Example 11.6.2
Solution : There are three address statements of following types
Case (i)

x : = y op z

Case (ii)

x : = op y

Case (iii)

x:= y

The DAG can be constructed as follows Step 1 : If y is undefined then create node (y) Similarly if z is undefined create a
node (z)
Step 2 : For the case (i) create a node (op) whose left child is node (y) and node (z) will
be right child. Also check for any common sub expressions. For case (ii) determine
whether a node is labeled op, such a node will have a child node (y). In case (iii) node n
will be node (y).
Step 3 : Delete x from list of identifiers for node (x). Append x to the list of attached
identifiers for node n found in 2.
The DAG for the basic block is as follows.
* t1, t3

[ ] t2

i0

* t1, t3

Fig. 11.1

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

i0

SPCC

11 - 3

Code Generation

Continuing in this fashion the DAG will be


+

t6,p

p0

[]

t5

t2

[ ] t4

<=

(1)

b
*

t1,t3

i0

t7,i

20

Fig. 11.2

qqq

TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

12

Code Optimization

Solutions of Examples for Practice


Example 12.3.1
Solution :
While optimizing the above given code some code may get modified and
some dead code will get eliminated.
L1

if(a==b) goto L13

L2

r1 = load (32)

L4

goto L14

L5

r1<<4

L6

if(m>n) goto L14

L6a

goto L10

L7

store(r2,r7)

L8

r5 = r5*20

L9

r4 = r4+r4

L10

if(m>n) goto L5

L11

r3=load(20,r9)

L13

r10=4*r10

L14

r11=r11/8

L15

goto L13

As on L15 line it is directed to goto


L13, we can directly state to jump to L13.
Similarly L3 and L4 both lines tell us to
jump at L14. Hence L3 is eliminated.

The line L12 simply directs to jump to


L13. Hence it is eliminated.

TM

TECHNICAL PUBLICATIONS
An up thrust for knowledge
(12 -- 1)

SPCC

12 - 2

Code Optimization

Example 12.4.1
Solution :
r4 = addr (A)
r5 = 0
R1 = r4 * 2
R2 = R1 << 2
R3 = r5 + 6

R1, R2, R3 are


new induction variable
placed outside the loop.

r1 = R1
r2 = load (r1 + 0)
r3 = R2
r6 = load (r3 + 8)
r7 = r2 + r6
r8 = R3
store (r8 + 0 , r7)
r4 = r4 + 1
r5 = r5 + 4
R1 = R1 + 2
R2 = R2 + 8
R2 = R3 + 4

Fig. 12.1

We can optimize the above code further more.


r4 = addr (A)
r5 = 0
R1 = r4 << 1
R2 = r4 << 3
R3 = 6

r4 = addr (A)
r5 = 0
R1 = r4 << 1
R2 = r4 << 3
R3 = 6

r2 = load (R1 + 0)
r6 = load (R2 + 8)
r7 = r2 + r6
store (R3 + 0, r7)
r4 = r4 + 1
r5 = r5 + 4
R1 = R1 + 2
R2 = R2 + 8
R3 = R3 + 4

r2 = load (R1 + 0)
r6 = load (R2 + 8)
r7 = r2 + r6
store (R3 + 0, r7)
R1 = R1 + 2
R2 = R2 + 8
R3 = R3 + 4

Dead code
it can
be eliminated

Fig. 12.2

Since R1, R2 and R3 cannot be eliminated because they have got different values
associated with them.

qqq
TM

TECHNICAL PUBLICATIONS - An up thrust for knowledge

You might also like