The document Context Free Grammars (CFG) Computer Science Engineering (CSE) Notes | EduRev is a part of the Computer Science Engineering (CSE) Course Theory of Computation.

All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

**Context Free Grammars and Languages**

As we learned in first module, according to Chomsky classification, Type- 2 languages are also called context free languages. They are generated using context free grammars. They are recognised using push down automata.

Also regular languages are a subset of context free languages.

**Part I. Context Free Grammars (CFG)**

Context free grammar, G is defined as,

G = (V,âˆ‘, P, S)

where

V is a set of non-terminals,

âˆ‘ is a set of terminals,

S is the start symbol,

P is a set of productions.

A grammar, G is context free, if productions are of the form, Î± â†’ Î²

where Î± is a single non terminal.

Every regular grammar is context free.

**Example 1:**

An example for context free grammar is shown below:

S â†’ ASA|BSB|a|b

A â†’ a

B â†’ b

where S is the start symbol.

From the definition of CFG,

G = {S, A, B}

P = {a, b}

P = {S â†’ ASA|BSB|a|b, A â†’ a, B â†’ b}

S = S

The production,

S â†’ ASA|BSB|a|b

can also be written as,

S â†’ ASA

S â†’ BSB

S â†’ a

S â†’ b

**Example 2:**

The following is an example for context free grammar.

Stmt â†’ id = Expr;

Stmt â†’ if (Expr) Stmt

Stmt â†’ if (Expr) Stmt else Stmt

Stmt â†’ while (Expr) Stmt

Stmt â†’ do Stmt while (Expr);

Stmtâ†’ { Stmts }

Stmts â†’ Stmts Stmt| Îµ

where Stmts is the start symbol.

From the definition of CFG,

G = {Stmts, stmt, Expr}

âˆ‘ = {id, =, ; ,(,), if, else, while, do}

P is the set of productions given above

S = Stmts

Note that above CFG corresponds to some statements in C programming language.

**1 Language of Context Free Grammar (Context Free Language)**

A language, L of G is a set of strings that can be derived from G.

**Example 1:**

Consider the following context free grammar,

S â†’ aA|bB

A â†’ x

B â†’ y

The language corresponding to the above CFG is {ax,by}

A string w belongs to a context free language (CFL), L, if it can be derived from the CFG associated with L.

That is,

There are two approaches to check whether a string belongs to a CFL. They are, Derivations, and Reductions.

**Derivations**

It is a mechanism to check whether a string w belongs to a context free language (CFL).**Example 1:**

Consider the following CFG,

S â†’ aSb | ab

where S is the start symbol.

Check whether the string aabb belongs to the CFL corresponding to above CFG.

Above diagram is called a derivation tree (parse tree).

Here, by performing two derivations, we got a^{2}b^{2}. So a^{2}b^{2 }belongs to the above CFL.

**Example 2:**

Consider the following CFG,

S â†’ aSa | bSb |c

where S is the start symbol.

Check whether the string abbcbba belongs to the language corresponding to the above grammar.

From the above derivation tree, the string abbcbba is derived using the given CFG. So the string abbcbba belongs to the above language.

**Example 3:**

Consider the CFG,

S â†’ aAS|a|SS

A â†’ SbA|ba

where S is the start symbol.

Check whether the string aabaa can be derived using the above grammar.**Example 4:**

Consider the CFG,

S â†’ aAS|a

A â†’ SbA|SS|ba

where S is the start symbol.

Check whether the string aabbaa belongs to this language.

Following is the derivation tree:

Thus the above string aabbaa or a^{2}b^{2}a^{2} belongs to the above CFL.

**Reductions**

This approach can also be used to check whether a string belongs to a context free language. Here, the string w is takenand an inverted tree is made. this is done by performing a number of reductions starting from the given string using the productions of the grammar.

**Example 1:**

Consider the grammar,

S â†’ aAS|a|SS

