The Gateway to Computer Science Excellence
+27 votes

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
in Compiler Design by Boss (41.4k points)
edited by | 2.5k views
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 can also be an alphabet.....or float ,etc
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?
no in g2 if after 2nd it derives 4th line then 1 dimension is also possible

1 Answer

+42 votes
Best answer

Option A Both G1 and G2

by Active (3.2k points)
edited by
i think only g2 is correct because c language is by default row major order

Its mentioned c like language, not C. Don't assume things not mentioned in questions.
thanku so much.. it's really helpful 😊

@smsubham what would if in question it was mentioned that it is c language ? i mean the reason ?


Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,666 questions
56,167 answers
94,014 users