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

Consider the given grammar
S → Abc | ad 

A → eS | Cr| ξ 

C → f/p/ξ
Compute FIRST (A)

The correct answer is

FIRST (A) = {e, f, p, r, \(\epsilon\) }

Understanding the FIRST Set Computation

This solution explains the process of calculating the FIRST set for a specific non-terminal 'A' using the rules of a given context-free grammar. The FIRST set of a non-terminal contains all the possible terminal symbols that can begin a string derived from that non-terminal, including the empty string (\(\epsilon\)) if the non-terminal can derive it.

Grammar Definition

The context-free grammar provided is:

  • S → Abc | ad
  • A → eS | Cr | ξ
  • C → f | p | ξ

In this grammar, 'ξ' represents the empty string (epsilon, denoted as \(\epsilon\)). 'S', 'A', and 'C' are non-terminal symbols, while 'a', 'b', 'c', 'e', 'f', 'p', 'r', and 'd' are terminal symbols.

Computing FIRST(A) Step-by-Step

We need to find the set of all terminals that can start a string derived from 'A'. We examine each production rule for 'A':

Step 1: Analyzing Production A → eS

The rule states that 'A' can directly produce 'e' followed by 'S'. Since 'e' is a terminal symbol, it is the first possible symbol in a string derived from 'A'.

Therefore, 'e' is added to FIRST(A).

Contribution from this rule: {e}

Step 2: Analyzing Production A → Cr

This rule states that 'A' can produce 'C' followed by 'r'. Here, 'C' is a non-terminal. To find the terminals that 'A' can start with via this rule, we first need to find the FIRST set of C (FIRST(C)).

Calculating FIRST(C)

Let's look at the rules defining 'C':

  • C → f: 'f' is a terminal. So, 'f' ∈ FIRST(C).
  • C → p: 'p' is a terminal. So, 'p' ∈ FIRST(C).
  • C → ξ: This rule allows 'C' to derive the empty string, \(\epsilon\). So, \(\epsilon\) ∈ FIRST(C).

Combining these, we get: FIRST(C) = {f, p, \(\epsilon\)}.

Applying FIRST(C) to A → Cr

Now, we use the calculated FIRST(C) for the rule A → Cr:

  • The terminals 'f' and 'p' from FIRST(C) are added to FIRST(A).
  • Crucially, FIRST(C) includes \(\epsilon\). This means 'C' can disappear, and the derivation continues with the *next* symbol in the rule A → Cr, which is 'r'. Since 'r' is a terminal, it is also added to FIRST(A).

Contribution from this rule: {f, p, r}

Step 3: Analyzing Production A → ξ

This rule directly shows that 'A' can derive the empty string, \(\epsilon\).

Therefore, \(\epsilon\) is added to FIRST(A).

Contribution from this rule: {\(\epsilon\)}

Combining the Results for FIRST(A)

To get the complete FIRST(A) set, we unite the contributions from all the rules for 'A':

  • From A → eS: {e}
  • From A → Cr: {f, p, r}
  • From A → ξ: {\(\epsilon\)}

Combining these sets: FIRST(A) = {e} \(\cup\) {f, p, r} \(\cup\) {\(\epsilon\)}

This results in: FIRST(A) = {e, f, p, r, \(\epsilon\)}.

Final Answer

The computed FIRST set for the non-terminal A is {e, f, p, r, \(\epsilon\)}.

Was this answer helpful?

Important Questions from Context Free Grammars

  1. Which of the following Grammars is/are only Context Free?
    AS $\rightarrow$ Ab
    aS$\rightarrow$ aA
    A$\rightarrow$ a
    BS $\rightarrow$ Ab
    A $\rightarrow$ Sa
    A $\rightarrow$ a
    CS $\rightarrow$ AS
    S $\rightarrow$ aA
    A$\rightarrow$ a
    DS $\rightarrow$ Ab
    S $\rightarrow$ aA
    A$\rightarrow$ a
    ES $\rightarrow$ Sb
    A $\rightarrow$ Aa
    A $\rightarrow$ $\varepsilon$

    Choose the correct answer from the options given below:
  2. Which of the following grammars is (are)ambiguous ?
    (1) S → SS | aSb | bSa |e 

    (2) S → aSbS | bSaS |e 

    (3) S → aAB 

    A → bBb 
    B → A Ι e

  3. Consider the grammar,

    G = S → abScB | e, 

    B→ bB | b. 

    What is the language generated by the given grammar ?

     

  4. Select the sentence which is meaningful and grammatically correct.

  5. Consider the following context-free grammar $G$.
    $$S \rightarrow abaABabba$$
    $$A \rightarrow aaBBAB \mid bBabaa$$
    $$B \rightarrow aBb \mid ab$$
    In the above grammar, $S$ is the start symbol, $a$ and $b$ are terminal symbols, and $A$ and $B$ are non-terminal symbols.
    Let $L(G)$ be the language generated by the grammar $G$. For a string $s \in L(G)$, let $n_1(s)$ be the number of $a$’s in $s$ and $n_2(s)$ be the number of $b$’s in $s$.
    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