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

Compiler Final PDF

The document discusses calculating first and follow functions for various context-free grammars. It also discusses removing left recursion from a grammar, constructing LL(1) and LR(0) parsing tables, defining bottom-up parsing, and benefits of bottom-up parsing. Finally, it discusses constructing DFA and SLR(1) parsing tables for grammars.

Uploaded by

Abu Ansari
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)
129 views

Compiler Final PDF

The document discusses calculating first and follow functions for various context-free grammars. It also discusses removing left recursion from a grammar, constructing LL(1) and LR(0) parsing tables, defining bottom-up parsing, and benefits of bottom-up parsing. Finally, it discusses constructing DFA and SLR(1) parsing tables for grammars.

Uploaded by

Abu Ansari
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/ 12

Compiler Design (CSE-323)

1. Calculate the first and follow functions for the given grammar

S → aBDh

B → cC

C → bC / ∈

D → EF

E→g/∈

F→f/∈

Solution

The first and follow functions are as follows-

Grammar First Functions Follow Functions

S → aBDh {a} {$}

B → cC {c} {g,f,h}

C → bC / ∈ {b,∈} {g,f,h}

D → EF {g,f,∈} {h}

E→g/∈ {g,∈} {f,h}

F→f/∈ {f,∈} {h}

1
2. Calculate the first and follow functions for the given grammar

E→E+T/T
T→TxF/F
F → (E) / id

Solution

We have-

The given grammar is left recursive.

So, we first remove left recursion from the given grammar.

After eliminating left recursion, we get the following grammar-

E → TE’

E’ → + TE’ / ∈

T → FT’

T’ → x FT’ / ∈

F → (E) / id

Now, the first and follow functions are as follows-

Grammar First Functions Follow Functions

E → TE’ { ( , id } {$,)}

E’ → + TE’ / ∈ {+,∈} {$,)}

T → FT’ { ( , id } {+,$,)}

T’ → x FT’ / ∈ {x,∈} {+,$,)}

F → (E) / id { ( , id } {x,+,$,)}

2
3. Calculate the first and follow functions for the given grammar

S → ACB / CbB / Ba

A → da / BC

B→g/∈

C→h/∈

Solution

The first and follow functions are as follows

Grammar First Functions Follow Functions

S → ACB / CbB / Ba {d,g,h,∈,b,a} {$}

A → da / BC {d,g,h,∈} {h,g,$}

B→g/∈ {g,∈} {$,a,h,g}

C→h/∈ {h,∈} {g,$,b,h}

3
4. Create a LL(1) parser table for the given grammar

E → TE’

E’ → + TE’ / ∈

T → FT’

T’ → x FT’ / ∈

F → (E) / id

Solution
To create LL(1) first we determine First and Follow functions which is as follow:

Grammar First Functions Follow Functions

E → TE’ { ( , id } {$,)}

E ’ → + TE’ / ∈ {+,∈} {$,)}

T → FT’ { ( , id } {+,$,)}

T ’ → x FT’ / ∈ {x,∈} {+,$,)}

F → (E) / id { ( , id } {x,+,$,)}

LL(1) parser table:

Non Input Symbol


Terminal

id + x ( ) $

E E → TE ’ E → TE ’

E ‘ E ’ → + TE ’ E’→ ∈ E’→ ∈

T T → FT ’ T → FT ’

T ’ T’→ ∈ T ’ → x FT ’ T’→ ∈ T’→ ∈

F F → id F → (E)

4
5. Define Bottom up or Shift reduced parser

Bottom-up parsing starts from the leaf nodes of a tree and works in upward

direction till it reaches the root node. Here, we start from a sentence and then

apply production rules in reverse manner in order to reach the start symbol.

6. Write down the classification of Bottom up parser

7. Write down the benefits of Bottom up parser

Benefits of Bottom up parser

a. Many programming languages using some variations of an LR parser. It should


be noted that C++ and Perl are exceptions to it.

b. LR Parser can be implemented very efficiently.

c. Of all the Parsers that scan their symbols from left to right, LR Parsers
detect syntactic errors, as soon as possible.

