0% found this document useful (0 votes)
24 views

Module 3 ContextFreeGrammar

The document discusses context free grammar and provides examples of constructing grammars for various languages. It defines context free grammar and describes how to represent a grammar using the formalism G=(V,T,P,S). It then gives examples of constructing grammars for languages over various alphabets and describes derivation trees.

Uploaded by

skandapmwork2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Module 3 ContextFreeGrammar

The document discusses context free grammar and provides examples of constructing grammars for various languages. It defines context free grammar and describes how to represent a grammar using the formalism G=(V,T,P,S). It then gives examples of constructing grammars for languages over various alphabets and describes derivation trees.

Uploaded by

skandapmwork2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CONTEXT FREE GRAMMAR

Chapter 3

CONTEXT FREE GRAMMAR

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.

L={0}*={Ɛ, 0, 00, 000, …}

L is represented in the form of grammar as

It means S has two values S=Ɛ or S= 0S

In general grammar is defined as G=(V, T, P, S)

Where, V is a set of variables. (First letters)

T is a set of terminals. (Second letters)

P is Productions

S is starting variable of the grammar.

In order to construct grammar it uses four different types of productions:

i. roduction for finite set of strings, 0*={ Ɛ+0+00…} is

and 0+ ={0+00+000…} the production is

ii. Production which separate the symbols (anbn where, n={0, 1, 2,,}) is

Production which mix the symbols ((ab)n where, n is {0, 1, 2,,}) is .

iii. Production for (0+1)* is .

Example 3.1: Construct the grammar for the language L={anbn n≥0}

L={Ɛ, ab. aabb, ….}

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}

L= {Ɛ, b. bb, …abb, aabbb ….}

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}

L={Ɛ, a, b. aba, abba, bab, baab ….}

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)}

L={Ɛ, ab. ba, aabb, abab….}

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)}

L={a, aab. aba, baa, ….b, bba, bab, abb…}

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}

Describe the language according to four rules mentioned above

For i=j the productions are

For j=k the productions are

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}

Describe the language according to four rules mentioned above

Page 3.3
CONTEXT FREE GRAMMAR

For i=k, j=0 the production is

For i=0, j=k the production is

The production for the language L= {xiyjzk |i+2j=k} is

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

L= {110, 1100, 0110, 1110,1101, …

G=(V, T, P, S)

V= {B, A, S} T={0, 1}

P is production

S=S

Derivation Tree/parse Tree:

It is a tree, consisting of root node, intermediate node and leaf node. They are of two types:

i. Right Most Derivation Tree (RMD)


ii. Left Most Derivation Tree (LMD)

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

Figure 3.1: LMD and RMD for the string xxxyyy

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)*

Identifier itself is an expression, represented in the form of production as shown below

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

Figure 3.2: LMD for the string w= (a11+b0)*(b00+001).

Figure 3.3: RMD for the string w= (a11+b0)*(b00+001) .

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

Figure 3.5: Two different RMD for the production

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:

There are two reasons for the ambiguity

i. Recursive use of variables.


ii. Similar operands used in the expression.

Page 3.8
CONTEXT FREE GRAMMAR

To resolve ambiguity, two variables are used

i. Factor: Factor can be identifier or parenthesis expression written as: .


ii. Term: Term is a factor+ given as

Example 3.12: Resolve the ambiguity in the given production

Example 3.13: Resolve the ambiguity in the given production

Page 3.9

You might also like