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

Grammar in Automata

The document discusses context free grammars. It defines a context free grammar as a 4-tuple (V, T, P, S) where V are non-terminal symbols, T are terminal symbols, P are production rules, and S is the start symbol. Context free grammars are called "context free" because production rules can be applied regardless of context. Context free grammars are used to define programming languages and parse programs into syntax trees. The language generated by a context free grammar is called a context free language.

Uploaded by

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

Grammar in Automata

The document discusses context free grammars. It defines a context free grammar as a 4-tuple (V, T, P, S) where V are non-terminal symbols, T are terminal symbols, P are production rules, and S is the start symbol. Context free grammars are called "context free" because production rules can be applied regardless of context. Context free grammars are used to define programming languages and parse programs into syntax trees. The language generated by a context free grammar is called a context free language.

Uploaded by

HARSH RAJ JHA
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

Grammar in Automata-

 Standard way of representing the language is called grammar in Automata.


 Grammar contains set of production rules which makes the strings of language.
 The set of all possible strings which can be derived from grammar is known as
Language of that grammar.

Real Life Example of Grammar

Grammar is just like the same as English grammar. If the sentence is correct grammatically
then that sentence will be the part of grammar otherwise not. Example
 “I am going to school”. It is a valid example of grammar.
 I going am to school. It is not a valid example of grammar.
Elements of Grammar

To get better understanding of Grammar, we first understand the elements of grammar.


Elements of a grammar depends on types of grammar. The basic elements of grammar are
given below. Any Grammar can be represented by 4 tuples:

G = (V, T, P, S) Where V, T, P, S are the elements of Grammar (G).


1. V = Variables (Non-Terminal)
 Finite number of non-empty set Represented by capital symbols. A, B, C
 Not a part of string which makes after production rule.

2. T = Terminal
 Finite set of Alphabets (Σ), Represented by small letters, i.e. a,b,c.
 All variables are replaced with non-terminals through production rules.
 Terminals are part of string which makes after production rule.

3. P = Production Rules
 (Finite set of non-empty rules to makes a string of Language)
 i.e. P = { S → aSb , S → bSa , S → ∈ }

4. S = Start symbol
Start symbol is used to start the production rule represented by S.

Note: Above grammar generates the strings through production rules having equal number
of a’s and b’s. As given Below
L = {ab, aabb, aaabbb, …….}
Grammar Constituents-
A Grammar is mainly composed of two basic elements-

1. Terminal symbols
2. Non-terminal symbols

1. Terminal Symbols-

 Terminal symbols are those which are the constituents of the sentence generated using a grammar.
 Terminal symbols are denoted by using small case letters such as a, b, c etc.

2. Non-Terminal Symbols-

 Non-Terminal symbols are those which take part in the generation of the sentence but are not part of
it.
 Non-Terminal symbols are also called as auxiliary symbols or variables.
 Non-Terminal symbols are denoted by using capital letters such as A, B, C etc.

Examples of Grammar-
Example-01:

Consider a grammar G = (V , T , P , S) where-


 V = { S } // Set of Non-Terminal symbols
 T = { a , b } // Set of Terminal symbols
 P = { S → aSbS , S → bSaS , S → ∈ } // Set of production rules
 S = { S } // Start symbol

This grammar generates the strings having equal number of a’s and b’s
Production Rules and Strings
As we Know every Grammar generates a specific language. And each Grammar holds some
rules which are called Production Rules. By using production rules strings are generated. Let
explain both Production Rules and Strings
What is string?
String is a part of language. Grammar produces strings through its production rules. String is
generally denoted as w and length of a string is denoted as |w|. There may be many strings,
a language with Empty string represented as ebsilon (ε) or lamda (λ).
Suppose a language with four strings is given below
L(G) = {a, bb, abc, b}
Representation: (w|w€ L)
Number of Strings
If Σ{a, b}
Number of Strings (of length 2) will be
a a
a b
b a
b b
Length of String |w| = 2
Possible Number of Strings = 4
Conclusion: For alphabet {a, b} with length n, number of strings can be generated = 2n.
Production Rule
Production rule is denoted by P. Examples of production rules is given below
P: S → aSb / ∈
Then above production rule can be written as
 S → aSb
 S→∈
The Production Rule (P: S → aSb / ∈) can generate the following
∈, ab, aabb, aaabbb…….
So, all above symbols are strings which are generated through production rules.
The language with grammar, generates the strings through production rules is represented
as below
L(G) = { ∈, ab, aabb, aaabbb…….} = L = { anbn , n>=0 }
Types of Grammars-
Grammars are classified on different basis as-

Equivalent Grammars-

Two grammars are said to be equivalent if they generate the same languages.

Example-

Consider the following two grammars-

Grammar G1-
S → aSb / ∈

Grammar G2-
S → aAb / ∈
A → aAb / ∈

Both these grammars generate the same language given as-


L = { anbn , n>=0 }

Thus, L(G1) = L(G2)


Since both the grammars generate the same language, therefore they are equivalent.

∴ G1 ≡ G2
Language Of Grammar-
Language of Grammar is the set of all strings that can be generated from that grammar.

 If the language consists of finite number of strings, then it is called as a Finite language.
 If the language consists of infinite number of strings, then it is called as an Infinite language.

Example-01:

Consider a grammar G = (V , T , P , S) where-


 V={S}
 T={a,b}
 P = { S → aSbS , S → bSaS , S → ∈ }
 S={S}

This grammar generates the strings having equal number of a’s and b’s.

So, Language of this grammar is-

L(G) = { ∈ , ab , ba , aabb , bbaa , abab , baba , …… }

 This language consists of infinite number of strings.


 Therefore, language of the grammar is infinite.

Example-02:

Consider a grammar G = (V , T , P , S) where-


 V={S,A,B,C}
 T={a,b,c}
 P = { S → ABC , A → a , B → b , C → c }
 S={S}

This grammar generates only one string “abc”.

So, Language of this grammar is-

L(G) = { abc }

 This language consists of finite number of strings.


 Therefore, language of the grammar is finite.
Important Concept-
 For any given grammar, the language generated by it is always unique.
 For any given language, we may have more than one grammar generating that language.

Example-
Consider the following two grammars-

Grammar G1-

S → AB
A→a
B→b
The language generated by this grammar is-
L(G1) = { ab }

Grammar G2-

S → AB
A→∈
B → ab
The language generated by this grammar is-
L(G2) = { ab }