5
8. Consider the following grammar for LR(0) parser

S → aABe

A → Abc | b

B→d

Now draw the following:

a) Bottom up parser where input string is ‘abbcde’

Solution:

The Given grammar is:

S → aABe

A → Abc | b

B→d

Input String is: ‘abbcde’

The bottom up parser is given below:

6
9. Consider the following grammar for LR(0) parser

S → AA

A → aA | b

Now draw the following:

b) DFA diagram

c) LR(0) parser table

Solution:
Add Augment Production and insert '•' symbol at the first position for every

production in G

S` → •S
S → •AA
A → •aA
A → •b

Drawing DFA

The DFA contains the 7 states I0 to I6.

7
LR(0) Table
o If a state is going to some other state on a terminal then it correspond to a shift
move.
o If a state is going to some other state on a variable then it correspond to go to
move.
o If a state contain the final item in the particular row then write the reduce node
completely.

Explanation:

o I0 on S is going to I1 so write it as 1.
o I0 on A is going to I2 so write it as 2.
o I2 on A is going to I5 so write it as 5.
o I3 on A is going to I6 so write it as 6.
o I0, I2and I3on a are going to I3 so write it as S3 which means that shift 3.
o I0, I2 and I3 on b are going to I4 so write it as S4 which means that shift 4.
o I4, I5 and I6 all states contains the final item because they contain • in the right
most end. So rate the production as production number.
Productions are numbered as follows:
S → AA ... (1)
A → aA ... (2)
A → b ... (3)

I1 contains the final item which drives(S` → S•), so action {I1, $} = Accept.
I4 contains the final item which drives A → b• and that production corresponds to the
production number 3 so write it as r3 in the entire row.
I5 contains the final item which drives S → AA• and that production corresponds to
the production number 1 so write it as r1 in the entire row.
I6 contains the final item which drives A → aA• and that production corresponds to
the production number 2 so write it as r2 in the entire row.

Note: Explanation is used for better understanding it’s not mandatory

8
10. Consider the following grammar for SLR(1) parser

S→E

E→E+T|T

T→T*F|F

F → id

Now draw the following:

d) DFA diagram

e) SLR(1) parser table

Solution:

Add Augment Production and insert '•' symbol at the first position for every
production in G

S` → •E
E → •E + T
E → •T
T → •T * F
T → •F
F → •id

Drawing DFA

The DFA contains the 9 states I0 to I8.

9
SLR(1) Table

Explanation:

Follow (E) = {+, $}


Follow (T) = {*, +, $}
Follow (F) = {*, +, $}

o I1 contains the final item which drives S → E• and follow (S) = {$}, so action {I1, $}
= Accept
o I2 contains the final item which drives E → T• and follow (E) = {+, $}, so action {I2,
+} = R2, action {I2, $} = R2
o I3 contains the final item which drives T → F• and follow (T) = {+, *, $}, so action
{I3, +} = R4, action {I3, *} = R4, action {I3, $} = R4
o I4 contains the final item which drives F → id• and follow (F) = {+, *, $}, so action
{I4, +} = R5, action {I4, *} = R5, action {I4, $} = R5
o I7 contains the final item which drives E → E + T• and follow (E) = {+, $}, so action
{I7, +} = R1, action {I7, $} = R1
o I8 contains the final item which drives T → T * F• and follow (T) = {+, *, $}, so
action {I8, +} = R3, action {I8, *} = R3, action {I8, $} = R3.

Note: Explanation is used for better understanding it’s not mandatory

10
11. Consider the following grammar

E → E + E | E x E | id

a) Construct Operator Precedence Parser.


b) Find the Operator Precedence Functions.

Solution

The terminal symbols in the grammar are { + , x , id , $ }

We construct the operator precedence table as-

g→

id + x $

id > > >

f↓ + < > < >

x < > > >

$ < < <

Operator Precedence Table

11
The graph representing the precedence functions is-

Here, the longest paths are-

fid → gx → f+ → g+ → f$
gid → fx → gx → f+ → g+ → f$

The resulting precedence functions are-

+ x id $

f 2 4 4 0

g 1 3 5 0

12

You might also like