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

NPTEL Online Certification Courses Indian Institute of Technology Kharagpur

The document is an assignment for a Compiler Design course from the Indian Institute of Technology Kharagpur, consisting of 11 multiple-choice questions related to LR parsing techniques and grammar analysis. It includes detailed solutions for each question, explaining concepts such as augmented grammar, closure computation, reduce-reduce conflicts, and the comparison of different LR parser types. The assignment aims to assess students' understanding of parsing algorithms and their applications in compiler design.

Uploaded by

rp20129421
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 views6 pages

NPTEL Online Certification Courses Indian Institute of Technology Kharagpur

The document is an assignment for a Compiler Design course from the Indian Institute of Technology Kharagpur, consisting of 11 multiple-choice questions related to LR parsing techniques and grammar analysis. It includes detailed solutions for each question, explaining concepts such as augmented grammar, closure computation, reduce-reduce conflicts, and the comparison of different LR parser types. The assignment aims to assess students' understanding of parsing algorithms and their applications in compiler design.

Uploaded by

rp20129421
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/ 6

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:

Step 1: Compute Augmented Grammar

We first augment the grammar by adding a new start symbol:

S' → S
S → CC
C → cC | d

Step 2: Compute Closure of State 0

The LR(1) parsing algorithm starts with the closure of the initial state containing the
augmented production:

[S' → .S, $]

Since S → CC, we expand:

[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):

• In S → CC, Follow(C) comes from Follow(S), which includes $.


• But we are only interested in the lookahead from S → CC, meaning First(C) = {c,
d}.
Thus, for C → .cC and C → .d, the lookahead set is {c, d}.

Step 3: Identify the Correct Answer

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:

Step 1: Compute Augmented Grammar

We augment the grammar by adding a new start symbol:

S' → S
S → CC
C → cC | d

Step 2: Compute Closure of State 0

The LR(1) parser starts with:

[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.

The Follow(C) in S → CC is First(C), which is {c, d}.

Thus, when expanding C, we get:

C → .cC, {c, d}
C → .d, {c, d}

Step 3: Identify the Correct Answer

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)

Solution: We are given the items in state I:

1. A → α. (A is completed, meaning it can be reduced)


2. B → δ. (B is also completed, meaning it can be reduced)

Additionally, we know:

• First(A) contains 'a'


• Follow(A) contains 'a'
• Follow(B) contains 'a'

Step 1: Understanding Reduce-Reduce Conflict

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.

This results in a reduce-reduce conflict.

Step 2: Evaluating the Options

• Option a) "Shift-reduce conflict" ❌


o A shift-reduce conflict occurs when a shift action and a reduce action are
both possible, but here we only have two reductions.
• Option b) "Reduce-reduce conflict" ✅
o Since both A → α and B → δ are completed and both have 'a' in their follow
sets, this leads to a reduce-reduce conflict.
• Option c) "Both shift-reduce and reduce-reduce conflicts" ❌
o There is no shift action in this scenario, so a shift-reduce conflict does not
exist.
• Option d) "No conflicts" ❌
o There is a conflict (reduce-reduce), so this is incorrect.
5. Which of the following statements is true regarding LR parsers?

a) SLR and Canonical LR have the same number of states.


b) LALR and Canonical LR have the same number of states.
c) SLR and LALR have the same number of states.
d) All three have the same number of states.
Ans: c)
Explanation:
• SLR and LALR have the same number of states, but LALR distinguishes states
using lookaheads to reduce conflicts.

• Canonical LR has more states than both because it does not merge states with
the same core items.

6. Ans: b)

Solution: Different LR parsing techniques handle reductions differently:

1. SLR (Simple LR):


o Uses Follow(A) to determine reduce actions.
o Requires the Follow set.
2. Canonical LR (LR(1)):
o Uses lookahead symbols explicitly attached to each item.
o Does NOT require the Follow set because the lookaheads are computed
directly from the grammar.
3. LALR (Look-Ahead LR):
o Similar to Canonical LR, but merges states.
o Also uses lookahead symbols rather than the Follow set.
o Does NOT require the Follow set.

7. Ans: a)
Solution:

Step 1: Constructing Items in State 0

State 0 starts with the closure of the augmented grammar:

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:

• The ε-production (E → .) means that E can be reduced to ε whenever Follow(E)


contains 'a' or 'b'.
• Follow(E) in SLR parsing is computed using the grammar:

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:

Step 1: Constructing Items in State 0

The initial items in state 0 include:

S → .B
S → .SabS
B → .bB
B→.

Now, let's analyze the shift and reduce possibilities.

Shift Possibility:

• The item S → .SabS allows shifting 'a'.


• The item B → .bB allows shifting 'b'.

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, $ }.

Step 2: Identifying the Conflict

For 'b':

• The parser can shift using B → .bB.


• The parser does not reduce immediately because B → bB takes precedence.

For 'a':

• The parser can reduce using B → ε (since 'a' is in Follow(B)).


• The parser can shift using S → .SabS.

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.

• LALR(1) is an improvement over SLR(1) but still merges states, making it


weaker than LR(1).

11. Ans: a)

END of Assignment

You might also like