All Exams Test series for 1 year @ ₹349 only
Question

Consider the following Grammar G:

S ➝ A | B

A➝ a | c

B➝ b | c

Where {S, A, B} is the set of non-terminals, {a, b, c} is the set of terminals.

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

S 1 : LR(1) can parse all strings that are generated using grammar G.

S 2 : LL(1) can parse all strings that are generated using grammar G.

The correct answer is

Neither S1 nor S 2

Let's analyze the given grammar G and determine if it can be parsed by LL(1) and LR(1) parsers. The grammar is defined as:

S ➝ A | B

A ➝ a | c

B ➝ b | c

Where {S, A, B} are non-terminals and {a, b, c} are terminals.

Analyzing LL(1) Parsability of Grammar G

A grammar is LL(1) if for every non-terminal A and every two distinct productions A ➝ α and A ➝ β, the following conditions hold:

  1. $\text{FIRST}(\alpha) \cap \text{FIRST}(\beta) = \emptyset$
  2. If $\epsilon \in \text{FIRST}(\alpha)$, then $\text{FIRST}(\beta) \cap \text{FOLLOW}(A) = \emptyset$
  3. If $\epsilon \in \text{FIRST}(\beta)$, then $\text{FIRST}(\alpha) \cap \text{FOLLOW}(A) = \emptyset$

Let's calculate the FIRST and FOLLOW sets for grammar G.

Calculating FIRST Sets

  • $\text{FIRST}(a) = \{a\}$
  • $\text{FIRST}(b) = \{b\}$
  • $\text{FIRST}(c) = \{c\}$
  • $\text{FIRST}(A)$: From A ➝ a and A ➝ c. $\text{FIRST}(A) = \text{FIRST}(a) \cup \text{FIRST}(c) = \{a\} \cup \{c\} = \{a, c\}$
  • $\text{FIRST}(B)$: From B ➝ b and B ➝ c. $\text{FIRST}(B) = \text{FIRST}(b) \cup \text{FIRST}(c) = \{b\} \cup \{c\} = \{b, c\}$
  • $\text{FIRST}(S)$: From S ➝ A and S ➝ B. $\text{FIRST}(S) = \text{FIRST}(A) \cup \text{FIRST}(B) = \{a, c\} \cup \{b, c\} = \{a, b, c\}$
FIRST Sets for Grammar G
Non-terminal/Terminal FIRST Set
a {a}
b {b}
c {c}
A {a, c}
B {b, c}
S {a, b, c}

Calculating FOLLOW Sets

Assume $ is the end-of-input marker.

  • $\text{FOLLOW}(S)$: S is the start symbol, so $\text{FOLLOW}(S)$ must include $$. $\text{FOLLOW}(S) = \{\$\}$
  • $\text{FOLLOW}(A)$: From S ➝ A, anything that follows A must be in $\text{FOLLOW}(S)$. $\text{FOLLOW}(A) = \text{FOLLOW}(S) = \{\$\}$
  • $\text{FOLLOW}(B)$: From S ➝ B, anything that follows B must be in $\text{FOLLOW}(S)$. $\text{FOLLOW}(B) = \text{FOLLOW}(S) = \{\$\}$
FOLLOW Sets for Grammar G
Non-terminal FOLLOW Set
S {$\$$}
A {$\$$}
B {$\$$}

Checking LL(1) Conflicts

Let's check the LL(1) conditions for each non-terminal with multiple productions:

  • For S ➝ A | B:

    We need to check $\text{FIRST}(A) \cap \text{FIRST}(B)$.

    $\text{FIRST}(A) = \{a, c\}$

    $\text{FIRST}(B) = \{b, c\}$

    $\text{FIRST}(A) \cap \text{FIRST}(B) = \{a, c\} \cap \{b, c\} = \{c\}$.

    Since the intersection is not empty (it contains 'c'), there is an LL(1) conflict. When the parser is trying to expand S and the next input token is 'c', it doesn't know whether to choose the production S ➝ A or S ➝ B.

  • For A ➝ a | c:

    We need to check $\text{FIRST}(a) \cap \text{FIRST}(c)$.

    $\text{FIRST}(a) = \{a\}$

    $\text{FIRST}(c) = \{c\}$

    $\text{FIRST}(a) \cap \text{FIRST}(c) = \emptyset$. No conflict here.

  • For B ➝ b | c:

    We need to check $\text{FIRST}(b) \cap \text{FIRST}(c)$.

    $\text{FIRST}(b) = \{b\}$

    $\text{FIRST}(c) = \{c\}$

    $\text{FIRST}(b) \cap \text{FIRST}(c) = \emptyset$. No conflict here.