A â†’ SbA|ba

where S is the start symbol.

Check whether the string aabaa belongs to the CFL associated with the above grammar.

The reduction tree is shown below:

Thus by performing a number of reductions, we finally reached the start symbol. So the string aabaa belongs to the above CFL.

**2 CFG corresponding to a CFL****Example 1:**

Design a CFG for the language,

L = {a^{n}b^{n}| n â‰¥ 0}

At n=0, Îµ is a valid string for the above language. The production for this is,

S â†’ Îµ

Every string is of length 2n. The strings are ab, abab, aaabbb, ......... etc..

The corresponding production is,

S â†’ aSb.

Hence the grammar is,

S â†’ aSb | Îµ

where S is the start symbol.

**Example 2:**

Design a CFG for the language,

L = {a^{n}b^{2n}| n â‰¥ 0}

At n=0, Îµ is a valid string for the above language. The production for this is,

S â†’ Îµ

It can be seen that there are double the number of bâ€™s as aâ€™s. The production is,

S â†’ aSbb

So the grammar is,

S â†’ aSbb | Îµ

where S is the start symbol.

**Example 3:**

Design a CFG for the language, L over {0,1} to generate all possible strings of even length.

Consider number 0 as even. The string of length 0 is Îµ. The corresponding production is,

S â†’ Îµ

An even length is of length 2n contains one or more repetitions of 00, 01, 10 or 11. The corresponding production is,

S â†’ 00S|01S|10S|11S

Thus the grammar is,

S â†’ 00S|01S|10S|11S|Îµ

**Example 4:**

Design a CFG for the language, L over {0,1} to generate all the strings having alternate sequence of 0 and 1.

The minimum string length in L is 2. The strings are 01 or 10.

If a string begins with 01, then it should follow a repetition of 01 and terminate with either 01 or 0.

If a string begins with 10, then it should follow a repetition of 10 and terminate with either 10 or 1.

The grammar will be,

S â†’ 01|10|01A|10B

A â†’ 01A|0|01

B â†’ 10B|1|10

**Example 5:**

Write a CFG for the regular expression,

a*b(a|b)*

On analysing this regular expression, we can see that the strings start with any number of aâ€™s, followed by a â€™bâ€™ and may

end with a combination of aâ€™s and bâ€™s.

The CFG can be written as,

S â†’ AbB

A â†’ aA|Îµ

B â†’ aB|bB|Îµ

For example, using the above productions

S â‡’ AbB

â‡’ aAbB

â‡’ aAbaB

â‡’ aaAbaB

â‡’ aaAbabB

â‡’ aabab

aabab is string belonging to the above CFL.

**Example 6:**

Write a CFG which generates strings of equal number of aâ€™s and bâ€™s.

The CFG is

S â†’ aSbS|bSaS|Îµ

For example, using the above productions,

S â‡’ bSaS

â‡’ bbSaSaS

â‡’ bbaSbSaSaS

â‡’ bbaSbaSbSaSaS

â‡’ bbababaa

Note that above string contains equal number of aâ€™s and bâ€™s.

**Example 7:**

Design a CFG for the regular expression,

(a|b)*

The CFG is,

S â†’ aS

S â†’ bS

S â†’ Îµ

That is,

S â†’ aS|bS|Îµ

where S is the start symbol.

**Example 8;**

Design a CFG for the language,

L(G) = {ww^{R}|w âˆˆ {0, 1}* }

The CFG is,

S â†’ 0S0|1S1|Îµ

where S is the start symbol.

[In the above, w^{R} means reverse of string w.]

For example, using the above productions,

S â‡’ 0S0

â‡’ 00S00

â‡’ 001S100

â‡’ 001100

**Example 9**:

Design a CFG for the language,

L = {a^{n}b^{m}| n 6 â‰ m}

**Case 1:** For n>m,

The language is

L = {a^{n}b^{m}| n > m}

The CFG is,

