NPTEL Online Certification Courses Indian Institute of Technology Kharagpur
NPTEL Online Certification Courses Indian Institute of Technology Kharagpur
Compiler Design
Assignment- Week 6
TYPE OF QUESTION:MCQ
Number ofquestions:11 Total mark: 11 X 1 = 11
1.
Ans: c)
Solution:
S' → S
S → CC
C → cC | d
The LR(1) parsing algorithm starts with the closure of the initial state containing the
augmented production:
[S' → .S, $]
[S' → .S, $]
[S → .CC, $]
Now, since C appears immediately after the dot (.), we need to expand C. Looking at the
production C → cC | d, we add:
C → .cC, (lookahead: Follow(C))
C → .d, (lookahead: Follow(C))
To determine Follow(C):
From the closure computation, we see that the item C → .cC has lookahead {c, d},
meaning the correct item is:
C → .cC, c, d
2.
Ans: c)
Solution:
S' → S
S → CC
C → cC | d
[S' → .S, $]
Expanding S → CC gives:
[S' → .S, $]
[S → .CC, $]
Since C appears immediately after the dot (.), we expand C → cC | d and determine the
look ahead.
C → .cC, {c, d}
C → .d, {c, d}
From the closure computation, we see that the item C → .d has lookahead {c, d},
meaning the correct item is:
C → .d, c, d
3. Ans: c)
Solution:
4. Ans: b)
Additionally, we know:
A reduce-reduce conflict occurs when two or more reductions are possible in the same
parser state for the same input symbol.
Here, both A → α and B → δ are ready for reduction, and since Follow(A) and Follow(B)
both contain 'a', the parser will not be able to decide which reduction to apply when 'a'
appears in the input.
• Canonical LR has more states than both because it does not merge states with
the same core items.
6. Ans: b)
7. Ans: a)
Solution:
E' → .E
E → .aEbE
E → .bEaE
E→.
Since E → . (i.e., E → ε) is a completed item, it means that E can be reduced whenever the
lookahead belongs to Follow(E).
Shift Possibility:
• The items E → .aEbE and E → .bEaE indicate that we can shift 'a' and 'b'.
• The First(aEbE) = {a} and First(bEaE) = {b}, so both 'a' and 'b' can be shifted.
Reduce Possibility:
Follow(E') = { $ }
Follow(E) = { a, b, $ } (Since E appears in recursive positions)
Since 'a' and 'b' are in Follow(E), the parser will try to reduce E → ε whenever it
encounters 'a' or 'b'.
8. Ans: a)
Solution:
9. Ans: b)
Solution:
S → .B
S → .SabS
B → .bB
B→.
Shift Possibility:
Reduce Possibility:
• The item B → . (i.e., B → ε) means that B can be reduced whenever the lookahead
belongs to Follow(B).
• Follow(B) is computed using:
o S → B, so Follow(B) contains Follow(S).
o Follow(S) = { $, a, b } (since S appears at the start and within SabS).
Thus, Follow(B) = { a, b, $ }.
For 'b':
For 'a':
Since both shift (S → .SabS) and reduce (B → ε) actions are possible on 'a', a shift-reduce
conflict occurs for 'a'.
10. Which of the following parser types is the most powerful in terms of recognizing a
broader class of grammars?
a) LL(1)
b) SLR(1)
c) LALR(1)
d) LR(1)
Ans: d)
Explanation:
• LL(1) is the weakest among these as it only looks one symbol ahead and is top-
down.
• SLR(1) is more powerful than LL(1) but can still fail for some grammars.
11. Ans: a)
END of Assignment