Grammar in Automata
Grammar in Automata
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
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:
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-
Grammar G1-
S → aSb / ∈
Grammar G2-
S → aAb / ∈
A → aAb / ∈
∴ 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:
This grammar generates the strings having equal number of a’s and b’s.
Example-02:
L(G) = { abc }
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
Example-01:
Example-02:
Applications-
Context Free Grammar (CFG) is of great practical importance. It is used for following purposes-
Properties-
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.
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
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
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)
Example-
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.
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)
Hence proved the given String (W=xxxyyxyyyx) is derived through Rightmost derivation. So,
given string belongs to Given Grammar (G).
Example-
S → aB / bA
A → aS / bAA / a
B → bS / aBB / b
(Unambiguous Grammar)
Rightmost Derivation-
S → aB
→ aaBaBbba (Using A → a)
→ aaBabbba (Using B → b)
→ 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,
Step 2
Step 3
Step 4
Step 5
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.
S → bB / aA
A → b / bS / aAA
B → a / aS / bBB
1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree
Solution-
1. Leftmost Derivation-
S → bB
→ bbaB (Using B → a)
→ bbaababa (Using B → a)
2. Rightmost Derivation-
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:
S → A1B
A → 0A / ∈
B → 0B / 1B / ∈
1. Leftmost derivation
2. Rightmost derivation
3. Parse Tree
Solution-
1. Leftmost Derivation-
S → A1B
→ 001B (Using A → ∈)
→ 00101 (Using B → ∈)
2. Rightmost Derivation-
S → A1B
→ A101 (Using B → ∈)
→ 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-
This is because it can be shown that this problem is equivalent to Post Correspondence Problem.
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
Problem-01:
S → SS
S→a
S→b
Solution-
w = abba
Problem-02:
S→A/B
A → aAb / ab
B → abB / ∈
Solution-
w = ab
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
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
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
Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Problem-06:
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
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-
w = ab + a
Since two different parse trees exist for string w, therefore the given grammar is ambiguous.
Normal Forms-
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.
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
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
Steps-
The following steps are followed to standardize the grammar using CNF-
Rule-01:
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.
Problem-01:
S → aAD
A → aB / bAB
B→b
D→d
Solution-
Step-01:
Step-02:
B → b ………..(1)
D → d ………..(2)
S → aAD ………..(3)
A → aB / bAB ………..(4)
Step-03:
This is done by introducing the following two new productions in the grammar-
Ca → a ………..(5)
Cb → b ………..(6)
S → CaAD ………..(7)
Step-04:
This is done by introducing the following two new productions in the grammar-
CAD → AD ………..(9)
CAB → AB ………..(10)
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
Problem-02:
S → 1A / 0B
A → 1AA / 0S / 0
B → 0BB / 1S / 1
Solution-
Step-01:
Step-02:
A → 0 ………..(1)
B → 1 ………..(2)
S → 1A / 0B ………..(3)
A → 1AA / 0S ………..(4)
B → 0BB / 1S ………..(5)
Step-03:
This is done by introducing the following two new productions in the grammar-
C → 0 ………..(6)
D → 1 ………..(7)
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)
A → DAA ………..(14)
B → CBB ………..(15)
Step-05:
This is done by introducing the following two new productions in the grammar-
E → AA ………..(16)
F → BB ………..(17)
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
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.
Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that each symbol
appears in a sentential form.
Derivation Procedure −
Step 2 − Include all symbols, Yi+1, that can be derived from Yi and include all production rules that
have been applied.
Problem
P:
S → AC | B,
A → a,
C → c | BC,
E → aA | e
Solution
Phase 1 −
T = { a, c, e }
W2 = { A, C, E } U { S } from rule S → AC
W3 = { A, C, E, S } U ∅
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
Y4 = { S, A, C, a, c }
G” = { { A, C, S }, { a, c }, P, {S}}
where P: S → AC, A → a, C → c
Removal Procedure −
Step 3 − Repeat from step 1 until all unit productions are removed.
Problem
Solution −
Y → Z, Z → M, and M → N
S → XY, X → a, Y → Z | b, Z → M, M → a, N → a
S → XY, X → a, Y → Z | b, Z → a, M → a, N → a
S → XY, X → a, Y → a | b, Z → a, M → a, N → a
S → XY, X → a, Y → a | b
ε: A → .......… → ε
Removal Procedure
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
S → ASA | aB | b, A → B, B → b | ∈
Solution −
S→ASA | aB | b | a, A ε B| b | &epsilon, B → b
S→ASA | aB | b | a | SA | AS | S, A → B| b, B → b
A→a
A → BC
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
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|∈
B → ∈ and A → ∈
S0→S,
S→ ASA | aB | a,
A → B | S | ∈,
B→b
S0→S,
S→ ASA | aB | a | AS | SA | S,
A → B | S, B → b
S0→S,
S→ ASA | aB | a | AS | SA,
A → B | S,
B→b
S→ ASA | aB | a | AS | SA
A → B | S, B → b
S0 → ASA | aB | a | AS | SA,
S→ ASA | aB | a | AS | SA
A→S|b
B→b
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
S → ASA,
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
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-> ε)
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:
We have,
S – ASB / a / bb
A → aSA / a.
B -- ՏbՏ / ԵԵ
A -> aSA / a
B -> SbS / tt
A -> aSA / a
B -> SbS / tt
This is in G.N.F
Left Recursion | Left Recursion Elimination
Recursion-
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 recursion is eliminated by converting the grammar into a right recursive grammar.
A → Aα / β
Then, we can eliminate left recursion by replacing the pair of productions with-
A → βA’
A’ → αA’ / ∈
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 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 / ∈
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.
i) E → E+T|T
E → TE′
E′→ +TE′|ϵ
In the production rule above, we still have left recursion:
ii) T → T*F|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
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
Problem-01:
Consider the following grammar and eliminate left recursion-
A → ABd / Aa / a
B → Be / b
Solution-
A → aA’
A’ → BdA’ / aA’ / ∈
B → bB’
B’ → eB’ / ∈
Problem-02:
E→E+E/ExE/a
Solution-
E → aA
A → +EA / xEA / ∈
Problem-03:
T→TxF/F
F → id
Solution-
E → TE’
E’ → +TE’ / ∈
T → FT’
T’ → xFT’ / ∈
F → id
Problem-04:
S → (L) / a
L→L,S/S
Solution-
S → (L) / a
L → SL’
L’ → ,SL’ / ∈
Problem-05:
Consider the following grammar and eliminate left recursion-
S → S0S1S / 01
Solution-
S → 01A
A → 0S1SA / ∈
Problem-06:
S→A
A → Ad / Ae / aB / ac
B → bBc / f
Solution-
S→A
A → aBA’ / acA’
A’ → dA’ / eA’ / ∈
B → bBc / f
Problem-07:
Consider the following grammar and eliminate left recursion-
A → AAα / β
Solution-
A → βA’
A’ → AαA’ / ∈
Problem-08:
A → Ba / Aa / c
B → Bb / Ab / d
Solution-
Step-01:
A → BaA’ / cA’
A’ → aA’ / ∈
Now, given grammar becomes-
A → BaA’ / cA’
A’ → aA’ / ∈
B → Bb / Ab / d
Step-02:
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’ / ∈
Problem-09:
X → XSb / Sa / b
S → Sb / Xa / a
Solution-
Step-01:
X → SaX’ / bX’
X’ → SbX’ / ∈
X → SaX’ / bX’
X’ → SbX’ / ∈
S → Sb / Xa / a
Step-02:
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’ / ∈
Problem-10:
S → Aa / b
A → Ac / Sd / ∈
Solution-
Step-01:
Step-02:
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’ / ∈
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->)