Module 3 ContextFreeGrammar
Module 3 ContextFreeGrammar
Chapter 3
To define a language, English grammar is used such a concept is called as Context Free Language (CFL)
or Context Free Grammar (CFG). For example:
// it is a symbol.
P is Productions
ii. Production which separate the symbols (anbn where, n={0, 1, 2,,}) is
Example 3.1: Construct the grammar for the language L={anbn n≥0}
G = (V, T, P, S)
V=S, T= {a, b}
P is production
Page 3.1
CONTEXT FREE GRAMMAR
S is starting symbol = S
Example 3.2: Construct the grammar for the language L={anbm |n<m}
G=(V, T, P, S)
V={S, B} T= {a, b}
P is production
S=S
Example 3.3: Construct the grammar for palindrome over the ∑={a, b}
G=(V, T, P, S)
V=S, T={a, b}
P is production
S=S
Example 3.4: Construct the grammar for the language L={x |na(x)=nb(x)}
G= (V, T, P, S)
V=S, T= {a, b}
P is production
S=S
Example 3.5: Construct the grammar for the language L={x |na(x) nb(x)}
Page 3.2
CONTEXT FREE GRAMMAR
G= (V, T, P, S)
V={A, B, S} T={a, b}
P is production
S=S
Example 3.6: Construct the grammar for the language L= {0i1j2k |i=j or j=k}
To obtain the language L= {0i1j2k |i=j or j=k} club the above productions
G= (V, T, P, S)
V={S, B, A, C, D} T= {0, 1, 2}
P is production
S=S
Example 3.7: Construct the grammar for the language L= {xiyjzk |i+2j=k}
Page 3.3
CONTEXT FREE GRAMMAR
G= (V, T, P, S)
V= {B, A} T={x, y, z}
P is production
S=A
Example 3.8: Construct the grammar for the language L= {w|wЄ(0+1)*} with at least one occurance of
110
G=(V, T, P, S)
V= {B, A, S} T={0, 1}
P is production
S=S
It is a tree, consisting of root node, intermediate node and leaf node. They are of two types:
Page 3.4
CONTEXT FREE GRAMMAR
Right Most Derivation Tree (RMD): In RMD right most variable simplified followed by other
variables.
Left Most Derivation Tree (LMD): In LMD left most variable simplified followed by other variables.
Example 3.9: Construct LMD and RMD for the string xxxyyy using production
Identifier:
Identifier is letters followed by zero or more than zero letters or digits. We can write identifier in the form
of expression over ∑={a, b, 0, 1} as follows:
I= (a + b) (a+b+0+1)*
Expression can be
According to mathematical equation of identifier, we can write the following productions for the
identifier as
Example 3.10: Design grammar for valid expression over operator + and *. The argument of the
expressions is valid identifier over symbols a, b, 0, 1. Derive LMD and RMD for the string
w=(a11+b0)*(b00+001).
Page 3.5
CONTEXT FREE GRAMMAR
Page 3.6
CONTEXT FREE GRAMMAR
Ambiguity:
Two different trees for a LMD or RMD called ambiguity. For example, the production:
for the string a+a+a, two LMD and RMD are shown in figure 3.4 and 3.5 respectively.
Figure 3.4: Two different LMD for the production over the string a+a+a
Example 3.11: Show that the given grammar is ambiguous by using LMD and RMD for the production
over a string (()()()).
Page 3.7
CONTEXT FREE GRAMMAR
Figure 3.6: Two different LMD for the production over a string (()()()).
Figure 3.7: Two different RMD for the production over a string (()()()).
For both RMD and LMD, the tree obtained for the same production has two different trees called
Ambiguity.
Resolve Ambiguity:
Page 3.8
CONTEXT FREE GRAMMAR
Page 3.9