ISRO Scientist/Engineer CSE 2018Compiler Design › Parsing
ISRO Scientist/Engineer CSE 2018 PYQ: A particular BNF definition for a "word is given by the following rules. <word> :: = <letter> | <letter>…
A particular BNF definition for a "word is given by the following rules.
<word> :: = <letter> | <letter> <charpair> | <letter> <intpair>
<charpair> :: = <letter> <letter> | <charpair> <letter> <letter>
<intpair> :: = <integer> <integer> | <intpair> <integer> <integer>
<letter> :: = a | b | c | ...... | Y | Z
<integer> :: = 0 | 1 | 2 | ...... | 9Which of the following lexical entries can be derived from $\textsf{< word >}?$
- pick
- picks
- $\textsf{c44}$
- AI, II and III
- BI and II only
- CI and III only
- DII and III onlyCorrect answer
Answer
(D) II and III only
Approach
The grammar is:
<word> ::= <letter> | <letter><charpair> | <letter><intpair>
<charpair> ::= <letter><letter> | <charpair><letter><letter>
<intpair> ::= <integer><integer> | <intpair><integer><integer>
Key structural facts:
- <charpair> generates strings of letters whose length is always EVEN (2, 4, 6, ...). The base rule gives 2 letters; the recursive rule adds 2 more letters each time, preserving parity.
- <intpair> generates strings of digits whose length is always EVEN (2, 4, 6, ...), by the same parity argument.
- Consequently a <word> can be: (a) exactly one letter; (b) one letter followed by an even number (≥2) of letters, giving an ODD total letter count (1, 3, 5, ...); or (c) one letter followed by an even number (≥2) of digits. A word can never be an even-length all-letter string, and never mixes letters with digits except in the single-letter-then-digits form.
Now test each entry:
I. "pick" — 4 letters, which is even. It is not a single letter, and letter+charpair forces an odd total. Letters alone must be odd in number. So I is NOT derivable.
II. "picks" — 5 letters (odd ≥3). Derive as letter + charpair with a 4-letter charpair: p + (i c) + (k s) = p i c k s. Derivable.
III. "c44" — one letter followed by two digits. Derive as letter + intpair with the base intpair giving 4 and 4: c + 4 + 4. Derivable.
Only II and III are derivable, matching option D. The attached derivation confirms picks via <letter><charpair> and c44 via <letter><intpair>. Both prior passes were correct; the self-flag was unnecessary.
Reviewed by ISRO IQ Editorial Team.