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.
Which of the grammars correctly generate the declaration mentioned above?
View worked solution
Answer: A
To determine which grammars correctly generate the array declaration int a[10] [3];, we can trace the derivations for both and using the terminals: int, ;, id (for a), [, ], and num (for 10 and 3).
Derivation in Grammar :
Using the rules of :
Substituting id with a, and the first and second num with 10 and 3 respectively, we get: int a[10] [3];. Thus, correctly generates the declaration.
Derivation in Grammar :
Using the rules of :
Substituting the terminals, we get: int a[10] [3];. Thus, also correctly generates the declaration.
Since both grammars successfully generate the given declaration, the correct option is A.
Free practice · Your answers on this page are not saved.
