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?

- Both
**G1**and**G2** - Only
**G1** - Only
**G2** - Neither
**G1**nor**G2**

–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

+42 votes

- All categories
- General Aptitude 1.9k
- Engineering Mathematics 7.4k
- Digital Logic 2.9k
- Programming and DS 4.9k
- Algorithms 4.4k
- Theory of Computation 6.2k
- Compiler Design 2.1k
- Databases 4.1k
- CO and Architecture 3.4k
- Computer Networks 4.1k
- Non GATE 1.4k
- Others 1.7k
- Admissions 595
- Exam Queries 576
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 17

50,666 questions

56,167 answers

193,838 comments

94,014 users