Here,
 Both the grammars generate a unique language.
 But given a language L(G) = { ab }, we have two different grammars generating that language.
This justifies the above concept.
Context Free Grammar-
A context Free Grammar (CFG) is a 4-tuple such that-
G = (V , T , P , S)
where-
 V = Finite non-empty set of variables / non-terminal symbols
 T = Finite set of terminal symbols
 P = Finite non-empty set of production rules of the form A → α where A ∈ V and α ∈ (V ∪ T)*
 S = Start symbol

Why Context Free Grammar Is Called So?


Context Free Grammar provides no mechanism to restrict the usage of the production rule
A → α within some specific context unlike other types of grammars.
That is why it is called as “Context Free” Grammar.

Example-01:

Consider a grammar G = (V , T , P , S) where-


 V={S}
 T={a,b}
 P = { S → aSbS , S → bSaS , S → ∈ }
 S={S}

 This grammar is an example of a context free grammar.


 It generates the strings having equal number of a’s and b’s.

Example-02:

Consider a grammar G = (V , T , P , S) where-


 V={S}
 T={(,)}
 P = { S → SS , S → (S) , S → ∈ }
 S={S}

 This grammar is an example of a context free grammar.


 It generates the strings of balanced parenthesis.

Applications-

Context Free Grammar (CFG) is of great practical importance. It is used for following purposes-

 For defining programming languages


 For parsing the program by constructing syntax tree
 For translation of programming languages
 For describing arithmetic expressions
 For construction of compilers
Context Free Language-
The language generated using Context Free Grammar is called as Context Free Language.

Properties-

 The context free languages are closed under union.


 The context free languages are closed under concatenation.
 The context free languages are closed under kleen closure.
 The context free languages are not closed under intersection and complement.
 The family of regular language is a proper subset of the family of context free language.
 Each Context Free Language is accepted by a Pushdown automaton.

Remember
If L1 and L2 are two context free languages, then-
 L1 ∪ L2 is also a context free language.
 L1.L2 is also a context free language.
 L1* and L2* are also context free languages.
 L1 ∩ L2 is not a context free language.
 L1′ and L2′ are not context free languages.

Ambiguity in Context Free Grammar-


A grammar is said to be ambiguous if for a given string generated by the grammar, there exists-
 more than one leftmost derivation
 or more than one rightmost derivation
 or more than one parse tree (or derivation tree).
Difference Between Ambiguous and Unambiguous Grammar-
Some of the important differences between ambiguous grammar and unambiguous grammar are-

S.No. Ambiguous Grammar Unambiguous Grammar

A grammar is said to be ambiguous A grammar is said to be


if for at least one string generated unambiguous if for all the strings
by it, it produces more than one- generated by it, it produces exactly
one-
1  parse tree  parse tree
 or derivation tree  or derivation tree
 or syntax tree  or syntax tree
 or leftmost derivation  or leftmost derivation
 or rightmost derivation  or rightmost derivation

For unambiguous grammar,


For ambiguous grammar, leftmost leftmost derivation and rightmost
2 derivation and rightmost derivation derivation represents the same
represents different parse trees. parse tree.

Ambiguous grammar contains less Unambiguous grammar contains


3
number of non-terminals. more number of non-terminals.

For ambiguous grammar, length of For unambiguous grammar, length


4
parse tree is less. of parse tree is large.

Ambiguous grammar is faster than Unambiguous grammar is slower


5 unambiguous grammar in the than ambiguous grammar in the
derivation of a tree. derivation of a tree.

Example- Example-

E→E+T/T

6 E → E + E / E x E / id T→TxF/F

(Ambiguous Grammar) F → id

(Unambiguous Grammar)
Derivation In Automata

The process of deriving a string from grammar is called as derivation in Automata.


The Representation of derivation in the form of tree is called as a derivation tree.

Purpose of Derivation
Derivation tree is useful, when a string and grammar (production rules) are given and we
have to check whether the string belongs to grammar or not?
Example of Derivation
As the following production rules are given
S → xB
B → xS| ε
And suppose a string W= xxx.
If the given string is derived from given production rules then derivation and its tree is possible.

Types of Derivation
There are four basic types derivations in theory of Automata
 Leftmost derivation
 Rightmost derivation
 Parse tree
 Syntax tree

But the first two types of derivations are mostly used.


Remember
 Leftmost and Rightmost derivation, parse and syntax tree will be similar for unambiguous
grammars.
 Leftmost and Rightmost derivation, parse and syntax tree will not be similar For ambiguous
grammars,.
1. Leftmost Derivation-
 The process of deriving a string by expanding the leftmost non-terminal at each step is called as
leftmost derivation.
 The geometrical representation of leftmost derivation is called as a leftmost derivation tree.

Example of Leftmost Derivation


Consider the following production rules for the grammar (G)
S → xB / yA
S → xS / yAA / x
B → yS / xBB / y
Consider a string W = xxxyyxyyyx

Let us derive the given string (W= xxxyyxyyyx) by using given production rules of grammar.
And will also draw the derivation tree of each step as given below
Leftmost Derivation and Tree
Let us see derivation through production rules and their tree as given below
Step 01: S → xB (W=xB)

Step 02: W= xxBB (Using B → xBB)

Step 03: W= xxxBBB (Using B → xBB)

Step 04: W= xxxyBB (Using B → y)


Step 05: W= xxxyyB (Using B → y)

Step 06: W= xxxyyxBB (Using B → xBB)

Step 07: W= xxxyyxyB (Using B → y)


Step 08: W= xxxyyxyyS (Using B → yS)

Step 09: W= xxxyyxyyyA (Using S → yA)

Step 10: W= xxxyyxyyyx (Using A → x)


Hence proved the given string (W= xxxyyxyyyx) is derived through leftmost derivation. So,
given string belongs to Given Grammar (G).

Example-

Consider the following grammar-


S → aB / bA
A → aS / bAA / a
B → bS / aBB / b
(Unambiguous Grammar)

Let us consider a string w = aaabbabbba


Now, let us derive the string w using leftmost derivation.

Leftmost Derivation-

S → aB
→ aaBB (Using B → aBB)
→ aaaBBB (Using B → aBB)
→ aaabBB (Using B → b)
→ aaabbB (Using B → b)
→ aaabbaBB (Using B → aBB)
→ aaabbabB (Using B → b)
→ aaabbabbS (Using B → bS)
→ aaabbabbbA (Using S → bA)
→ aaabbabbba (Using A → a)

