Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Context Free Grammars

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
SSS
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:
VT*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.

Right-Linear Grammars and NFAs


There is a simple connection between right-linear grammars and NFAs, as
shown in the following illustration.
x
A  xB A B
x y z
A  xyzB A B

AB A B
x
Ax A
As an example of the correspondence between an NFA and a right linear
grammar, the following automaton and grammar both recognize the set of set
of strings consisting of an even number of 0’s and an even number of 1’s.
1
S 
S A
S0B
S0A 1

A0C 0 0 0 0
A1S
1
B  0S
B1C 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.

2.1.6 Conversion of Left-linear Grammar into


Right-Linear Grammar

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}

 Example 2.1.2: Construct right-and left-linear grammars for the


language L  {a n b m : n  2, m  3}.

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.

Definition of a Derivation Tree


Let G = (V, T, S, P) be a CFG. An ordered tree is a derivation tree for G iff it
has the following properties:
(i) The root of the derivation tree is S.
(ii) Each and every leaf in the tree has a label from T  {}.
(iii) Each and every interior vertex (a vertex which is no a leaf) has a
label from V.
(iv) If a vertex has label A V , and its children are labeled (from left
to right) a1 , a 2 , KK an , then P must contain a production of the
form
A  a1 , a 2 , K K an
(v) A leaf labeled  has no siblings, that is, a vertex with a child
labeled  can have no other children.

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

S  aA  aaB  aabB  aaba


The resultant of the derivation tree is the word w = aaba.
This is said to be in “Sentential Form”.

Partial Derivation Tree


In the definition of derivation tree given, if every leaf has a label from
V  T  {} it is said to be “partial derivation tree”.

Right Most/Left Most/Mixed Derivation


Consider the grammar G with production
1. S  aSS 
 

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”.

 Example 1: A grammar G which is context-free has the productions

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

 Example 2: A CFG given by productions is 


S  a,
S  aAS,
and A  bS
Obtain the derivation tree of the word w = abaabaa.

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

Derivation tree is as follows.


S

a b
S

a b
S

a b

Language generated L(G)  {a n bn | n 1}.

 Example 4: Given a CFG G = (N, T, P, S)


 (1) S  aSa 
 
with N = {S}, T = {a, b, c} and P   (2) S  bSb.
 (3) S  c 
 
Obtain the derivation tree and language generated L(G).

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

L(G)  {wcw R | w {a, b}* }

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

 Example 5: Given G = (N, T, P, S) with N =


{E}, S = E, T = {id, +, *, c}

P:1.EEE
and 2.EE*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
Sa

a
S

SSS S
 aS S
 aa a
a

S  SS  aS  aSS  aaS  aaa and so on....


Therefore the language generated is
L(G)  {a n | n 1}

 Example 7: Obtain the language generated by each of the following


production rules.

(a) A  a (b) S  aS (c) A  a


A  aB S  A  aB
A  A 
(d) A  aS (e) S  aS (f) S  ab
S  bS S  bS S  bs
S  Sa Sa
Sb

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 
Aa 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
Sa
(f) S  ab

Sa
Sb

 Example 8: Given a CFG G = (N, T, P, S)


1. S  aS 
2. S  aA 

with N = {S, A}, T = {a, b} and P 3. A  bA 
 

 4. Ab 
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.

The language generated is


L(G)  {a n b m n  1, m 1}
 Example 9: Given a CFG with
1. S  aA 
 

P  2. A  bS . Obtain the derivation tree and L(G).


 3. A  b 


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 

 Example 11: Given a grammar G with production rules


S  aB
S  bA
A  aS
A  bAA
Aa
B  bS
B  aBB
Bb
Obtain the (i) leftmost derivation, and (ii) rightmost derivation for the
string “aaabbabbba”.

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

You might also like