The GATE Grind

GATE 2016 CS (CS2) – Question 56

Compiler Design · Parsing and Syntax Analysis · 2 marks · Multiple choice

Start symbol D and terminals int, ;, id, [, ], num generate one C-like array declaration with at least one dimension, such as int a[10][3];. G₁: D → int L ;, L → id [ E, E → num ] | num ] [ E. G₂: D → int L ;, L → id E, E → E [ num ] | [ num ]. Which generate the intended declarations?

  1. Both G₁ and G₂
  2. Only G₁
  3. Only G₂
  4. Neither

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) Both G₁ and G₂

Explanation

G₁ opens the first bracket in L; each recursive E rule completes one dimension and opens another, while the base rule completes the last. G₂ starts with one [num] and appends more through left recursion. Both therefore generate one or more dimensions after the identifier, A.

More Parsing and Syntax Analysis questions

All Parsing and Syntax Analysis questions →