2. Rightmost Derivation-
 The process of deriving a string by expanding the rightmost non-terminal at each step is called as
rightmost derivation.
 The geometrical representation of rightmost derivation is called as a rightmost derivation tree.

Example of Rightmost Derivation


Consider the following production rules of a Grammar (G)
S → xB / yA
S → xS / yAA / x
B → yS / xBB / y
Consider a string W = xxxyyxyyyx

Let us derive the given string (W= xxxyyxyyyx) by using given production rules of grammar.
And will also draw the derivation tree of each step as given below
Rightmost Derivation and Tree
Let us see derivation through production rules and their tree as given below
Step 01: S → xB (Using S → xB)
Step 02: W= xxBB (Using B → xBB)

Step 03: W= xxBxBB (Using B → xBB)

Step 04: W= xxBxByS (Using B → yS)

Step 05: W= xxBxByyA (Using S → yA)


Step 06: W= xxBxByyx (Using A → x)

Step 07: W= xxBxyyyx (Using B → y)


Step 08: W= xxxBBxyyyx (Using B → xBB)

Step 09: W= xxxByxyyyx (Using B → y)


Step 10: W= xxxyyxyyyx (Using B → y)

Hence proved the given String (W=xxxyyxyyyx) is derived through Rightmost derivation. So,
given string belongs to Given Grammar (G).
Example-

Consider the following grammar-

S → aB / bA

A → aS / bAA / a

B → bS / aBB / b

(Unambiguous Grammar)

Let us consider a string w = aaabbabbba

Now, let us derive the string w using rightmost derivation.

Rightmost Derivation-

S → aB

→ aaBB (Using B → aBB)

→ aaBaBB (Using B → aBB)

→ aaBaBbS (Using B → bS)

→ aaBaBbbA (Using S → bA)

→ aaBaBbba (Using A → a)

→ aaBabbba (Using B → b)

→ aaaBBabbba (Using B → aBB)

→ aaaBbabbba (Using B → b)

→ aaabbabbba (Using B → b)
NOTES

 For unambiguous grammars, Leftmost derivation and Rightmost derivation represents the
same parse tree.
 For ambiguous grammars, Leftmost derivation and Rightmost derivation represents different
parse trees.

Here,

 The given grammar was unambiguous.


 That is why, leftmost derivation and rightmost derivation represents the same parse tree.

Leftmost Derivation Tree = Rightmost Derivation Tree


Parse Tree in Automata

The process of deriving a string from given grammar is called as derivation.


The geometrical representation of a derivation is known as a derivation tree or parse tree.
In Parse Tree, the deepest sub-tree from leftmost is traversed first by following the rule of In-
order traversal. In this way the original input string is obtained but
 All leaf nodes must be terminals.
 All interior nodes must be Non-terminals.
Example of Parse Tree
Suppose the following Production rules of a Grammar (G)
S→S+S|S*S
S → x|y|z
and Input is (x * y + Z)
Step 1

Step 2

Step 3

Step 4
Step 5

Properties Of Parse Tree-


 Root node of a parse tree is the start symbol of the grammar.
 Each leaf node of a parse tree represents a terminal symbol.
 Each interior node of a parse tree represents a non-terminal symbol.
 Parse tree is independent of the order in which the productions are used during derivations.

Yield Of Parse Tree-


 Concatenating the leaves of a parse tree from the left produces a string of terminals.
 This string of terminals is called as yield of a parse tree.
Syntax Tree In Automata
When you construct a parse tree then it may contains more details than actually required. So,
it is very difficult to compiler to execute unnecessary information’s. That’s why a syntax tree is
used which holds just necessary information’s.
Example of Syntax Tree
Consider the following Parse tree

As above parse tree is providing the string is “id + id * id ”. Now let us eliminate the extra
information from given parse tree to get the same string.

Hence, A String “id + id * id” is derived by elimination of unnecessary information’s. It is called


syntax tree.

PRACTICE PROBLEMS BASED ON DERIVATIONS AND


PARSE TREE-
Problem-01:

Consider the grammar-

S → bB / aA

A → b / bS / aAA

B → a / aS / bBB

For the string w = bbaababa, find-

1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree

Solution-
1. Leftmost Derivation-

S → bB

→ bbBB (Using B → bBB)

→ bbaB (Using B → a)

→ bbaaS (Using B → aS)

→ bbaabB (Using S → bB)

→ bbaabaS (Using B → aS)

→ bbaababB (Using S → bB)

→ bbaababa (Using B → a)

2. Rightmost Derivation-

S → bB

→ bbBB (Using B → bBB)

→ bbBaS (Using B → aS)

→ bbBabB (Using S → bB)

→ bbBabaS (Using B → aS)

→ bbBababB (Using S → bB)

→ bbBababa (Using B → a)

→ bbaababa (Using B → a)
3. Parse Tree-

 Whether we consider the leftmost derivation or rightmost derivation, we get the above parse tree.
 The reason is given grammar is unambiguous.

Problem-02:

Consider the grammar-

S → A1B

A → 0A / ∈

B → 0B / 1B / ∈

For the string w = 00101, find-

1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree

Solution-

1. Leftmost Derivation-

S → A1B

→ 0A1B (Using A → 0A)

→ 00A1B (Using A → 0A)

→ 001B (Using A → ∈)

→ 0010B (Using B → 0B)

→ 00101B (Using B → 1B)

→ 00101 (Using B → ∈)
2. Rightmost Derivation-

S → A1B

→ A10B (Using B → 0B)

→ A101B (Using B → 1B)

→ A101 (Using B → ∈)

→ 0A101 (Using A → 0A)

→ 00A101 (Using A → 0A)

→ 00101 (Using A → ∈)

3. Parse Tree-

 Whether we consider the leftmost derivation or rightmost derivation, we get the above parse tree.
 The reason is given grammar is unambiguous.
Grammar Ambiguity-

 There exists no algorithm to check whether any given grammar is ambiguous or not.
 This general decision problem is undecidable-

“Whether a grammar is ambiguous or not?”

 This is because it can be shown that this problem is equivalent to Post Correspondence Problem.

General Approach To Check Grammar Ambiguity-


To check whether a given grammar is ambiguous or not, we follow the following steps-

Step-01:

We try finding a string from the Language of Grammar such that for the string there exists more
than one-

 parse tree
 or derivation tree
 or syntax tree
 or leftmost derivation
 or rightmost derivation

Step-02:

If there exists at least one such string, then the grammar is ambiguous otherwise unambiguous.

On the basis of number of derivation trees, ambiguity Grammar is classified into two
terms. One is ambiguous grammar and other is Unambiguous grammar.

1. Ambiguous Grammar
A grammar is said to ambiguous grammar if for any string generated by it produces more
than one
 Parse tree
 Or syntax tree
 Or leftmost derivation
 Or rightmost derivation
Examples of Ambiguous Grammar
Example 01
Check whether the following grammar is ambiguous or not for string w = ab

S→A/B
A → aAb / ab
B → abB / ∈
Solution
Now we draw more than one parse trees to get string w = ab.

As original string (w =ab) can derived through two different parse trees. So, the given
grammar is ambiguous.
Example-02
Check whether the following grammar is ambiguous or not for string w = aabbccdd
S → AB / C
A → aAb / ab
B → cBd / cd
C → aCd / aDd
D → bDc / bc
Solution
Now we draw more than one parse trees to get string w = aabbccdd.
As original string (w =aabbccdd) can derived through two different parse trees. So, the given
grammar is ambiguous.

2. Unambiguous Grammar
A grammar is said to unambiguous grammar if for every string generated by it produces
exactly the one
 Parse tree
 Or syntax tree
 Or leftmost derivation
 Or rightmost derivation
Note: So, If we try to derive the more than one trees of unambiguous grammar than all trees
will be similar
Examples of Unambiguous Grammar
Example 01
For string “aab” the following grammar is unambiguous
S → AB
A → Aa | a
B→b
Solution
Let’s draw leftmost and rightmost derivations of above grammar to get the string “aab”.
Because all parse trees, syntax tress, left or right derivations will be similar for above grammar
of string “aab”. So, above grammar is unambiguous.

Example 02
For string “id+id*id” the following grammar is unambiguous
 E→E+T
 E→T
 T→T*F
 T→F
 F → id
Solution
Because all parse trees, syntax tress, left or right derivations will be similar for above grammar
of string “id+id*id”. As given below

Removal of Ambiguity from CFG


Conversion from ambiguous grammar into an unambiguous grammar is not always
possible.
There are some methods which are used for the removal of ambiguity from CFG. Methods
are given below
 By fixing the grammar
 By adding the precedence rules
 By adding grouping rules
 By using semantics

PROBLEMS BASED ON CHECKING WHETHER


GRAMMAR IS AMBIGUOUS-

Problem-01:

Check whether the given grammar is ambiguous or not-

S → SS

S→a

S→b

Solution-

Let us consider a string w generated by the given grammar-

w = abba

Now, let us draw parse trees for this string w.


Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

Problem-02:

Check whether the given grammar is ambiguous or not-

S→A/B

A → aAb / ab

B → abB / ∈

Solution-

Let us consider a string w generated by the given grammar-

w = ab

Now, let us draw parse trees for this string w.


Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

Problem-03:
Check whether the given grammar is ambiguous or not-

S → AB / C

A → aAb / ab

B → cBd / cd

C → aCd / aDd

D → bDc / bc

Solution-
Let us consider a string w generated by the given grammar-

w = aabbccdd

Now, let us draw parse trees for this string w.


Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

Problem-04:
Check whether the given grammar is ambiguous or not-

S → AB / aaB

A → a / Aa

B→b

Solution-
Let us consider a string w generated by the given grammar-

w = aab

Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-05:
Check whether the given grammar is ambiguous or not-

S → a / abSb / aAb

A → bS / aAAb

Solution-
Let us consider a string w generated by the given grammar-

w = abababb

Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

Problem-06:

Check whether the given grammar is ambiguous or not-

E→E+T/T

T→T*F/F

F → id
Solution-
 There exists no string belonging to the language of grammar which has more than one parse tree.
 Since a unique parse tree exists for all the strings, therefore the given grammar is unambiguous.

Problem-07:
Check whether the given grammar is ambiguous or not-

S → aSbS / bSaS / ∈

Solution-
Let us consider a string w generated by the given grammar-

w = abab

Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

Problem-08:
Check whether the given grammar is ambiguous or not-

R → R + R / R . R / R* / a / b

Solution-

Let us consider a string w generated by the given grammar-

w = ab + a

Now, let us draw parse trees for this string w.

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Normal Forms-

Normalization is performed in order to standardize the grammar.

 By reducing the grammar, the grammar gets minimized but does not gets standardized.
 This is because the RHS of productions have no specific format.
 In order to standardize the grammar, normalization is performed using normal forms.

Types of Normal Forms-


The most frequently used normal forms are-

1. Chomsky Normal Form (CNF)


2. Greibach Normal Form (GNF)

Chomsky Normal Form-

A context free grammar is said to be in chomsky normal form (CNF) if all its productions are of
the form-

A → BC or A → a

where A, B, C are non-terminals and a is a terminal.

From here, we infer-

 To be in CNF, all the productions must derive either two non-terminals or a single terminal.
 CNF restricts the number of symbols on the right side of a production to be two.
 The two symbols must be non-terminals or a single terminal.

Example-

S → AB

A→a

B→b

This context free grammar is in chomsky normal form.

Steps-

The following steps are followed to standardize the grammar using CNF-

Rule-01:

Reduce the grammar completely by-

 Eliminating ∈ productions
 Eliminating unit productions
 Eliminating useless productions

Rule-02:

Replace each production of the form A → B1B2B3….Bn where n > 2 with A → B1C where C →
B2B3….Bn.

Repeat this step for all the productions having more than two variables on RHS.

Rule-03:
Replace each production of the form A → aB with A → XB and X → a.

Repeat this step for all the productions having the form A → aB.

PRACTICE PROBLEMS BASED ON CHOMSKY NORMAL


FORM-

Problem-01:

Convert the given grammar to CNF-

S → aAD

A → aB / bAB

B→b

D→d

Solution-

Step-01:

The given grammar is already completely reduced.

Step-02:

The productions already in chomsky normal form are-

B → b ………..(1)

D → d ………..(2)

These productions will remain as they are.


The productions not in chomsky normal form are-

S → aAD ………..(3)

A → aB / bAB ………..(4)

We will convert these productions in chomsky normal form.

Step-03:

Replace the terminal symbols a and b by new variables C a and Cb.

This is done by introducing the following two new productions in the grammar-

