Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Context-free grammars
Example1
Find a context-free grammar (CFG) for the language L of all
words of odd length with exactly one a or exactly one b
over the alphabet Σ = { a, b }
Solution 1
S BaB | AbA | X | Y
B bbB | /\
A aaA | /\
X BbabB
Y AabaA
A bi-directional proof that CFG = L:
a) Prove that all words generated by the CFG are words in L
The first production results in one of four working
strings:
(1) S BaB, which can only produce words if a B-production
is applied. B bbB | /\ replaces the B non-terminals with
an even number of b’s, so all words produced in this way
will have one a, and an even number of b’s on the left and
an even number on the right.
(2) S AbA, which can only produce words if an Aproduction is applied. A aaA | /\ replaces the A nonterminals with an even number of a’s, so all words produced
in this way will have one b, and an even number of a’s on
the left and an even number on the right.
(3) S X, which can only produce words if the X-production
is applied, followed by one or more B-productions. The Xproduction produces a working string with one a, and one b
on the left and one on the right (i.e. an even number of
b’s). We already know that the B-productions will add only
an even number of b’s to the word, so the total number of
b’s in words produced this way will always be even.
(4) S Y, which can only produce words if the Y-production
is applied, followed by one or more A-productions. The Yproduction produces a working string with one b, and one a
on the left and one on the right (i.e. an even number of
a’s). We already know that the A-productions will add only
an even number of a’s to the word, so the total number of
a’s in words produced this way will always be even.
From the above, it is evident that all the words produced
by the CFG either have one a and an even number of b’s, or
one b and an even number of a’s. Because ‘even + 1 = odd’,
the total number of letters in all these words is odd. All
these words are indeed in L.
b) Prove that all words in L can be generated by the CFG
All words in L have either one a and an even number of b’s,
or one b and an even number of a’s, giving an odd wordlength.
Let us examine those words with only one a:
The smallest word in the language is a, which can be
generated by the CFG with the productions S -> BaB /\a/\.
If the word does contain some b’s, there must be an even
total of them, i.e. an even number on the left of a and an
even number on the right (Case 1), or an odd number on the
left and an odd number on the right (Case 2).
Case 1:
S BaB produces a working string that will be used to add
b’s to the left and right of a.
B bbB | /\ replaces the B’s on the left / right with an
even number of b’s.
Case 2:
S X BbabB produces a working string that already
contains two b-terminals - one on the left of a, and one on
the right.
B bbB | /\ replaces the B’s on the left / right with an
even number of b’s, resulting in an odd number of b’s on
each side of a.
A similar argument holds for all words that have only one b
and an even number of a’s. We can conclude that all words
in L can be generated by the CFG.
From a) and b) above, it follows that The CFG generates the
language of all words of odd length with either one a or
one b, and no other words.
Example2
Find a CFG for the language consisting of words of odd
length over the alphabet ∑={a,b} that start and end with
different letters.
Solution 2
-S
b
a
P
b
X
a,b
a,b
Q
a
Y
a
b
a,b
+R
a,b
+Z
Assuming that the FA is correct, Cohen’s Theorem 21 can be
applied, which results in the following CFG:
S
X
Y
Z
P
Q
R
aX
aY
aX
aY
aQ
aR
aQ
|
|
|
|
|
|
|
bP
bY
bZ
bY | /\
bQ
bP
bQ | /\
Example3
Find a context-free grammar (CFG) for the language L of all
words in which groups of a’s and groups of b’s alternate,
a’s occurring only in groups (substrings) of even length
and b’s only in groups (substrings) of odd length over the
alphabet Σ = { a, b }
Solution 3
S AS | BXS | BA
A aaA | /\
X aaA | aa
B bbB | b
Bi-directional proof that CFG = L:
a) Prove that all words generated by the CFG are in L:
The three S-productions result in the following working
strings:
1) S AS, which, after using the A-productions, gives any
even number of a’s followed by S. To produce words, the
third S-production must be used (See below) to obtain
substrings that follow these (optional) a’s.
2) S BXS produces working strings that start with an odd
number of b’s (after the compulsory production(s) B bbB |
b). At least two a’s must follow the b(s) (from the
compulsory production X aaA | aa), which results in an
odd number of b’s followed by an even number of a’s (not
zero!), followed by S, which in turn relies on production 3
(see below) to produce words.
3) S BA gives you an odd number of b’s (from B bbB |
b) followed by an even number of a’s (from A aaA | /\)
All words have to use the 3rd S-production, which gives you
(odd b’s)(even a’s). Optionally, one of the first two Sproductions may be used (repetitively) beforehand, to
concatenate substrings in front. The substrings produced by
the 1st and 2nd S-productions give you (even a’s) and ((odd
b’s)(even non-zero a’s))* respectively. When these are
placed in front of (odd b’s)(even a’s), you always get the
same result: a word in which there are alternating a’s and
b’s, with the b’s in odd clumps and the a’s in even ones.
(Note: In the CFG, care was taken to ensure that the odd
b’s were always separated by at least two a’s, hence the Xproduction, because concatenating two odd strings leads to
an even string!)
b) Prove that all words in L can be generated by the CFG:
Words in L can take several forms, but they all have one
thing in common: each word must contain at least one b.
This (smallest word) can be produced by applying S BA,
and then A /\ and B b.
All words start with an even number of a’s (which includes
zero). By applying S AS and then one of the Aproductions, this even-a-substring is obtained. Next come
the b’s. If there is only one b group, the production S
BA, followed by any number of B-productions (which give you
an odd number of b’s) and A /\ must be used. If there are
in fact a’s after this sole clump of b’s, then A aaA must
be used first.
Now, if there are several groups of odd-b clumps, separated
by even a’s, the following productions are necessary:
Repeated use of S BXS concatenates odd b’s with even a’s.
To complete the word, the production S BA must be used,
which allows words to end with b (from B bbB | b, A
/\) or a (from B bbB | b, A aaA, /\).
In short, S BXS must be used n-1 times (where n is the
number of b-groups) and finally S BA results in a word.
In this way, all words in L can be generated.
From a) and b) above, it can be deduced that the CFG is
correct.
Example4
Find a CFG for the language consisting of all words without
the substring aa over the alphabet ∑={a,b}
Solution 4
b
a
a
+-S
+X
b
Y
a,b
Assuming that the FA is correct, Cohen’s Theorem 21 can be
applied, which results in the following CFG:
S aX | bS | /\
X aY | bS | /\
Example5
Find a context-free grammar (CFG) for the language L of all
words with length multiples of 4 and where every fourth
letter is an a. Assume that the alphabet is Σ = { a, b }.
(You need not prove that the language generated by your
grammar is the same as L). Hint: You may find it helpful to
draw an FA.
Solution 5
X
a,b
a,b
+-S
Y
a,b
a
Z
b
a,b
Assuming that the FA is correct, Cohen’s Theorem 21 can be
applied, which results in the following CFG:
S
X
Y
Z
aX | bX | /\
aY | bY
aZ | bZ
aS
Example6
Find a CFG for the language over the alphabet Σ = { a, b }
of all words that end in b and have an even number of b’s
in total.
Solution 6
a
a
b
-S
b
X
+Y
a
b
Assuming that the FA is correct, Cohen’s Theorem 21 can be
applied, which results in the following CFG:
S aS | bX
X aX | bY
Y aS | bX | /\
Example7
Find a context-free grammar (CFG) for the language L of all
words ending on b and where the a’s occur in groups of even
length. An example is bbaabbbaaaab: it ends on b and it
contains two groups of a’s: one group has length 2 and one
group has length 4. Assume the alphabet Σ = { a, b }. (You
need not prove that the language generated by your grammar
is the same as L.) Hint: You may find it helpful to draw an
FA.
Solution 7
a
-S
b
b
X
a
a
+Z
b
Y
a,b
Assuming that the FA is correct, Cohen’s Theorem 21 can be
applied, which results in the following CFG:
S aX | bZ
X aS
Z aX | bZ | /\
(The Y state was excluded from the CFG because it is a
dead-end state and is not used in the generation of words).
Example8
Find a CFG for the language L consisting of all words which
do not end on ba. Assume that the alphabet is ∑={ a, b }
(You need not prove that the language generated by your
grammar is the same as L). Hint: You may find it helpful to
draw an FA.
Solution 8
a
b
b
+-S
a
+X
Y
a
b
Assuming that the FA is correct, Cohen’s Theorem 21 can be
applied, which results in the following CFG:
S aS | bX | /\
X bX | aY | /\
Y aS | bX