The Gateway to Computer Science Excellence
+27 votes
2.5k 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
in Compiler Design by Boss (41.4k points)
edited by | 2.5k 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

1 Answer

+42 votes
Best answer

Option A Both G1 and G2

by Active (3.2k points)
edited by
–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.
0
thanku so much.. it's really helpful 😊
0

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

Answer:

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
193,838 comments
94,014 users