Ca → a ………..(5)

Cb → b ………..(6)

Now, the productions (3) and (4) modifies to-

S → CaAD ………..(7)

A → CaB / CbAB ………..(8)

Step-04:

Replace AD and AB by new variables CAD and CAB respectively.

This is done by introducing the following two new productions in the grammar-

CAD → AD ………..(9)

CAB → AB ………..(10)

Now, the productions (7) and (8) modifies to-


S → CaCAD ………..(11)

A → CaB / CbCAB ………..(12)

Step-05:

From (1), (2), (5), (6), (9), (10), (11) and (12), the resultant grammar is-

S → CaCAD

A → CaB / CbCAB

B→b

D→d

Ca → a

Cb → b

CAD → AD

CAB → AB

This grammar is in chomsky normal form.

Problem-02:

Convert the given grammar to CNF-

S → 1A / 0B

A → 1AA / 0S / 0

B → 0BB / 1S / 1

Solution-
Step-01:

The given grammar is already completely reduced.

Step-02:

The productions already in chomsky normal form are-

A → 0 ………..(1)

B → 1 ………..(2)

These productions will remain as they are.

The productions not in chomsky normal form are-

S → 1A / 0B ………..(3)

A → 1AA / 0S ………..(4)

B → 0BB / 1S ………..(5)

We will convert these productions in chomsky normal form.

Step-03:

Replace the terminal symbols 0 and 1 by new variables C and D.

This is done by introducing the following two new productions in the grammar-

C → 0 ………..(6)

D → 1 ………..(7)

Now, the productions (3), (4) and (5) modifies to-


S → DA / CB ………..(8)

A → DAA / CS ………..(9)

B → CBB / DS ………..(10)

Step-04:

Out of (8), (9) and (10), the productions already in Chomsky Normal Form are-

S → DA / CB ………..(11)

A → CS ………..(12)

B → DS ………..(13)

These productions will remain as they are.

The productions not in chomsky normal form are-

A → DAA ………..(14)

B → CBB ………..(15)

We will convert these productions in Chomsky Normal Form.

Step-05:

Replace AA and BB by new variables E and F respectively.

This is done by introducing the following two new productions in the grammar-

E → AA ………..(16)

F → BB ………..(17)

Now, the productions (14) and (15) modifies to-

A → DE ………..(18)
B → CF ………..(19)

Step-06:

From (1), (2), (6), (7), (11), (12), (13), (16), (17), (18) and (19), the resultant grammar is-

S → DA / CB

A → CS / DE / 0

B → DS / CF / 1

C→0

D→1

E → AA

F → BB

This grammar is in chomsky normal form.


CFG Simplification
In a CFG, it may happen that all the production rules and symbols are not needed for the derivation of
strings. Besides, there may be some null productions and unit productions. Elimination of these
productions and symbols is called simplification of CFGs. Simplification essentially comprises of
the following steps −

 Reduction of CFG
 Removal of Unit Productions
 Removal of Null Productions

Reduction of CFG
CFGs are reduced in two phases −

Phase 1 − Derivation of an equivalent grammar, G’, from the CFG, G, such that each variable
derives some terminal string.

Derivation Procedure −

Step 1 − Include all symbols, W1, that derive some terminal and initialize i=1.

Step 2 − Include all symbols, Wi+1, that derive Wi.

Step 3 − Increment i and repeat Step 2, until Wi+1 = Wi.

Step 4 − Include all production rules that have Wi in it.

Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that each symbol
appears in a sentential form.

Derivation Procedure −

Step 1 − Include the start symbol in Y1 and initialize i = 1.

Step 2 − Include all symbols, Yi+1, that can be derived from Yi and include all production rules that
have been applied.

Step 3 − Increment i and repeat Step 2, until Yi+1 = Yi.

Problem

Find a reduced grammar equivalent to the grammar G, having production rules,

P:

S → AC | B,

A → a,

C → c | BC,
E → aA | e

Solution

Phase 1 −

T = { a, c, e }

W1 = { A, C, E } from rules A → a, C → c and E → aA

W2 = { A, C, E } U { S } from rule S → AC

W3 = { A, C, E, S } U ∅

Since W2 = W3, we can derive G’ as −

G’ = { { A, C, E, S }, { a, c, e }, P, {S}}

where P: S → AC, A → a, C → c , E → aA | e

Phase 2 −

Y1 = { S }

Y2 = { S, A, C } from rule S → AC

Y3 = { S, A, C, a, c } from rules A → a and C → c

Y4 = { S, A, C, a, c }

Since Y3 = Y4, we can derive G” as −

G” = { { A, C, S }, { a, c }, P, {S}}

where P: S → AC, A → a, C → c

Removal of Unit Productions


Any production rule in the form A → B where A, B ∈ Non-terminal is called unit production..

Removal Procedure −

Step 1 − To remove A → B, add production A → x to the grammar rule whenever B → x occurs in


the grammar. [x ∈ Terminal, x can be Null]

Step 2 − Delete A → B from the grammar.

Step 3 − Repeat from step 1 until all unit productions are removed.

Problem

Remove unit production from the following −


S → XY, X → a, Y → Z | b, Z → M, M → N, N → a

Solution −

There are 3 unit productions in the grammar −

Y → Z, Z → M, and M → N

At first, we will remove M → N.

As N → a, we add M → a, and M → N is removed.

The production set becomes

S → XY, X → a, Y → Z | b, Z → M, M → a, N → a

Now we will remove Z → M.

As M → a, we add Z→ a, and Z → M is removed.

The production set becomes

S → XY, X → a, Y → Z | b, Z → a, M → a, N → a

Now we will remove Y → Z.

As Z → a, we add Y→ a, and Y → Z is removed.

The production set becomes

S → XY, X → a, Y → a | b, Z → a, M → a, N → a

Now Z, M, and N are unreachable, hence we can remove those.

The final CFG is unit production free −

S → XY, X → a, Y → a | b

Removal of Null Productions


In a CFG, a non-terminal symbol ‘A’ is a nullable variable if there is a production A → ε or there is a
derivation that starts at A and finally ends up with

ε: A → .......… → ε

Removal Procedure

Step 1 − Find out nullable non-terminal variables which derive ε.

Step 2 − For each production A → a, construct all productions A → x where x is obtained from ‘a’
by removing one or multiple non-terminals from Step 1.