S_{1 }â†’ AS_{1}

S_{1} â†’ aS_{1}b|Îµ

A â†’ aA|a

**Case 2:** For n<m,

The language is

L = {a^{n}b^{m}| n < m}

The CFG is,

S_{2 }â†’ S_{2}B

S_{2} â†’ aS_{2}b|Îµ

B â†’ bB|b

Combining Case 1 and Case 2, we get,

S â†’ S_{1}|S_{2}

Thus the CFG is,

S â†’ S_{1}|S_{2}2

S_{1} â†’ AS_{1}

S_{1} â†’ aS_{1}b|Îµ

A â†’ aA|a

S_{2}â†’ S_{2}B

S_{2} â†’ aS_{2}b|Îµ

B â†’ bB|b

where S is the start symbol.

**Example 10:**

Design a CFG for the language,

L = { (0^{n}1^{n}| n â‰¥ 0) âˆª (1^{n}0^{n}| n â‰¥ 0) }

We can write it as,

L = L_{1} âˆª L_{2}

**Case 1:**

Consider L1,

L_{1} = { 0^{n}1^{n}| n â‰¥ 0 }

The CFG is,

S_{1} â†’ 0S_{1}1|Îµ

**Case 2:**

Consider L2,

L_{2} = { 1^{n}0^{n}| n â‰¥ 0 }

The CFG is,

S_{2} â†’ 1S_{2}0|Îµ

Then we have L = L_{1 }âˆª L_{2}

Combining above two cases, we get the CFG as,

S â†’ S_{1}|S_{2}

Thus the complete CFG is,

S â†’ S_{1}|S_{2}

S_{1} â†’ 0S_{1}1|Îµ

S_{2} â†’ 1S_{2}0|Îµ

where S is the start symbol.

**Example 11:**

Design a CFG for the language, L

L = { a^{n}b^{2n}| n â‰¥ 0 }

The CFG is,

S â†’ aSbb|Îµ

**Example 12;**

Write a CFG for the language,

L = { a^{2n}b^{m}| n > 0, m â‰¥ 0 }

From the above we can say that L is the set of strings starting with even number of aâ€™s followed by any number of bâ€™s. There is no â€™aâ€™ in the string after first â€™bâ€™ is encountered. Also there is no â€™bâ€™ before any â€™aâ€™ in the string.

The CFG is,

S â†’ aaAB

A â†’ aaA|Îµ

B â†’ bB|Îµ

where S is the start symbol.

**Example 13:**

Write a CFG for the language,

L = {a^{n}b^{2n}c^{m}| n, m â‰¥ 0}

This means strings start with â€™aâ€™ or â€™câ€™, but not with a â€™bâ€™.

If the string starts with â€™aâ€™, then number of aâ€™s must follow bâ€™s, and the number of bâ€™s is twice than number of aâ€™s.

If the string starts with â€™câ€™, it is followed by any number of câ€™s.

There is no â€™aâ€™ after any â€™bâ€™ or any â€™câ€™.

There is no â€™bâ€™ after any â€™câ€™.

There is no â€™bâ€™ or â€™câ€™ after any â€™aâ€™.

There is no â€™câ€™ after any â€™bâ€™.

Thus the CFG is,

S â†’ AB

A â†’ aAbb|Îµ

B â†’ cB|Îµ

where S is the start symbol.

**Example 14:**

Design a CFG for the language,

L = {a^{n}b^{m}c^{2m}| n, m â‰¥ 0}

The CFG is

S â†’ AB

A â†’ aA|Îµ

B â†’ bBcc|Îµ

**Example 15:**

Design a CFG for the language, L = {wcw^{R}|w âˆˆ (a, b)*}

The CFG is

S â†’ aSa

S â†’ bSb

S â†’ c

where S is the start symbol.

Offer running on EduRev: __Apply code STAYHOME200__ to get INR 200 off on our premium plan EduRev Infinity!