Parsing & Syntax Analysis
![Question 46, GATE 2016: Two grammars describe C-like declarations with at least one array dimension, e.g. int a[10][3];. Terminals: int, ;, id, [, ], num. G1: D→int L;, L→id [E, E→num] | num][E. G2: D→int L;, L→id E, E→E[num] | [num]. Which correctly generate the declarations? A both; B G1 only; C G2 only; D neither.](../../assets/questions/CS-2016-S2-Q56-1.webp)
Answer & solution
Official answer: A
G1 emits the opening bracket with id, then its recursive E→num][E adds each completed dimension and the next opening bracket, ending with E→num]. G2 emits one or more bracketed num groups through left recursion E→E[num] with base[num]. Both generate int id[num]...[num]; with at least one dimension, including the example. Thus A.
For two dimensions, G1 derives L⇒id [E⇒id [num][E⇒id [num][num]. G2 derives L⇒id E⇒id E[num]⇒id [num][num]. Substituting id=a and the two num tokens by 10 and 3 yields the example in each. Left recursion may inconvenience a parser, but it does not make the generated language incorrect.