Step 3 − Combine the original productions with the result of step 2 and remove ε - productions.
Problem

Remove null production from the following −

S → ASA | aB | b, A → B, B → b | ∈

Solution −

There are two nullable variables − A and B

At first, we will remove B → ε.

After removing B → ε, the production set becomes −

S→ASA | aB | b | a, A ε B| b | &epsilon, B → b

Now we will remove A → ε.

After removing A → ε, the production set becomes −

S→ASA | aB | b | a | SA | AS | S, A → B| b, B → b

This is the final production set without null transition.

Chomsky Normal Form:


A CFG is in Chomsky Normal Form if the Productions are in the following forms −

 A→a
 A → BC
 S→ε

where A, B, and C are non-terminals and a is terminal.

Algorithm to Convert into Chomsky Normal Form −


Step 1 − If the start symbol S occurs on some right side, create a new start symbol S’ and a new
production S’→ S.

Step 2 − Remove Null productions. (Using the Null production removal algorithm discussed earlier)

Step 3 − Remove unit productions. (Using the Unit production removal algorithm discussed earlier)

Step 4 − Replace each production A → B1…Bn where n > 2 with A → B1C where C → B2 …Bn.
Repeat this step for all productions having two or more symbols in the right side.

Step 5 − If the right side of any production is in the form A → aB where a is a terminal and A, B are
non-terminal, then the production is replaced by A → XB and X → a. Repeat this step for every
production which is in the form A → aB.
Problem

Convert the following CFG into CNF

S → ASA | aB,

A → B | S,

B→b|ε

Solution

(1) Since S appears in R.H.S, we add a new state S0 and S0→S is added to the production set and it
becomes −

S0→S,

S→ ASA | aB,

A → B | S,

B→b|∈

(2) Now we will remove the null productions −

B → ∈ and A → ∈

After removing B → ε, the production set becomes −

S0→S,

S→ ASA | aB | a,

A → B | S | ∈,

B→b

After removing A → ∈, the production set becomes −

S0→S,

S→ ASA | aB | a | AS | SA | S,

A → B | S, B → b

(3) Now we will remove the unit productions.

After removing S → S, the production set becomes −

S0→S,

S→ ASA | aB | a | AS | SA,
A → B | S,

B→b

After removing S0→ S, the production set becomes −

S0→ ASA | aB | a | AS | SA,

S→ ASA | aB | a | AS | SA

A → B | S, B → b

After removing A→ B, the production set becomes −

S0 → ASA | aB | a | AS | SA,

S→ ASA | aB | a | AS | SA

A→S|b

B→b

After removing A→ S, the production set becomes −

S0 → ASA | aB | a | AS | SA,

S→ ASA | aB | a | AS | SA

A → b |ASA | aB | a | AS | SA,

B→b

(4) Now we will find out more than two variables in the R.H.S

Here, S0→ ASA,

S → ASA,

A→ ASA violates two Non-terminals in R.H.S.

Hence we will apply step 4 and step 5 to get the following final production set which is in CNF −

S0→ AX | aB | a | AS | SA

S→ AX | aB | a | AS | SA

A → b |AX | aB | a | AS | SA

B→b

X → SA

(5) We have to change the productions S0→ aB, S→ aB, A→ aB


And the final production set becomes −

S0→ AX | YB | a | AS | SA

S→ AX | YB | a | AS | SA

A → b A → b |AX | YB | a | AS | SA

B→b

X → SA

Y→a

Example 1:
What is the Chomsky normal form for the following CFG?
CFG
S -> a B
CFG IN Chomsky normal form
S -> H0 H1
H0 -> a
H1 -> B
Example 2:
Convert the given CFG into Chomsky normal form
CFG
S -> a B c C
CFG IN Chomsky normal form
S -> H1 H0
H0 -> C
H1 -> H3 H2
H2 -> c
H3 -> H4 H5
H4 -> a
H5 -> B
Example 3:
Convert the following CFG into Chomsky normal form
CFG
S -> a B B B | b A A A
A -> a | A s | b B B
B -> b| b S |A a a
CFG IN Chomsky normal form
S -> H0 B | H1 A
A -> a | A H2 | H3 B
B -> H5 H4
H0 -> H6 B
H1 -> H7 A
H2 -> s
H3 -> H8 B
H4 -> a
H5 -> H9 H4
H6 -> H4 B
H7 -> H8 A
H8 -> b
H9 -> H11 H10
H10 -> |A
H11 -> H12 S
H12 -> H13 H8
H13 -> b|
Example 4:
Convert the following CFG into Chomsky normal form
CFG
S -> a B B B | b A A A
CFG IN Chomsky normal form
S -> H1 H0 | H3 H2
H0 -> B
H1 -> H4 H0
H2 -> A
H3 -> H5 H2
H4 -> H6 H0
H5 -> H7 H2
H6 -> a
H7 -> b
Example 5:
CFG conversion into Chomsky normal form
CFG
S -> b A A
CFG IN Chomsky normal form
S -> H1 H0
H0 -> A
H1 -> H2 H0
H2 -> b
Example 6:
CFG conversion into Chomsky normal form
CFG
S -> C b A D A
CFG IN Chomsky normal form
S -> H1 H0
H0 -> A
H1 -> H3 H2
H2 -> D
H3 -> H4 H0
H4 -> H5 H6
H5 -> C
H6 -> b
Example 7:
CFG TO CNF
S -> a B | b d P A
CFG IN Chomsky normal form
S -> H0 H1 | H3 H2
H0 -> a
H1 -> B
H2 -> A
H3 -> H5 H4
H4 -> P
H5 -> H6 H7
H6 -> b
H7 -> d
Greibach Normal Form(GNF)
 A non-terminal generating a terminal (e.g.; X->x)
 A non-terminal generating a terminal followed by any number of non-terminals (e.g.; X->xX 1X2…Xn)
 Start symbol generating ε. (e.g.; S-> ε)

Consider the following grammars:

G1 = {S->aA|bB, B->bB|b, A->aA|a}


G2 = {S->aA|bB, B->bB|ε, A->aA|ε}
The grammar G1 is in GNF as production rules satisfy the rules specified for GNF. However, the
grammar G2 is not in GNF as the production rules B-> ε and A-> ε do not satisfy the rules specified
for GNF (only start symbol can generate ε).
Note –

 For a given grammar, there can be more than one GNF


 GNF produces the same language as generated by CFG

How to convert CFG to GNF –

