Copyright © 2006 Gradiance Corporation.
Context-Free Grammars
Grammars, ambiguity, extended grammars.
1.
Programming languages are often described using an extended form of context-free grammar, where curly brackets are used to denote a construct that can repeat 0, 1, 2, or any number of times. For example, A → B{C}D says that an A can be replaced by a B and a D, with any number of C's (including 0) between them. This notation does not allow us to describe anything but context-free languages, since an extended production can always be replaced by several conventional productions.
Suppose a grammar has the extended production:
A → BC{DE}F
Convert this extended production to conventional productions. Identify, from the list below, the conventional productions that are equivalent to the extended production above.
a)
A → BCA1F
A1 → DE | ε
b)
A1 → A1DE | DE
c)
A → BCDEA1F
A1 → DEA1 | ε
d)
2.
Consider the grammar G with start symbol S:
S → bS | aA | b A → bA | aB B → bB | aS | a
Which of the following is a word in L(G)?
ababbbbbbbb
ababba
ababbbbb
ababbbbbbbbb
3.
Programming languages are often described using an extended form of context-free grammar, where square brackets are used to denote an optional construct. For example, A → B[C]D says that an A can be replaced by a B and a D, with an optional C between them. This notation does not allow us to describe anything but context-free languages, since an extended production can always be replaced by several conventional productions.
Suppose a grammar has the extended productions:
A → BC[DEFG]HI | BCDE[FGH]I
Convert this pair of extended productions to conventional productions. Identify, from the list below, the conventional productions that are equivalent to the extended productions above.
A → BCA1HI | BCDEA2I
A1 → DEFG | ε
A2 → FGH | ε
A → BCDEFGHI | BCHI | BCDEI | BCI
A1 → DEFG
A2 → FGH
A → BCA1I
A1 → DEFG | FGH
4.
Consider the following four grammars:
The initial symbol is S in all cases. Determine the language of each of these grammars. Then, find, in the list below, the pair of grammars that define the same language.
G1: S → aB, B → bS, B → b G2: S → aB, B → bS, S → ab
G1: S → aB, B → bS, B → b G2: S → aB, B → bS, S → a
G1: S → aB, B → bS, B → a G2: S → aB, B → bS, B → b
G1: S → aB, B → bS, B → ab G2: S → SS, S → ab
5.
The grammar G:
S → SS | a | b
is ambiguous. That means at least some of the strings in its language have more than one leftmost derivation. However, it may be that some strings in the language have only one derivation. Identify from the list below a string that has exactly TWO leftmost derivations in G.
aaaa
abab
abbab
aab
6.
Here is a context-free grammar G:
S → AB
A → 0A1 | 2
B → 1B | 3A
Which of the following strings is in L(G)?
002111300211
00213021
00021132
00211100211