GateAI
Context-Free Grammars and PDAs

GATE CSE 2016 Set 2 · Q46

MCQ2 marksMedium

Time

A student wrote two context-free grammars **G1** and **G2** 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 **G1** D→int  L;D \rightarrow \mathbf{int} \; L; L→id  [EL \rightarrow \mathbf{id} \; [E E→num]E \rightarrow \mathbf{num}] E→num]  [EE \rightarrow \mathbf{num}] \; [E Grammar **G2** D→int  L;D \rightarrow \mathbf{int} \; L; L→id  EL \rightarrow \mathbf{id} \; E E→E  [num]E \rightarrow E \; [\mathbf{num}] E→[num]E \rightarrow [\mathbf{num}] Which of the grammars correctly generate the declaration mentioned above?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: A) Both G1 and G2

Context-Free Grammars and PDAs

Let's analyze both grammars to see if they generate declarations of the form int  id  [num]…[num];\mathbf{int} \; \mathbf{id} \; [\mathbf{num}] \dots [\mathbf{num}]; for any dimension ≥1\ge 1:

  1. Grammar G1:

    • Derivation starts with: D⇒int  L;⇒int  id  [E;D \Rightarrow \mathbf{int} \; L; \Rightarrow \mathbf{int} \; \mathbf{id} \; [E;

    • If dimension is 1: E⇒num]E \Rightarrow \mathbf{num}] Yields: int  id  [num];\mathbf{int} \; \mathbf{id} \; [\mathbf{num}];

    • If dimension is 2: E⇒num]  [E⇒num]  [num]E \Rightarrow \mathbf{num}] \; [E \Rightarrow \mathbf{num}] \; [\mathbf{num}] Yields: int  id  [num][num];\mathbf{int} \; \mathbf{id} \; [\mathbf{num}][\mathbf{num}];

    • In general, EE generates one or more repetitions of num]\mathbf{num}] separated by [[, correctly forming id[num][num]…[num]\mathbf{id}[\mathbf{num}][\mathbf{num}]\dots[\mathbf{num}]. Thus, G1 generates the required declarations.

  2. Grammar G2:

    • Derivation starts with: D⇒int  L;⇒int  id  E;D \Rightarrow \mathbf{int} \; L; \Rightarrow \mathbf{int} \; \mathbf{id} \; E;

    • EE is a left-recursive non-terminal producing one or more [num][\mathbf{num}] pairs: E⇒+[num][num]…[num]E \Rightarrow^+ [\mathbf{num}][\mathbf{num}]\dots[\mathbf{num}]

    • Combining with int  id\mathbf{int} \; \mathbf{id}, it yields int  id[num][num]…[num];\mathbf{int} \; \mathbf{id}[\mathbf{num}][\mathbf{num}]\dots[\mathbf{num}];.

    • Thus, G2 also correctly generates the required declarations.

Therefore, Both G1 and G2 correctly generate the declaration, making (A) the correct option.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Context-Free Grammars and PDAs previous year question is Q46 of the GATE CSE 2016 Set 2 paper. Find more GATE CSE PYQs on Context-Free Grammars and PDAs in Compiler Design, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 1 related Context-Free Grammars and PDAs PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Context-Free Grammars and PDAs in GATE CSE

GATE CSE 2016 Set 2 Q46 is the only Context-Free Grammars and PDAs question in the GATE CSE papers so far.

How GATE tests Context-Free Grammars and PDAs

More from GATE CSE 2016 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2016 Set 2 · Q46 · MCQ

A student wrote two context-free grammars G1 and G2 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 D as the start symbol, and use six terminal symbols int ; id [ ] num. Grammar G1 D → int L; L → id [E E → num] E → num] [E Grammar G2 D → int L; L → id E E → E [num] E → [num] Which of the grammars correctly generate the declaration mentioned above?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.