Step 1. Convert the grammar into CNF.


If the given grammar is not in CNF, convert it to CNF. You can refer following article to convert
CFG to CNF.

Step 2. Eliminate left recursion from grammar if it exists.


If CFG contains left recursion, eliminate them. You can refer following article to eliminate left
recursion.

Step 3. Convert the production rules into GNF form.


If any production rule is not in the GNF form, convert them. Let us take an example to convert CFG
to GNF. Consider the given grammar G1:

S → XA|BB
B → b|SB
X → b
A → a
As G1 is already in CNF and there is not left recursion, we can skip step 1and 2 and directly move to
step 3.
The production rule B->SB is not in GNF, therefore, we substitute S -> XA|BB in production rule B-
>SB as:

S → XA|BB
B → b|XAB|BBB
X → b
A → a
The production rules S->XA and B->XAB is not in GNF, therefore, we substitute X->b in production
rules S->XA and B->XAB as:

S → bA|BB
B → b|bAB|BBB
X → b
A → a
Removing left recursion (B->BBB), we get:
S → bA|BB
B → bC|bABC
C → BBC| ε
X → b
A → a
Removing null production (C-> ε), we get:

S → bA|BB
B → bC|bABC|b|bAB
C → BBC|BB
X → b
A → a
The production rules S->BB is not in GNF, therefore, we substitute B → bC|bABC|b|bAB in
production rules S->BB as:

S → bA| bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
C → BBC|BB
X → b
A → a
The production rules C->BB is not in GNF, therefore, we substitute B → bC|bABC|b|bAB in
production rules C->BB as:

S → bA| bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
C → BBC
C → bCB|bABCB|bB|bABB
X → b
A → a
The production rules C->BBC is not in GNF, therefore, we substitute B → bC|bABC|b|bAB in
production rules C->BBC as:

S → bA| bCB|bABCB|bB|bABB
B → bC|bABC|b|bAB
C → bCBC|bABCBC|bBC|bABBC
C → bCB|bABCB|bB|bABB
X → b
A → a
This is the GNF form for the grammar G1.
Question:

S–ASB/a/bb

A → aSA/a.

B -> ՏbՏ/tt

SOLUTION:

Given Grammar in in CNF format.

By using the substitution,

We have,

S – ASB / a / bb

A → aSA / a.

B -- ՏbՏ / ԵԵ

Put all the values of ‘A’

S -> aSASB / aSB / a / bb

A -> aSA / a

B -> SbS / tt

Put all the values of ‘B’

S -> aSASSbS / aSAStt / aSSbS / aStt / a / bb

A -> aSA / a

B -> SbS / tt

This is in G.N.F
Left Recursion | Left Recursion Elimination
Recursion-

Recursion can be classified into following three types-

1. Left Recursion
2. Right Recursion
3. General Recursion

1. Left Recursion-

 A production of grammar is said to have left recursion if the leftmost variable of its RHS is same as
variable of its LHS.
 A grammar containing a production having left recursion is called as Left Recursive Grammar.

Example-

S → Sa / ∈

(Left Recursive Grammar)

 Left recursion is considered to be a problematic situation for Top down parsers.


 Therefore, left recursion has to be eliminated from the grammar.
Elimination of Left Recursion

Left recursion is eliminated by converting the grammar into a right recursive grammar.

If we have the left-recursive pair of productions-

A → Aα / β

(Left Recursive Grammar)

where β does not begin with an A.

Then, we can eliminate left recursion by replacing the pair of productions with-

A → βA’

A’ → αA’ / ∈

(Right Recursive Grammar)

This right recursive grammar functions same as left recursive grammar.

2. Right Recursion-

 A production of grammar is said to have right recursion if the rightmost variable of its RHS is same as
variable of its LHS.
 A grammar containing a production having right recursion is called as Right Recursive Grammar.

Example-

S → aS / ∈

(Right Recursive Grammar)

 Right recursion does not create any problem for the Top down parsers.
 Therefore, there is no need of eliminating right recursion from the grammar.
3. General Recursion-

 The recursion which is neither left recursion nor right recursion is called as general recursion.

Example-

S → aSb / ∈

A Grammar G (V, T, P, S) is left recursive if it has a production in the form.


A → A α |β.
The above Grammar is left recursive because the left of production is occurring at a first
position on the right side of production. It can eliminate left recursion by replacing a pair of
production with
A → βA′
A → αA′|ϵ
Elimination of Left Recursion
Left Recursion can be eliminated by introducing new non-terminal A such that.

This type of recursion is also called Immediate Left Recursion.


In Left Recursive Grammar, expansion of A will generate Aα, Aαα, Aααα at each step, causing
it to enter into an infinite loop
The general form for left recursion is
A → Aα1|Aα2| … . |Aαm|β1|β2| … . . βn
can be replaced by
A → β1A′|β2A′| … . . | … . . |βnA′
A → α1A′|α2A′| … . . |αmA′|ε

Example:

i) E → E+T|T

ii) T → T*F|F

iii) F → (E)|id

The left and right variables are the same in the production rules above, that is, E
and T.

So to eliminate the left recursion, we have to change the production rules to a


different form.

In the production rules above:

i) E → E+T|T

α=+T and β=T

E → TE′

E′→ +TE′|ϵ
In the production rule above, we still have left recursion:
ii) T → T*F|F

α=*F and β=F

T → FT′

T′→ *FT′|ϵ
After eliminating the left recursion, the final production rules are as follows:

E → TE′

E′→ +TE′|ϵ

T → FT′

T′→ *FT′|ϵ

F → (E)|id
Example2 − Eliminate the left recursion for the following Grammar.
S → a|^|(T)
T → T, S|S
Solution
We have immediate left recursion in T-productions.
Comparing T → T, S|S With A → A α | β where A = T, α =, S and β = S

∴ Complete Grammar will be

S→ a|^(T)
T→ ST′
T′ →,ST′| ε
Example3 − Eliminate the left recursion from the grammar
E → E + T|T
T → T * F|F
F → (E)|id
Solution
The production after removing the left recursion will be
E → TE′

E′ → +TE′| ∈

T → FT′

T′ →∗ FT′| ∈

F → (E)|id
Example4 − Remove the left recursion from the grammar
E → E(T)|T
T → T(F)|F
F → id
Solution
Eliminating immediate left-recursion among all Aα productions, we obtain
E → TE′

E → (T)E′|ε

T → FT′

