Question

A student wrote two context-free grammars G1G_1 and G2G_2 for generating a single C-like array declaration. The dimension of the array is at least one. For example,

int a[10][3];

The grammars use DD as the start symbol, and use six terminal symbols int ; id [ ] num.

Grammar G1G_1:
D→int L;D \to \text{int } L;
L→id [EL \to \text{id } [ E
E→num]E \to \text{num}]
E→num][EE \to \text{num}] [ E

Grammar G2G_2:
D→int L;D \to \text{int } L;
L→id EL \to \text{id } E
E→E[num]E \to E[\text{num}]
E→[num]E \to [\text{num}]

Which of the grammars correctly generate the declaration mentioned above?

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

Solution

Trace the derivations of G1G_1 and G2G_2 for generating multidimensional array declarations.

A C-like array declaration of dimension k≥1k \ge 1 has the form:

int id [num]1[num]2…[num]k;\text{int } \text{id } [\text{num}]_1 [\text{num}]_2 \dots [\text{num}]_k ;

Trace Grammar G1G_1:
D→int L;D \to \text{int } L;
L→id [EL \to \text{id } [ E
E→num]∣num][EE \to \text{num}] \mid \text{num}] [ E

  • For dimension k=1k = 1:
D  ⟹  int L;  ⟹  int id [E;  ⟹  int id [num];D \implies \text{int } L; \implies \text{int id } [ E; \implies \text{int id } [\text{num}];
  • For dimension k=2k = 2:
D  ⟹  int id [E;  ⟹  int id [num][E;  ⟹  int id [num][num];D \implies \text{int id } [ E; \implies \text{int id } [\text{num}] [ E; \implies \text{int id } [\text{num}] [\text{num}];

By applying E→num][EE \to \text{num}] [ E recursively (k−1)(k-1) times followed by E→num]E \to \text{num}], G1G_1 generates an array declaration with any dimension k≥1k \ge 1. Hence, G1G_1 correctly generates the declaration.

Trace Grammar G2G_2:
D→int L;D \to \text{int } L;
L→id EL \to \text{id } E
E→E[num]∣[num]E \to E[\text{num}] \mid [\text{num}]

  • For dimension k=1k = 1:
D  ⟹  int L;  ⟹  int id E;  ⟹  int id [num];D \implies \text{int } L; \implies \text{int id } E; \implies \text{int id } [\text{num}];
  • For dimension k=2k = 2:
D  ⟹  int id E;  ⟹  int id E[num];  ⟹  int id [num][num];D \implies \text{int id } E; \implies \text{int id } E [\text{num}]; \implies \text{int id } [\text{num}] [\text{num}];

By applying the left-recursive rule E→E[num]E \to E [\text{num}] repeatedly (k−1)(k-1) times followed by E→[num]E \to [\text{num}], G2G_2 generates an array declaration with any dimension k≥1k \ge 1. Hence, G2G_2 correctly generates the declaration.

Therefore, both G1G_1 and G2G_2 correctly generate the declaration.

Final answer

A

Common trap

Assuming G1G_1 is invalid because the opening bracket is in the production for LL rather than EE, overlooking that the brackets match up identically in the full string.

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

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 2
Question
56 of 65, printed as CS Q.46
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.