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 Grammar G1 Grammar G2 D → int L; D → int L; L → id[E L → idE E → num] E → E[num] E → num][E E → [num]   Which of the grammars correctly generate the declaration mentioned above?

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 Grammar G1 Grammar G2 D → int L; D → int L; L → id[E L → idE E → num] E → E[num] E → num][E E → [num]   Which of the grammars correctly generate the declaration mentioned above? Correct Answer Both <strong>G1</strong> and <strong>G2</strong>

Grammar G1 is:

D → int L;

L → id

E → num]

D → int L

→ int id

This leads to int a

Generate two-dimensional array: int a ;

D → int L;

→ int id ;

This leads to int a

It correctly generates declaration given.

Grammar G2 is:

D → int L;

L → idE

E → E

E →

Generate one dimensional array: a

D → int L;

→ int idE

→ int id

This leads to int a

Generate two-dimensional array: int a ;

int a;

D → int L;

→ int id E;

→ int id E;

→ int id ;

This leads to int a

So, both grammar G1 and G2 generates the given declaration.

Related Questions

What is the right choice, if the following loop is implemented?
void main(){ int num = 0; do{ --num; printf("%d", num); }while( ++num >= 0 );}