T′ → (F)T′|ε

F → id

PRACTICE PROBLEMS BASED ON LEFT RECURSION


ELIMINATION-

Problem-01:
Consider the following grammar and eliminate left recursion-

A → ABd / Aa / a

B → Be / b

Solution-

The grammar after eliminating left recursion is-

A → aA’

A’ → BdA’ / aA’ / ∈

B → bB’

B’ → eB’ / ∈

Problem-02:

Consider the following grammar and eliminate left recursion-

E→E+E/ExE/a

Solution-

The grammar after eliminating left recursion is-

E → aA

A → +EA / xEA / ∈

Problem-03:

Consider the following grammar and eliminate left recursion-


E→E+T/T

T→TxF/F

F → id

Solution-

The grammar after eliminating left recursion is-

E → TE’

E’ → +TE’ / ∈

T → FT’

T’ → xFT’ / ∈

F → id

Problem-04:

Consider the following grammar and eliminate left recursion-

S → (L) / a

L→L,S/S

Solution-

The grammar after eliminating left recursion is-

S → (L) / a

L → SL’

L’ → ,SL’ / ∈

Problem-05:
Consider the following grammar and eliminate left recursion-

S → S0S1S / 01

Solution-

The grammar after eliminating left recursion is-

S → 01A

A → 0S1SA / ∈

Problem-06:

Consider the following grammar and eliminate left recursion-

S→A

A → Ad / Ae / aB / ac

B → bBc / f

Solution-

The grammar after eliminating left recursion is-

S→A

A → aBA’ / acA’

A’ → dA’ / eA’ / ∈

B → bBc / f

Problem-07:
Consider the following grammar and eliminate left recursion-

A → AAα / β

Solution-

The grammar after eliminating left recursion is-

A → βA’

A’ → AαA’ / ∈

Problem-08:

Consider the following grammar and eliminate left recursion-

A → Ba / Aa / c

B → Bb / Ab / d

Solution-

This is a case of indirect left recursion.

Step-01:

First let us eliminate left recursion from A → Ba / Aa / c

Eliminating left recursion from here, we get-

A → BaA’ / cA’

A’ → aA’ / ∈
Now, given grammar becomes-

A → BaA’ / cA’

A’ → aA’ / ∈

B → Bb / Ab / d

Step-02:

Substituting the productions of A in B → Ab, we get the following grammar-

A → BaA’ / cA’

A’ → aA’ / ∈

B → Bb / BaA’b / cA’b / d

Step-03:

Now, eliminating left recursion from the productions of B, we get the following grammar-

A → BaA’ / cA’

A’ → aA’ / ∈

B → cA’bB’ / dB’

B’ → bB’ / aA’bB’ / ∈

This is the final grammar after eliminating left recursion.

Problem-09:

Consider the following grammar and eliminate left recursion-

X → XSb / Sa / b
S → Sb / Xa / a

Solution-

This is a case of indirect left recursion.

Step-01:

First let us eliminate left recursion from X → XSb / Sa / b

Eliminating left recursion from here, we get-

X → SaX’ / bX’

X’ → SbX’ / ∈

Now, given grammar becomes-

X → SaX’ / bX’

X’ → SbX’ / ∈

S → Sb / Xa / a

Step-02:

Substituting the productions of X in S → Xa, we get the following grammar-

X → SaX’ / bX’

X’ → SbX’ / ∈

S → Sb / SaX’a / bX’a / a

Step-03:
Now, eliminating left recursion from the productions of S, we get the following grammar-

X → SaX’ / bX’

X’ → SbX’ / ∈

S → bX’aS’ / aS’

S’ → bS’ / aX’aS’ / ∈

This is the final grammar after eliminating left recursion.

Problem-10:

Consider the following grammar and eliminate left recursion-

S → Aa / b

A → Ac / Sd / ∈

Solution-

This is a case of indirect left recursion.

Step-01:

First let us eliminate left recursion from S → Aa / b

This is already free from left recursion.

Step-02:

Substituting the productions of S in A → Sd, we get the following grammar-

S → Aa / b
A → Ac / Aad / bd / ∈

Step-03:

Now, eliminating left recursion from the productions of A, we get the following grammar-

S → Aa / b

A → bdA’ / A’

A’ → cA’ / adA’ / ∈

This is the final grammar after eliminating left recursion.

A Context Free Grammar (CFG) is said to be in Greibach Normal Form(GNF), if production


rules satisfy one of the following criteria −
 Only a start symbol can generate ε. For example, if S is the start symbol then S
→ ε is in GNF.
 A non-terminal can generate a terminal. For example, if A is Non terminal and a
is terminal then, A → a is in GNF.
 A non-terminal can generate a terminal followed by any number of non-
terminals. For Example, S → aAS is in GNF.

Case 1
G1 = {S → aAB | aB, A → aA| a, B → bB | b}
The production rules of G1 satisfy the rules specified for GNF, then the grammar G1 is in
GNF.

Case 2
G2 = {S → aAB | aB, A → aA | ε, B → bB | ε}
The production rule of G2 is not satisfying the rules specified for GNF as
A → ε and B → ε contain ε(only the start symbol can generate ε).
So, the grammar G2 is not in GNF.
Steps for converting CFG into GNF
 Step 1 − Convert the grammar into CNF. If the given grammar is not in CNF,
convert it into CNF.
 Step 2 − If the grammar consists of left recursion, eliminate it. If the context free
grammar contains any left recursion, eliminate it.
 Step 3 − In the grammar, convert the given production rule into GNF form. If any
production rule in the grammar is not in GNF form, convert it.
Example
Consider the context free grammar
S→SS|(S)|a
Convert this grammar to Greibach Normal Form.
Solution
Given below is an explanation for conversion of CFG to GNF −
Step 1:
Converting to CNF:
S->SS|XSY|a
X->(
Y->)

Step 2:
Remove left recursion from S->SS
S->XSYP/aP
P->SP/ε
X->(
Y->)

Step 3:
Remove null production P->ε
S->XSYP/aP
P->SP/S
X->(
Y->)

Step 4:
Convert to GNF as S->XSYP is not in GNF,
Replace X with (
S->(SYP/aP
P->SP/S
X->(
Y->)

Step 5:
Convert to GNF as P->SP is not in GNF,
Replace S with (SYP/aP
S->(SYP/aP
P->(SYPP/aPP/(SYP/aP
X->(
Y->)

You might also like