# GATE | GATE-CS-2016 (Set 2) | Question 56

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
D → int L;
L → id [E
E → num]
E → num] [E

Grammar G2
D → int L;
L → id E
E → E[num]
E → [num] ```

Which of the grammars correctly generate the declaration mentioned above?

(A) Both G1 and G2
(B) Only G1
(C) Only G2
(D) Neither G1 nor G2

Explanation: Context-Free Grammars

A context-free grammar (CFG) is a set of recursive rewriting rules (or productions) used to generate patterns of strings.

A CFG consists of the following components:
1) A set of terminal symbols, which are the characters of the alphabet that appear in the strings generated by the grammar.
2) A set of nonterminal symbols, which are placeholders for patterns of terminal symbols that can be generated by the nonterminal symbols.
3) A set of productions, which are rules for replacing (or rewriting) nonterminal symbols (on the left side of the production) in a string with other nonterminal or terminal symbols (on the right side of the production).
4) A start symbol, which is a special nonterminal symbol that appears in the initial string generated by the grammar.

To generate a string of terminal symbols from a CFG, we:
1) Begin with a string consisting of the start symbol;
2) Apply one of the productions with the start symbol on the left hand size, replacing the start symbol with the right hand side of the production;
3) Repeat the process of selecting nonterminal symbols in the string, and replacing them with the right hand side of some corresponding production, until all nonterminals have been replaced by terminal symbols.

Given a grammar G with start symbol S, if there is some sequence of productions that, when applied to the initial string S, result in the string s, then s is in L(G), the language of the grammar.

Source : https://www.cs.rochester.edu/~nelson/courses/csc_173/grammars/cfg.html

We need to check which of the two grammars correctly generate “int a[10][3];”. This means we need to check if int id[num][num];

```Grammar G1
D → int L;
L → id [E
E → num]
E → num] [E

Grammar G2
D → int L;
L → id E
E → E[num]
E → [num] ```

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.