Linked answer questions

Statement for Linked Answer Questions 52 and 53:

For the grammar below, a partial LL(1) parsing table is also presented along with the grammar. Entries that need to be filled are indicated as E1, E2, and E3. ϵ\epsilon is the empty string, $\$ indicates end of input, and ∣| separates alternate right hand sides of productions.

S→aAbB∣bAaB∣ϵS \to aAbB \mid bAaB \mid \epsilon
A→SA \to S
B→SB \to S
Non-terminal aa bb $\$
SS E1 E2 S→ϵS \to \epsilon
AA A→SA \to S A→SA \to S error
BB B→SB \to S B→SB \to S E3

Shared with question 53.

Question

The FIRST and FOLLOW sets for the non-terminals A and B are

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2012 answer key

Solution

Compute FIRST and FOLLOW sets for non-terminals A and B in the given grammar.

  1. Grammar productions:
S→aAbB∣bAaB∣ϵS \to aAbB \mid bAaB \mid \epsilon
A→SA \to S
B→SB \to S
  1. Computing FIRST sets:
  • FIRST(S)={a,b,ϵ}\text{FIRST}(S) = \{a, b, \epsilon\}.
  • Since A→SA \to S, FIRST(A)=FIRST(S)={a,b,ϵ}\text{FIRST}(A) = \text{FIRST}(S) = \{a, b, \epsilon\}.
  • Since B→SB \to S, FIRST(B)=FIRST(S)={a,b,ϵ}\text{FIRST}(B) = \text{FIRST}(S) = \{a, b, \epsilon\}.
  1. Computing FOLLOW sets:
  • SS is start symbol, so $∈FOLLOW(S)\text{\$} \in \text{FOLLOW}(S).
  • In S→aAbBS \to aAbB: AA is followed by bb, so b∈FOLLOW(A)b \in \text{FOLLOW}(A). BB is followed by nothing, so FOLLOW(S)⊆FOLLOW(B)\text{FOLLOW}(S) \subseteq \text{FOLLOW}(B).
  • In S→bAaBS \to bAaB: AA is followed by aa, so a∈FOLLOW(A)a \in \text{FOLLOW}(A). BB is followed by nothing, so FOLLOW(S)⊆FOLLOW(B)\text{FOLLOW}(S) \subseteq \text{FOLLOW}(B).
  • Thus, FOLLOW(A)={a,b}\text{FOLLOW}(A) = \{a, b\}.
  • Since A→SA \to S, FOLLOW(A)⊆FOLLOW(S)\text{FOLLOW}(A) \subseteq \text{FOLLOW}(S), meaning {a,b}⊆FOLLOW(S)\{a, b\} \subseteq \text{FOLLOW}(S). Combined with $\text{\$}, FOLLOW(S)={a,b,$}\text{FOLLOW}(S) = \{a, b, \text{\$} \}.
  • Because FOLLOW(S)⊆FOLLOW(B)\text{FOLLOW}(S) \subseteq \text{FOLLOW}(B), FOLLOW(B)={a,b,$}\text{FOLLOW}(B) = \{a, b, \text{\$} \}.
  1. Therefore:
  • FIRST(A)={a,b,ϵ}=FIRST(B)\text{FIRST}(A) = \{a, b, \epsilon\} = \text{FIRST}(B)
  • FOLLOW(A)={a,b}\text{FOLLOW}(A) = \{a, b\}
  • FOLLOW(B)={a,b,$}\text{FOLLOW}(B) = \{a, b, \text{\$} \}
  1. This matches option (A).
Final answer

A

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2012 Computer Science and Information Technology (CS)
Question
52 of 65, printed as CS Q.52
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.