Compiler Final PDF
Compiler Final PDF
1. Calculate the first and follow functions for the given grammar
S → aBDh
B → cC
C → bC / ∈
D → EF
E→g/∈
F→f/∈
Solution
B → cC {c} {g,f,h}
C → bC / ∈ {b,∈} {g,f,h}
D → EF {g,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-
E → TE’
E’ → + TE’ / ∈
T → FT’
T’ → x FT’ / ∈
F → (E) / id
E → TE’ { ( , id } {$,)}
T → FT’ { ( , id } {+,$,)}
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
A → da / BC {d,g,h,∈} {h,g,$}
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:
E → TE’ { ( , id } {$,)}
T → FT’ { ( , id } {+,$,)}
F → (E) / id { ( , id } {x,+,$,)}
id + x ( ) $
E E → TE ’ E → TE ’
E ‘ E ’ → + TE ’ E’→ ∈ E’→ ∈
T T → FT ’ T → FT ’
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.
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
Solution:
S → aABe
A → Abc | b
B→d
6
9. Consider the following grammar for LR(0) parser
S → AA
A → aA | b
b) DFA diagram
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
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.
8
10. Consider the following grammar for SLR(1) parser
S→E
E→E+T|T
T→T*F|F
F → id
d) DFA diagram
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
9
SLR(1) Table
Explanation:
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.
10
11. Consider the following grammar
E → E + E | E x E | id
Solution
g→
id + x $
11
The graph representing the precedence functions is-
fid → gx → f+ → g+ → f$
gid → fx → gx → f+ → g+ → f$
+ x id $
f 2 4 4 0
g 1 3 5 0
12