2.3k views

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.
$$\begin{array}{l|l} \text{Grammar } \textbf{G1} & \text{Grammar } \textbf{G2} \\\hline \text{D} \rightarrow \textbf{int L;} & \text{D} \rightarrow \textbf{int L;}\\ \text{L} \rightarrow \textbf{id [E} & \text{L} \rightarrow \textbf{id E}\\ \text{E} \rightarrow \textbf{num ]} & \text{E} \rightarrow \textbf{E [num]}\\ \text{E} \rightarrow \textbf{num ] [E} & \text{E} \rightarrow \textbf{[num]}\\ \end{array}$$Which of the grammars correctly generate the declaration mentioned above?

1. Both G1 and G2
2. Only G1
3. Only G2
4. Neither G1 nor G2
edited | 2.3k views
–5
i dont know whether its a valid reason or not .... but according to me niether g1 nor g2 can generate correctly...... reason : here num is not defined to be an integer...it can also be an alphabet.....or float ,etc
0
ans-A
0
In this question its mentioned that dimension of array is "atleast 1". If we choose G2 isnt the dimension of the array becoming atleast 2?
0
no in g2 if after 2nd it derives 4th line then 1 dimension is also possible

Option A Both G1 and G2

edited
–1
i think only g2 is correct because c language is by default row major order
0
@nithin

Its mentioned c like language, not C. Don't assume things not mentioned in questions.

1
2