Consider the following grammar \(𝐺\): \(π‘†β†’π΄βˆ£π΅ \\ π΄β†’π‘Žβˆ£π‘ \\ π΅β†’π‘βˆ£π‘\)…

2018

Consider the following grammar \(𝐺\):

\(π‘†β†’π΄βˆ£π΅ \\ π΄β†’π‘Žβˆ£π‘ \\ π΅β†’π‘βˆ£π‘\)

whereΒ \(\{𝑆,𝐴,𝐡\}\)Β is the set of non-terminals,Β \(\{π‘Ž,𝑏,𝑐\}\)Β is the set of terminals.

Which of the following statement(s) is/are correct?

\(𝑆_1\):Β \(𝐿𝑅(1)\)Β can parse all strings that are generated using grammarΒ \(𝐺\).

\(𝑆_2\):Β \(𝐿𝐿(1)\)Β can parse all strings that are generated using grammarΒ \(𝐺\).

Choose the correct answer from the code given below:

  1. A.

    OnlyΒ \(𝑆_1\)

  2. B.

    OnlyΒ \(𝑆_2\)

  3. C.

    BothΒ \(𝑆_1\)Β andΒ \(𝑆_2\)

  4. D.

    NeitherΒ \(𝑆_1\)Β norΒ \(𝑆_2\)

Attempted by 79 students.

Show answer & explanation

Correct answer: D

Answer: Neither statement is correct.

  • Grammar: S -> A | B ; A -> a | c ; B -> b | c

  • Why LL(1) fails: FIRST(A) = {a, c} and FIRST(B) = {b, c}. These sets overlap on c, so when the parser sees lookahead c it cannot decide whether to expand A or B. Therefore the grammar is not LL(1).

  • Why LR(1) fails: the grammar is ambiguous because the terminal string "c" has two different parse trees (S -> A -> c and S -> B -> c). Ambiguous grammars cannot be parsed deterministically by LR(1); attempting to build an LR(1) parser yields a reduce/reduce conflict for the completed reductions A -> c and B -> c with the same lookahead (end of input).

  • Conclusion: Neither an LL(1) parser nor an LR(1) parser can parse all strings generated by this grammar. Strings "a" and "b" are unambiguous, but "c" is ambiguous.

A video solution is available for this question β€” log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor