Professional Documents
Culture Documents
Lecture 11 - Context Free Grammars (CFG)
Lecture 11 - Context Free Grammars (CFG)
Definition of CFG
A context-free grammar is a 4-tuple (V, T, S, P) where
(i) V is a finite set called the variables
(ii) T is a finite set, disjoint from V, called the terminals
(iii) P is a finite set of rules, with each rule being a variable and a
string of variables and terminals, and
(iv) S V is the start variable.
If u, v and w are strings of variables and terminals, and A w is a rule
of the grammar, we say that uAv yields uwv, written uAv uwv.
Example of CFG
Given a grammar G = ({S}, {a, b}, R, S).
The set of rules R is
S aSb
SSS
S
This grammar generates strings such as
abab, aaabbb, and aababb
If we assume that a is left paranthesis ‘(’ and b is right paranthesis ‘)’,
then L(G) is the language of all strings of properly nested parantheses.
Right-Linear Grammar
In general productions have the form:
(V T) (V T)*.
In right-linear grammar, all productions have one of the two forms:
VT*V
or V T*
i.e., the left hand side should have a single variable and the right hand side
consists of any number of terminals (members of T) optionally followed by a
single variable.
A0C 0 0 0 0
A1S
1
B 0S
B1C B C
Left-Linear Grammar
In a left-linear grammar, all productions have one of the two forms:
V VT*
or V T*
i.e., the left hand side must consist of a single varibale, and the right-hand side
consists of an optional single variable followed by one number of terminals.
Step Method
(a) Construct a right-linear Replace each production A x of L
grammar for the different with a production A x R and
languages LR. replace each production A Bx
with a production A x R B
R
(b) Construct an NFA for L from Refer to section 2.1.4 for deriving an
the right-linear grammar. This NFA from a right-linear grammar.
NFA should have just one
final state.
(c) Reverse the NFA for LR to (i) Construct an NFA to
obtain an NFA for L. recognize the language L.
(ii) Ensure the NFA has only a
single final state
(iii) Reverse the direction of arcs
(iv) Make the initial state final and
final state initial
(d) Construct a right-linear This is the technique described in
grammar for L from the the previous section.
NFA for L.
Example 2.1.1: Give some example of context-free languages.
Solution
(a) The grammar G = ({S}, {a, b}, S, P) with productions
S aSa, S bSb, S
is context free.
S aSa aaSaa aabSbaa aabbaa
Thus we have L(a) {ww R : w {a, b}* }.
This language is context free.
(b) The grammar G, with production rules given by
S abB,
A aaBb,
B bbAa,
A
is context free.
The language is
L(G) {ab (bbaa ) n bba (ba ) n :n 0}
Solution
Right-Linear Grammar:
S aS
S aaA
A bA
A bbb
Left-Linear Grammar:
S Abbb
S Sb
A Aa
A aa
DERIVATION TREES
A ‘derivation tree’ is an ordered tree which the the nodes are labeled with the
left sides of productions and in which the children of a node represent its
corresponding right sides.
Sentential Form
For a given CFG with productions S aA, A aB , B bB , B a. The
derivation tree is as shown below.
S
a
A
a
B
b
B
2. S b
Now, we have
1
S aSS
1
aaSSS
2
aabSS (Left Most Derivation)
1
aabaSSS
2
aababSS
2
aababbS
2
aababbb
The sequence followed is “left most derivation”, following “1121222”, giv
- ing “aababbb”.
1
S aSS
2
aSb
1
aaSSb (Mixed Derivation)
2
aabSb
1
aabaSSb
2
aabaSbb
2
aababbb
The sequence 1212122 represents a “Mixed Derivation”, giving
“aababbb”.
1
S aSS
2
aSb
1
aaSSb
1
aaSaSSb(Right Most Derivation)
2
aaSaSbb
2
aaSabbb
2
aababbb
The sequence 1211222 represents a “Right Most Derivation”, giving
“aababbb”.
S aAB
A Bba
B bB
B c.
(The word w = acbabc is derived as follows)
S aAB a(Bba)B acbaB acba(bB ) acbabc.
Obtain the derivation tree.
Solution
S S S
a B a B a B
A A A
B a B a
b b
c
(a) S aAB (b) A Bba (c) B c
S S
a a
A B A B
a
B b B B a b c
b b
c
(d) B bB (e) B c
Solution
w = abaabaa is derived from S as
S aAS a (bS )S abaS aba (aAS )
abaa (bs)S
abaabaS
abaabaa
The derivation tree is sketched below.
S
a
A S
b
S
S a A
a b S a
a
Example 3: Given a CFG given by G = (N, T, P, S)
(1) S aSb
with N = {S}, T = {a, b}, P = .
(2) S ab
Obtain the derivation tree and the language generated L(G).
Solution
S ab i.e., ab L(G)
S aSb
aabb i.e., a 2 b2 L(G)
S aSb
aaSbb
aaabbb i.e., a 3 b3 L(G),
a 3 b3
a b
S
a b
S
a b
Solution
S
(i) S c, c L(G)
C
S
a a
(ii) S aSa aca L(G) S
c
(iii) S bSb bcb L(G)
a a
S
(iv) S aSa
abSba b b
abcba L(G) S
and so on.
Hence the language generated L(G) is given by c
where wR = reversal of w
i.e., if w a 1 a 2 KK a n 1 an
then wR a a KK a a .
n n 1 2 1
P:1.EEE
and 2.EE*E.
3.E(E)
4. E id
Obtain the derivation tree.
Solution
E E
E E E E
+ *
E E ( )
* id E id
id id E E
+
id id
id * id + id id + id) * id
Example 6: Obtain the language generated L(G) for a CFG given
1. S SS
G(N, T, P, S) with N = {S}, T{a}, P:
2. S a
Solution
S
Sa
a
S
SSS S
aS S
aa a
a
Solution
(a) The language generated is a “type-3 language” or “regular set”.
(b) S
S aS a
S as aaS aa
and so on.
Hence the language generated is
L(G) {a n | n 0}
(c) A
Aa L(G) {ww R | w {a , b} }
A aB
(d) S aS
S bS L(G) {a, b}*
S Language generated of any string of a,b
(e) S aS
S bS L(G) {a, b}* a
Sa
(f) S ab
Sa
Sb
4. Ab
Obtain the derivation tree and L(G).
Solution
S aA ab
S aS aaA aab
S aS aaS aaaA aaabA aaabb
and so on ...
The derivation tree has been shown here in fig.
Solution
S
a A
S aA ab
a A
b S
S aA abS abaA ababK K
a A
b
The derivation trees suggest ab, abab, ....
Therefore the language generated
L(G) {(ab) n | n 1}
Example 10: Obtain the production rules for CFG given the language
generated as
(a) L(G) {w | w {a, b}* , a (w) b (w)}
(b) L(G) {w | w {a, b}* , a (w) 2 b (w)}
(c) L(G) {w | w {a, b}* , a (w) 3 b (w)}
Solution
(a) S SaSbS
S
S SbSaS
(b) S SaSaSbS
S SaSbSaS
S SbSaSaS
S
(c) S SaSaSaSbS
S SaSaSbSaS
S SaSbSaSaS
S SbSaSaSaS
S
Solution
(i) Leftmost derivation:
S aB aaBB aaaBBB aaabBB aaabbB
aaabbabB aaabbabbB aaabbabbbS aaabbabbba
aaabbabb
(ii) Rightmost derivation:
S aB aaBB aaBbS aaBbbA aaaBBbba
aaabBbba aaabbSbba aaabbaBbba aaabbabbba