Because of the conflict for non-terminal S, Grammar G is not an LL(1) grammar. Therefore, an LL(1) parser cannot parse all strings generated by this grammar.

Analyzing LR(1) Parsability of Grammar G

A grammar is LR(1) if the LR(1) parsing table constructed for it contains no conflicts (shift-reduce or reduce-reduce). Let's consider the string "c". This string can be generated in two ways:

  • S ➝ A ➝ c
  • S ➝ B ➝ c

When an LR(1) parser has read 'c' and the next input is the end-of-input marker ($), it will be in a state containing items that could lead to reduction. Specifically, it might contain items like:

  • A ➝ c . , { $ }
  • B ➝ c . , { $ }

The lookahead for both these completed items is {$}. This situation presents a reduce-reduce conflict on the lookahead symbol $$. The parser does not know whether to reduce the parsed 'c' using the production A ➝ c or using the production B ➝ c.

Because of this reduce-reduce conflict, Grammar G is not an LR(1) grammar. Therefore, an LR(1) parser cannot parse all strings generated by this grammar.

Evaluating Statements on Grammar Parsability

Based on the analysis:

  • S1: LR(1) can parse all strings that are generated using grammar G.

    This statement is false because we found a reduce-reduce conflict in the LR(1) analysis, meaning the grammar is not LR(1).

  • S2: LL(1) can parse all strings that are generated using grammar G.

    This statement is false because we found an LL(1) conflict for non-terminal S (based on FIRST sets of A and B), meaning the grammar is not LL(1).

Since both S1 and S2 are false, neither statement is correct.

Revision Table: Grammar G Parsing

Parsing Technique Analysis Result Reason
LL(1) Cannot parse G Conflict: $\text{FIRST}(A) \cap \text{FIRST}(B) = \{c\}$ for S ➝ A | B
LR(1) Cannot parse G Reduce-reduce conflict on 'c' with lookahead $

Additional Information: Parser Types

Let's quickly review LL(1) and LR(1) parsers:

  • LL(1) Parsers: These are top-down parsers. They read the input from Left to right, and construct a Leftmost derivation. The '(1)' indicates that they use one lookahead symbol to make parsing decisions. Conflicts arise from ambiguities in choosing which production to apply based on the current non-terminal and the next input symbol.
  • LR(1) Parsers: These are bottom-up parsers. They read the input from Left to right, and construct a Rightmost derivation in reverse. The '(1)' indicates they use one lookahead symbol. LR parsers are more powerful than LL parsers. Conflicts (shift-reduce or reduce-reduce) arise when the parser cannot decide whether to shift the next input symbol or reduce a sequence of symbols on the stack using a grammar production, or when it cannot decide between multiple possible reductions.

While LR(1) parsers are more powerful than LL(1) parsers, meaning they can parse a larger set of grammars, there exist grammars that are neither LL(1) nor LR(k) for any k, or grammars that are LR(k) but still contain conflicts in simpler LR variants (like SLR(1) or LALR(1)). The given grammar G is a simple example that fails both LL(1) and LR(1) properties due to the inherent ambiguity for the terminal 'c'.

Was this answer helpful?

Important Questions from Parser

  1. Consider the following statements:

    Statement I: LALR parser is more powerful than canonical LR Parser.

    Statement II: SLR parser is more powerful than LALR

    Which of the following is correct?

  2. Given below are two statements

    Statement I : LL(1) and LR are examples of Bottom‐up parsers.

    Statement II : Recursive descent parser and SLR are examples of Top‐down parsers

    In light of the above statements, choose the correct answer from the options given below

  3. The grammar S → SS | (S)| ϵ is not suitable for predictive parsing because the grammar is:

  4. Arrange the following parsers in increasing order of their power of handling grammars i.e. from the least powerful parser to the most powerful parser.
    A. LR(0)
    B. LR(1)
    C. LALR(1)
    D. LL(0)
    E. SLR
    Choose the correct answer from the options given below:
  5. Which of the following statements is/are true?
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App