0% found this document useful (0 votes)
7 views112 pages

Automata Theory Computability - M3

Module 3 covers Context-Free Grammars (CFG) and Pushdown Automata (PDA), detailing their definitions, design, simplification, and correctness proofs. It includes topics such as derivation, parse trees, ambiguity, normal forms, and the relationship between CFGs and PDAs. The module also provides examples of CFGs for various formal languages and discusses the generation of strings using rewrite systems.

Uploaded by

anamijames03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views112 pages

Automata Theory Computability - M3

Module 3 covers Context-Free Grammars (CFG) and Pushdown Automata (PDA), detailing their definitions, design, simplification, and correctness proofs. It includes topics such as derivation, parse trees, ambiguity, normal forms, and the relationship between CFGs and PDAs. The module also provides examples of CFGs for various formal languages and discusses the generation of strings using rewrite systems.

Uploaded by

anamijames03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 112

Module-3

Context-Free Grammars(CFG): Introduction to Rewrite Systems and Grammars, CFGs and


languages, designing CFGs, simplifying CFGs, proving that a Grammar is correct, Derivation and
Parse trees, Ambiguity, Normal Forms. Pushdown Automata (PDA): Definition of non-deterministic
PDA, Deterministic and Non-deterministic PDAs, Non-determinism and Halting, alternative
equivalent definitions of a PDA, alternatives that are not equivalent to PDA.
Module 3
CONTENTS

Title Page
Chapter No: 3
No.
3.1 CONTEXT FREE GRAMMAR 1 - 80

3.1.1 Introduction to Rewrite Systems and Grammars 1


3.1.2 CFGs and languages 3
3.1.3 Designing CFGs 4
3.1.4 Derivation and Parse trees 23
3.1.4.1 Ambiguity: Ambiguous and un-ambiguous grammar 32
3.1.5 Proving That a Grammar is Correct 47
3.1.6 Simplification of Context Free Grammar: Eliminating ε productions. 48
3.1.6.1 Eliminating unit production rules 52
3.1.6.2 Eliminating useless symbols and useless production rules 56
3.1.7 Normal Forms: Chomsky Normal Form 63
3.1.7.1 Greibach Normal Form 80
3.2 PUSH DOWN AUTOMATA 81- 110

3.2.1 Definition of Non-deterministic PDA 81


3.2.2 Alternative equivalent definitions of a PDA 84
3.2.3 Designing of PDA 84

3.2.4 Deterministic and Non-deterministic PDAs 98


3.2.5 Equivalence of Context-Free Grammars and PDAs 101
3.2.5.1.1 Building a PDA from CFG using Top Down Parsing 101

3.2.5.1.2 Building a PDA from CFG using Bottom up Parsing 105

3.2.5.2 Building CFG from PDA 108


3.2.6 Non-determinism and Halting 110
3.2.7 Alternatives that are not equivalent to PDA 110
Automata Theory and Computability Introduction to re-write systems (Context Free Grammar)

CONTEXT FREE GRAMMAR (CFG)


Regular Languages (FSMs, Regular expressions. and Regular grammars) that offer less power
and flexibility than a general purpose programming language provides. Because the frameworks
were restrictive, we were able to describe a large class of useful operations that could be
performed on the languages that we defined.
We will begin our discussion of the context-free languages with another restricted formalism, the
context-free grammar.
Introduction to Re-write systems and grammars:
Re-write system is a rule based system in which there is a collection of rules and an algorithm for
applying them.
Each rule has LHS and RHS
Example:
S → aS
S → bA
A→ ɛ
Re-write system works on particular set of strings and try to match the LHS of the rule against
some part of the string. But the core ideas that we will present can be used to define rewrite
systems that operate on richer data structures (programming Languages)
Obtaining the string w using the rules in re-write system is called Derivation.
Re-write system Algorithm:
{
// Input : R: re-write system, w: Initial string
Set working string to w
Until R gets halt do
{
1. Match the LHS of some rule against some part of working string.
2. Replace the matched part of working string with the RHS of the rule that was matched.
3. Return working string
}
}

Athmaranjan K Dept of ISE Page 1


Automata Theory and Computability Introduction to re-write systems (Context Free Grammar)

Grammar:
What is a grammar?
The re-write system which is used to define a language is called Grammar.
G is a grammar which generates a language L then the language is specified as L(G). Grammar work on
set of symbols; can be of two types: Non-terminal symbols and Terminal symbols.
Non-terminal and Terminal Symbols
What is Non-terminal and Terminal Symbols?
Non-terminal symbols are kind of symbols that act as working symbols while the grammar is
working on derivation. Non-terminal symbols disappear when the grammar completely derived
the string w of L(G).
Terminal Symbols are from the input ∑. These symbols generate the string w of L(G).
Every grammar need one special symbol called start symbol. It is normally denoted by S.

Athmaranjan K Dept of ISE Page 2


Automata Theory and Computability Context Free Grammars and Languages

Context-Free Grammars and Languages


We now define a context-free grammar (or CFG) to be a grammar in which each rule must:
 have a left-hand side that is a single non terminal, and
 have any sequence (possibly empty) of symbols (non-terminal and/or terminals) on right-
hand side.
For Example: S→ aSb
S→ ε
T→ T
S→ aSbbTT
The grammar is so called context Free because, using these rules, the decision to replace a non
terminal by some other sequence is made without looking at the context in which the non
terminal occurs. This rule says that S can be replaced by aSb or ε or aSbbTT, as required.
For example, the rule aSa → aTa is not a context Free grammar. This rule says that S can be
replaced by T when it is surrounded by a's. This type of grammar rule is called context-sensitive
because its rules allow context to be considered.
Define Context Free Grammar? (CFG)
A Context Free Grammar G is a Quadruple (V, T, P, S ) where
V – Set of Non-terminal symbols
T – Set of Terminal symbols.
P – Set of production rules, where each production rule is in the form of:
A → α where α € (V U T)*
S - is the start symbol of grammar G.
Example:
S → AB
A → aA|a
B→ bB|ε
Language generated by CFG G:
Language generated by CFG G, is denoted by L(G) is the set of all strings of terminals that can be
derived from S using zero or more applications of rules in G.
Context Free Language (CFL):
A language L is context-free if it is generated by some context-free grammar G.
The context-free languages (or CFLs) are a proper superset of the regular languages.
Athmaranjan K Dept of ISE Page 3
Automata Theory and Computability Design of CFG

Designing Context-Free Grammars


Context free grammar can be generated for different formal languages by constructing the basic
building block grammar for the languages like:
an | n ≥ 0.
an bn | n ≥ 0.
an+1 bn | n ≥ 0.
an bn+1 | n ≥ 0.
a2n bn | n ≥ 0.
an b2n | n ≥ 0.
Write the CFG for the language L = { an | n ≥ 0 }
This we can easily write, by constructing DFA and then converting into CFG or we can directly
write the grammar.
The DFA for the given language is

The transition function is given δ ( S, a ) = S.


This transition function can be converted into CFG as follows:
The first symbol (non-terminal) is identified as the state for which transition is defined.

ie: S and , is replaced by → and the input symbol ‘a’ and the next state S are concatenated and
written on RHS of CFG. ie: S → aS.
For final state we have to include ‘ɛ’ on RHS of CFG . ie: S → ɛ.
Therefore the CFG for the language L ={ an | n ≥ 0 } is given by:
S → aS.
S → ɛ.

Note: The CFG for the language L ={ an | n ≥ 1} is given by:


S → aS
S → a Where the minimum string is a when n =1
Similarly we can write the CFG for other languages as follows:
Athmaranjan K Dept of ISE Page 4
Automata Theory and Computability Design of CFG

The CFG for the language L ={ an bn | n ≥ 0} is given by:


S → aSb
S→ɛ
Here for every ‘a’ one ‘b’ has to be generated. This is obtained by suffixing ‘aS’ with one ‘b’.
The minimum string when n= 0 is ɛ.
If L ={ an bn | n ≥ 1}, then minimum string is ‘ab’ instead of ɛ when n= 1, so the resulting
grammar is: S → aSb
S →ab
The CFG for the language L ={ an+1 bn | n ≥ 0} is given by:
S → aSb
S→a
If n ≥ 1 then CFG becomes:
S → aSb
S → aab
The CFG for the language L ={ an bn+1 | n ≥ 0} is given by:
S → aSb
S→b
If n ≥ 1 then CFG becomes:
S → aSb
S → abb
The CFG for the language L = { a2n bn | n ≥ 0}
S → aaSb
S→ɛ
Here for every two ‘a’s one ‘b’ has to be generated. This is obtained by suffixing ‘aaS’ with one
‘b’. The minimum string is ɛ.
If L= {a2n bn | n ≥ 1}, then minimum string is ‘aab’ instead of ɛ, when n= 1, so the resulting
grammar is:
S → aaSb
S →aab
The CFG for the language L = { an b2n | n ≥ 0}

Athmaranjan K Dept of ISE Page 5


Automata Theory and Computability Design of CFG

S → aSbb
S→ɛ
Here for every ‘a’ two ‘b’s have been be generated. This is obtained by suffixing ‘aS’ with ‘bb’.
The minimum string is ɛ.
If n ≥ 1 then CFG becomes:
S → aSbb
S → abb
Generation of context free grammar for the language represented using regular expression.
i. Obtain CFG for the language L = { (a, b)*}
Language represents any number of a’s and b’s with ɛ.
S → aS | bS | ɛ
ii. Obtain CFG for the language L = { (w ab w| where w € (a+b) * } OR
Obtain CFG for the language containing strings of a’s and b’s with substring ‘ab’
L = {w ab w} can be re-written as:

Where A production represents any number of a’s and b’s and is given by:
A → aA | bA | ɛ
Therefore the resulting grammar is G = ( V, T, P, S) where,
V = { S, A }, T = { a, b}, S is the start symbol, and P is the production rule is as shown below:
S → AabA
A → aA | bA | ɛ
iii. Obtain CFG for the language L = { ( 011 + 1) * 01 }
L can be re-written as:

A production represents any number of 011’s and 1’s including ɛ.


ie: A → 011A | 1A | ɛ
Therefore the resulting grammar is G = ( V, T, P, S) where,
V = { S, A }, T = { 0, 1}, S is the start symbol, and P is the production rule is as shown below:
S → A01
A → 011A | 1A | ɛ
Athmaranjan K Dept of ISE Page 6
Automata Theory and Computability Design of CFG

iv. Obtain CFG for the language L = { w| w € (0,1)* with at least one occurrence of ‘101’ }.
The regular expression corresponding to the language is L = { w 101 w }
Where A production represents any number of 0’s and 1’s and is given by:
A → 0A | 1A | ɛ
Therefore the resulting grammar is G = ( V, T, P, S) where,
V = { S, A }, T = { 0, 1}, S is the start symbol, and P is the production rule is as shown below:
S → A101A
A → 0A | 1A | ɛ
v. Obtain CFG for the language L = { w| wab € (a,b) * }. OR
Obtain CFG for the language containing strings of a’s and b’s ending with’ab’. }.
The resulting grammar is G = ( V, T, P, S) where,
V = { S, A }, T = { a, b}, S is the start symbol, and P is the production rule is as shown below:
S → Aab
A → aA | bA | ɛ
vi. Obtain CFG for the language containing strings of a’s and b’s ending with’ab’ or ‘ba’. }.
OR
Obtain the context free grammar for the language L = { XY | X € (a, b) * and Y € (ab or ba)
The regular expression corresponding to the language is w (ab + ba) where w is in( a, b) *

X→ aX | bX | ɛ
Y → ab | ba
The resulting grammar is G = ( V, T, P, S) where,
V = { S, X, Y }, T = { a, b}, S is the start symbol, and P is the production rule is as shown
below:
S → XY
X→ aX | bX | ɛ
Y → ab | ba

Athmaranjan K Dept of ISE Page 7


Automata Theory and Computability Design of CFG

a). Obtain the CFG for the language L = { Na (w) = Nb (w) | w € (a, b) * }
OR
Obtain the CFG for the language containing strings of a’s and b’s with equal number of a’s and
b’s.
Answer:
To get equal number of a’s and b’s, we know that there are 3 cases:
i. An empty string ɛ has equal number of a’s and b’s
ii. Equal number of a’s followed by equal number of b’s.
iii. Equal number of b’s followed by equal number of a’s.
The corresponding productions for these 3 cases can be written as
S→ ɛ
S→ aSb
S→ bSa
Using these productions the strings of the form ɛ, ab, ba, ababab….., bababa…. etc can be
generated.
But the strings such as abba, baab, etc, where the strings starts and ends with the same symbol,
cannot be generated from these productions. So to generate these type of strings, we need to
concatenate the above two productions which generates equal a’s and equal b’s and equal b’s
and equal a’s or vice versa. The corresponding production is S→ SS.
The resulting grammar corresponding to the language with equal number of a’s and equal
number of b’s is G = ( V, T, P, S) where,
V = { S }, T = { a, b}, S is the start symbol, and P is the production rule is as shown below:
S→ɛ
S→ aSb
S → bSa
S → SS
b). Obtain the CFG for the language L = { Na (w) = Nb (w) + 1 | w € (a, b)* }
The language containing stings of a’s and b’s with number of a’s one more than number of ‘b’s.
Here we should have one more a’s either in the beginning or at the end or at the middle.
We can write the A production with equal number of a’s and equal number of b’s as

Athmaranjan K Dept of ISE Page 8


Automata Theory and Computability Design of CFG

A→ ɛ | aAb | bAa |AA


and finally inserting one extra ‘a’ between these A production. ie:
S→ AaA
The resulting grammar corresponding to the language Na(w) = Nb(w) +1 is G = ( V, T, P, S)
where, V = { S, A }, T = { a, b}, S is the start symbol, and P is the production rule is as shown
below:
S → AaA
A→ aAb | bAa | AA | ɛ
c. Obtain the CFG for the language L = { set of all palindromes over {a, b} }
i. ɛ is a palindrome; ie: S→ ɛ
ii. a is a palindrome; ie: S→ a ( odd length string)
iii. b is a palindrome ; ie: S→ b ( odd length string)
iv. If w is a palindrome then the string awa and the string bwb are palindromes. ie: If S is
palindrome then aSa and bSb is also palindrome. The corresponding production is
S→ aSa | bSb
The resulting grammar corresponding to the language L = { set of all palindromes} is G = ( V, T,
P, S) where,
V = { S }, T = { a, b}, S is the start symbol, and P is the production rule is as shown below:
S → aSa | bSb | a | b | ɛ

d. Obtain the CFG for the language L = {set of all non-palindromes over {a, b}}
Answer:
Non-palindrome strings are not having same symbol at the start and ending point.
ie: A→ aBb | bBa
Where B corresponds to any number of a’s and b’s; ie: B→ aB| bB |ɛ
Finally non-palindrome strings are generated by inserting A production between a palindrome
production S; ie S→ aSa| bSb | A
The resulting grammar corresponding to the language L = { set of all non-palindromes} is G = (
V, T, P, S) where,
V = { S, A, B }, T = { a, b}, S is the start symbol, and P is the production rule is as shown below:

Athmaranjan K Dept of ISE Page 9


Automata Theory and Computability Design of CFG

S → aSa | bSb | A
A→ aBb | bBa
B→ aB | bB |ɛ
e.Obtain the CFG for the language L = { wwR | w€ (a, b) *}
Answer:
NOTE: wwR generate palindrome strings of a’s and b’s of even length.
That means we can remove the odd length palindrome strings such as ‘a’ and ‘b’ from the
palindrome problem(c).
The resulting grammar corresponding to the language L = { wwR} is G = ( V, T, P, S) where,
V = { S }, T = { a, b}, S is the start symbol, and P is the production rule is as shown below:
S → aSa | bSb | ɛ
f. Obtain the CFG for the language L = { w = wR | w is in (a, b) *} 5
OR
L = { palindrome strings over {a,b}
Note: w = wR indicates that string w and its reversal wR is always equal; That means the strings
generated from the language is palindrome strings. (either even or odd length palindrome).

The resulting grammar corresponding to the language L = { w= wR } is G = ( V, T, P, S) where,


V = { S }, T = { a, b}, S is the start symbol, and P is the production rule is as shown below:
S → aSa | bSb | a | b | ɛ

Obtain the CFG for the language containing all positive odd integers up to 999.
The resulting grammar corresponding to the language L = {all positive odd integers up to 999 }
is G = ( V, T, P, S) where,
V = { S, C, D }, T = { 0,1, 2,3,4,5,6,7,8,9}, S is the start symbol, and P is the production rule is
as shown below:
S → C | DC | DDC
C → 1|3|5|7|9
D → 0| 1|2|3|4|5|6|7|8|9
Athmaranjan K Dept of ISE Page 10
Automata Theory and Computability Design of CFG

Obtain the context free grammar for the language L = {0 m 1m 2n | m, n ≥ 1 }


Answer:

We know that CFG corresponding to the language 0m 1m | m ≥ 1, by referring the basic building
block grammar of an bn | n ≥ 1.
The equivalent A production is:
A → 0A1
A → 01
Here B represents any number of 2’s with at least one 2 (n ≥ 1), which is similar to an grammar.
The equivalent B production is:
B → 2B
B→2
So the context free grammar for the language L = { 0m 1m 2n | m, n ≥ 1 } is G = ( V, T, P, S)
where,
V = { S, A, B}, T = { 0, 1, 2}, S is the start symbol, and P is the production rule is as shown
below:
S → AB
A → 0A1 | 01
B → 2B | 2
Obtain the context free grammar for the language L = {a2n bm | m, n ≥ 0 }
Answer:
Since ‘a’ represented in terms of ‘n’ and ‘b’ represented in terms of ‘m’, we can re-write the
language as:

Here A represents 2n number of a’s, and its equivalent production is A→ aaA| ɛ


and B represents m number of b’s, and its equivalent production is B→ bB| ɛ
So the context free grammar for the language L = {a2n bm | m, n ≥ 0 } is G = ( V, T, P, S) where,

Athmaranjan K Dept of ISE Page 11


Automata Theory and Computability Design of CFG

V = { S, A, B}, T = { a, b }, S is the start symbol, and P is the production rule is as shown below:
S → AB
A → aaA |ɛ
B → bB |ɛ
Obtain the context free grammar for the language L = {0 i 1j 2k | i = j or j =k where i, j, k ≥ 0 }
Case 1: when i = j
The given language becomes
The resultant production is given by: A→ 0A1| ɛ and B→ 2B| ɛ
Therefore case 1 results in productions
S→ AB
A→ 0A1| ɛ
B→ 2B| ɛ
Case 2: when j = k

Therefore case 2 results in productions


S→ CD
C→ 0C | ɛ
D→ 1D2| ɛ
So the context free grammar for the language L = { 0i 1j 2k | i = j or j =k where i, j, k ≥ 0 }
is G = ( V, T, P, S) where,
V = {S, A, B, C, D}, T = {0, 1, 2}, S is the start symbol, and P is the production rule is as shown
below:
S→ AB| CD
A→ 0A1| ɛ
B→ 2B| ɛ
C→ 0C | ɛ
D→ 1D2| ɛ

Athmaranjan K Dept of ISE Page 12


Automata Theory and Computability Design of CFG

Obtain the context free grammar for the language L = { 0 i 1j | i ≠ j where i, j ≥ 0 }


Answer:
Here we should not have equal number of 0’s and 1’s. ie: i ≠ j.
Therefore we have two possible cases:
Case 1: when i > j. ie: Number of 0’s greater than number of 1’s. That means at least one 1
followed by equal number of 0’s and 1’s. Therefore the corresponding language is
L = { 0i1j 1+ | i, j ≥ 0 }

Where A→ 0A| 0 B→ 0B1| ɛ


Case 2: when i < j. ie: Number of 0’s less than number of 1’s. That means at least one 0
preceded by equal number of 0’s and 1’s. Therefore the corresponding language is
+
L = { 0 0i1j | i, j ≥ 0 }

Where C→ 1C| 1
So the context free grammar for the language L = { 0i 1j | i ≠ j where i, j ≥ 0 }
is G = ( V, T, P, S) where,
V = {S, A, B, C}, T = {0, 1}, S is the start symbol, and P is the production rule is as shown
below:
S→ AB |BC
A→ 0A| 0
B→ 0B1| ɛ
C→ 1C| 1
Obtain the context free grammar for the language L = {an bm | n = 2m where m ≥ 0 }
Answer:
By substituting n = 2m we have
L= { a2m bm | m ≥ 0 }

Athmaranjan K Dept of ISE Page 13


Automata Theory and Computability Design of CFG

Here for every two ‘a’s one ‘b’ has to be generated. This is obtained by suffixing ‘aaS’ with one
‘b’. The minimum string is ɛ.
So the context free grammar for the language L = {an bm | n = 2m where m ≥ 0 }
is G = ( V, T, P, S) where,
V = {S }, T = {a, b}, S is the start symbol, and P is the production rule is as shown below:
S → aaSb
S→ɛ
Obtain the context free grammar for the language L = {an bm | n ≠ 2m where n, m ≥ 1 }
Answer:
Here n ≠ 2m means n > 2m or n< 2m, which results in two possible cases of Language L.
Case 1: when n > 2m, we can re-write the language L by taking n = 2m + 1
L= { a2m+1 bm | m ≥ 1}; by referring the basic building block grammar example, the resulting
production ( a2m bm ) is given by:
A → aaAb
The minimum string when m = 1 is ‘aaab’.
ie : A → a
Therefore A → aaAb | aaab
Case 2: when n < 2m, we can re-write the language L by taking n = 2m - 1
L= { a2m-1 bm | m ≥ 1 }; by referring the basic building block grammar example, the resulting
production ( a2m bm ) is given by:
B → aaBb
The minimum string when m = 1 is ‘ab’.
ie : B→ ab
Therefore B → aaBb | ab.
So the context free grammar for the language L = {an bm | n ≠ 2m where n, m ≥ 1 }
is G = ( V, T, P, S) where,
V = {S, A, B }, T = {a, b}, S is the start symbol, and P is the production rule is as shown below:
S →A | B
A → aaAb | aaab
B → aaBb | ab.

Athmaranjan K Dept of ISE Page 14


Automata Theory and Computability Design of CFG

Obtain the context free grammar for the language L = {an bm | n ≠ 2m where n, m ≥ 0 }

Note: Answer is same as that of previous problem, except the minimum string value,
ie: when m= 0.
For A production minimum string value when m= 0 is: ‘a’
A→a
For B production minimum string value can be obtained when m= 1 ie: ‘ab’
B → ab
So the context free grammar for the language L = {an bm | n ≠ 2m where n, m ≥ 0 }
is G = ( V, T, P, S) where,
V = {S, A, B}, T = {a, b}, S is the start symbol, and P is the production rule is as shown below:
S →A | B
A → aaAb | a
B → aaBb | ab
Obtain the context free grammar for the language L = { 0 i 1j 2k | i + j = k where i, j ≥ 0 }
Answer:
When i+ j =k, the given language becomes: L = 0i 1j 2i + j
L = 0i 1j 2i 2 j = ; minimum value when i=0 is A
Note: For this type of language we have to select the middle string as a substring (A) and we
need to insert this substring between the start production ie: 0i 2i (where middle term A is
ignored)
The equivalent substring A production is given by: A→ 1A2| ɛ
The start production S→ 0S2| A ; here the minimum value when i = 0 is A
So the context free grammar for the language L = { 0i 1j 2k | i + j = k where i, j ≥ 0 }
is G = ( V, T, P, S) where,
V = {S, A}, T = {0, 1, 2}, S is the start symbol, and P is the production rule is as shown below:
S→ 0S2| A
A→ 1A2| ɛ

Athmaranjan K Dept of ISE Page 15


Automata Theory and Computability Design of CFG

Obtain the context free grammar for the language L = { an bm ck | n+ 2m = k where n, m ≥ 0 }


When n+2m = k, the given language becomes: L = an bm cn + 2m =
Minimum value when n = 0 is A
So the context free grammar for the language L = { an bm ck | n + 2m = k where n, m ≥ 0 }
is G = ( V, T, P, S) where,
V = {S, A}, T = {a, b, c}, S is the start symbol, and P is the production rule is as shown below:
S→ aSc| A
A→ bAcc| ɛ
Obtain the context free grammar for the language L = { w an bn wR| W is in (0, 1)* and n ≥ 0 }
Answer: we can re-write the language L as

The corresponding A production is given by; A → aAb |ɛ ; min. value is ɛ when n = 0


R
We can insert this substring A production between ww production represented by S.
The corresponding S production is S → 0S0 | 1S1 |A
Note: In S production minimum value is A, when wwR results in ɛ; ie: only the middle substring
A appears.
So the context free grammar for the language L = { w an bn wR| w is in (0, 1)* and n ≥ 0 }
is G = ( V, T, P, S) where,
V = {S, A}, T = {a, b, 0, 1}, S is the start symbol, and P is the production rule is as shown
below:
S → 0S0 | 1S1 | A
A→ aAb | ɛ
Obtain the context free grammar for the language L = { an wwR bn | w is in (0, 1)* and n ≥ 2 }

L=

Corresponding A production is A → 0A0 | 1A1 | ɛ


By inserting this substring A production between start production S; ie for an bn | n ≥ 2
S production is; S → aSb | aaAbb ; minimum string when n =2 is aaAbb
So the context free grammar for the language L = { an wwR bn | W is in (0, 1)* and n ≥ 2 }
Athmaranjan K Dept of ISE Page 16
Automata Theory and Computability Design of CFG

is G = ( V, T, P, S) where,
V = {S, A}, T = {a, b, 0, 1}, S is the start symbol, and P is the production rule is as shown
below:
S → aSb | aaAbb
A→ 0A0| 1A1 |ɛ

Obtain the context free grammar for the language L = { a n bn ci | n ≥ 0, i ≥1 U an bn cm dm | n, m


≥0}
Answer:

S1 production is ; S1 → AB
A → aAb |ɛ
B → cB | c
S2 production is ; S2 → AC
C → cCd | ɛ
So the context free grammar for the language L = {an bnci | n ≥ 0, i ≥1 U an bn cm dm | n, m ≥ 0 }
is G = ( V, T, P, S) where,
V = {S, S1, S2 A, B, C}, T = {a, b, c, d}, S is the start symbol, and P is the production rule is as
shown below:
S → S 1 | S2
S1 → AB
A → aAb |ɛ
B → cB | c
S2 → AC
C → cCd | ɛ

Athmaranjan K Dept of ISE Page 17


Automata Theory and Computability Design of CFG

Obtain the context free grammar for the language L1L2 where L1 = { an bn ci | n ≥ 0, i ≥1 } and L2
={ 0n12n | n ≥ 0 }
Answer:

S1 production is ; S1 → AB
A → aAb |ɛ
B → cB | c
S2 production is: S2 → 0 S211 | ɛ
So the context free grammar for the language L1 = { an bn ci | n ≥ 0, i ≥1 } and L2 ={ 0n12n | n ≥
0}
is G = ( V, T, P, S) where,
V = {S, S1, S2, A, B}, T = {a, b, c, 0, 1}, S is the start symbol, and P is the production rule is as
shown below:
S → S 1 S2
S1 → AB
A → aAb |ɛ
B → cB | c
S2 → 0S211 | ɛ

******* Obtain the context free grammar for the language L = { an+2 bm | n ≥ 0, m ≥ n }
It is clear from the above language that set of strings that can be generated is represented as:
n=0 n=1 n=2 …….
m=1 m=2 m=3 … m=2 m=3 m=4 . m=3 m=4 m=5 ……….
aab aabb aabbb … aaabb aaabbb aaabbb . aaaabbb aaaabbbb aaaabbbbb

a ab b* a aabb b* a aaabbb b* ……….

a anbn b*
We observe that above language consists of strings of a’s and b’s which starts with one a
followed by an bn n ≥ 1, which in-term followed by any number of b’s (b*) .

Athmaranjan K Dept of ISE Page 18


Automata Theory and Computability Design of CFG

Therefore the given language L can be re-written as L = { a anbn b* | n ≥ 1}


L=

A → aAb |ab
B→ bB |ɛ ; and S production is S → aAB
So the context free grammar for the language L = { an+2 bm | n ≥ 0, m ≥ n } is G = ( V, T, P, S)
where,
V = {S, A, B}, T = {a, b}, S is the start symbol, and P is the production rule is as shown below:
S → aAB
A → aAb |ab
B→ bB |ɛ
******* Obtain the context free grammar for the language L = { an bm | n ≥ 0, m ≥ n }

n=0 n=1 n=2 …….


m=1 m=2 m=3 … m=2 m=3 m=4 . m=3 m=4 m=5 ……….
ɛb ɛ bb ɛ bbb … abb abbb abbbb . aabbb aabbbb aabbbbb

ɛ b+ ab b+ aabb b+ ……….

an bn b+ where n ≥ 1
We observe that above language consists of strings of a’s and b’s with n number of a’s followed
by n number of b’s, which in term followed by any number of b’s with at least one b
L = { a n bn b+ | n ≥ 0 }

A production is; A → aAb | ɛ


B production is; B → bB | b
So the context free grammar for the language L = { an bm | n ≥ 0, m ≥ n } is G = ( V, T, P, S)
where,
V = {S, A, B}, T = {a, b}, S is the start symbol, and P is the production rule is as shown below:
S → AB
A → aAb | ɛ
B→ bB | b
Athmaranjan K Dept of ISE Page 19
Automata Theory and Computability Design of CFG

******* Obtain the context free grammar for the language L = { an bn-3 | n ≥ 3 }
Answer:
L = { aaaɛ, aaaab, aaaaabb, aaaaaabbb,………………………………….. }
So we can re-write the language as;
L = aaa an bn | n ≥ 0
So the context free grammar for the language L = { an bn-3 | n ≥ 3 } is G = ( V, T, P, S) where,
V = {S, A}, T = {a, b}, S is the start symbol, and P is the production rule is as shown below:
S → aaaA
A → aAb | ɛ
Modulo – k Problems: Writing CFG by constructing DFA:
******* Obtain the context free grammar for the language L = { w € ( a)* | |w| mod 3 ≠ |w|
mod 2 }
Answer:
Here mod 3 results in 3 remainders such as; 0, 1 and 2 and mod 2 results in 2 remainders such as
0 and 1:
The possible states are: ( 0, 0), ( 0, 1), ( 1, 0), ( 1, 1), ( 2, 0), (2, 1)
The equivalent DFA:

The productions are:


S → aA
A→ aB
B→ aC | ɛ
C→ aD | ɛ
D→ aE| ɛ
E→ aS |ɛ
So the context free grammar for the language L = { w € ( a)* | |w| mod 3 ≠ |w| mod 2 }

Athmaranjan K Dept of ISE Page 20


Automata Theory and Computability Design of CFG

is G = ( V, T, P, S) where,
V = {S, A, B, C, D, E}, T = {a, b}, S is the start symbol, and P is the production rule is as shown
below:
S → aA
A→ aB
B→ aC | ɛ
C→ aD | ɛ
D→ aE| ɛ
E→ aS |ɛ
******* Obtain the context free grammar for the language L = { w € ( a, b) * | |w| mod 3 ≠ |w|
mod 2 }
DFA:

The productions are:


S → aA | bA
A→ aB |bB
B→ aC | bC| ɛ
C→ aD |bD | ɛ
D→ aE| bE |ɛ
E→ aS |bS| ɛ
So the context free grammar for the language L = {w € (a, b)* | |w| mod 3 ≠ |w| mod 2 }
is G = ( V, T, P, S) where,
V = {S, A, B, C, D, E}, T = {a, b}, S is the start symbol, and P is the production rule is as shown
below:
S → aA | bA
A→ aB |bB
B→ aC | bC| ɛ
Athmaranjan K Dept of ISE Page 21
Automata Theory and Computability Design of CFG

C→ aD |bD | ɛ
D→ aE| bE |ɛ
E→ aS |bS| ɛ
******* Obtain the context free grammar for the language L = {w: N a(w) mod 2 = 0 where
w € ( a, b)* }
Answer:
Na(w) mod 2 = 0 means; the string contains even number of a’s and any number of b’s.
The Language can be re-written as: bn a2m bn | n ≥ 0, m ≥ 0
The S production is given by: S → ABA where A represents bn | n ≥ 0 and B represents a2m | m ≥
0.
A production is given by: A → bA | ɛ
B production is given by: B→ aaB | ɛ
So the context free grammar for the language L = {w: Na(w) mod 2 = 0 where w € ( a, b)* }
is G = ( V, T, P, S) where,
V = {S, A, B }, T = {a, b}, S is the start symbol, and P is the production rule is as shown below:
S → ABA
A → bA | ɛ
B→ aaB | ɛ
Write a CFG for the language L defines balanced parentheses. OR
L = { { (, ) }*| parentheses are balanced }
So the context free grammar G = ( V, T, P, S) where,
V = {S}, T = {(, )}, S is the start symbol, and P
S → (S)
S → SS
S→ ɛ

Athmaranjan K Dept of ISE Page 22


Automata Theory and Computability Derivation and Parse Tree

Derivation
Define the following terms:
i. Derivation
ii. Left Most Derivation
iii. Right Most Derivation.
iv. Sentential Form
v. Left Sentential Form
Derivation: The process of obtaining string of terminals and/or non-terminals from the start
symbol by applying some or all production rules is called derivation.
If a string is obtained by applying only one production, then it is called one step derivation.
Example: Consider the Productions: S →AB, A→ aAb|ɛ, B →bB|ɛ
S => AB
 aAbB
 abB
 abbB
 abb
Note: The derivation process may end whenever one of the following things happens.
i. The working string no longer contains any non terminal symbols (including, as a special case
when the working string is ε). Ie: working string is generated.
i. There are non terminal symbols in the working string but there is no match with the left-hand
side of any rule in the grammar. For example, if the working string were AaBb, this would
happen if the only left-hand side were C
Left Most Derivation (LMD): In the derivation process, if a leftmost variable is replaced at every
step, then the derivation is said to be leftmost.
Example: E → E+E | E*E | a | b
Let us derive a string a+b*a by applying LMD.
E => E*E
 E+E*E
 a +E*E
 a+b*E

Athmaranjan K Dept of ISE Page 23


Automata Theory and Computability Derivation and Parse Tree

 a+b*a
Right Most Derivation (RMD): In the derivation process, if a rightmost variable is replaced at
every step, then the derivation is said to be rightmost.
Example: E → E+E | E*E | a | b
Let us derive a string a+b*a by applying RMD.
E => E+E
 E+E*E
 E +E*a
 E+b*a
 a+b*a
Sentential form: For a context free grammar G, any string ‘w’ in (V U T)* which appears in
every derivation step is called a sentence or sentential form.
Two ways we can generate sentence:
i. Left sentential form
ii. Right sentential form
Example: S => AB
 aAbB
 abB
 abbB
 abb
Here {S, AB, aAbB, abB, abbB, abb } can be obtained from start symbol S, Each string in the set
is called sentential form.
Left Sentential form: For a context free grammar G, any string ‘w’ in (V U T)* which appears in
every Left Most Derivation step is called a Left sentential form.
Example: E => E*E
 E+E*E
 a +E*E
 a+b*E
 a+b*a
Left sentential form = {E, E*E, E+E*E, a +E*E, a+b*E, a+b*a }

Athmaranjan K Dept of ISE Page 24


Automata Theory and Computability Derivation and Parse Tree

Right Sentential form: For a context free grammar G, any string ‘w’ in (V U T)* which appears
in every Right Most Derivation step is called a Left sentential form.
Example: E => E+E
 E+E*E
 E +E*a
 E + b*a
 a + b*a
Right sentential form = {E, E+E, E+E*E, E +E*a, E+ b*a, a + b * a }
PARSE TREE: ( DERIVATION TREE)
What is parse tree?
The derivation process can be shown in the form of a tree. Such trees are called derivation trees
or Parse trees.
Example: E → E+E | E*E | a | b
The Parse tree for the LMD of the string a+b*a is as shown below:

YIELD OF A TREE:
What is Yield of a tree?
The yield of a tree is the string of terminal symbols obtained by only reading the leaves of the
tree from left to right without considering the ɛ symbols.
Example:

For the above parse tree, the yield of a tree is a+b*a

Athmaranjan K Dept of ISE Page 25


Automata Theory and Computability Derivation and Parse Tree

Branching factor
Define the branching factor of a CFG
The branching factor of a grammar G is the length (the number of symbols) of the longest right-
hand side of any rule in G.
Then the branching factor of any parse tree generated by G is less than or equal to the branching
factor of G.
NOTE:
1. Every leaf node is labelled with terminal symbols including ɛ.
2. The root node is labelled with start symbol S
3. Every interior- node is labelled with some element of V.

Problem 1:
Consider the following grammar G:
S → aAS |a
A→ SbA |SS |ba
Obtain: i) LMD; ii. RMD iii. Parse tree for LMD iv. Parse tree for RMD for the string
‘aabbaa’

Parse tree for RMD: Parse tree for LMD:

Athmaranjan K Dept of ISE Page 26


Automata Theory and Computability Derivation and Parse Tree

Problem 2:
Design a grammar for valid expressions over operator – and /. The arguments of expressions are
valid identifier over symbols a, b, 0 and 1. Derive LMD and RMD for string w = (a11 – b0) /
(b00 – a01). Write parse tree for LMD
Grammar for valid expression:
E → E – E | E / E | (E) |I
I → a | b | Ia |Ib | I0 |I1

Athmaranjan K Dept of ISE Page 27


Automata Theory and Computability Derivation and Parse Tree

Parse Tree for LMD:

Problem 3:
Consider the following grammar G:
E → + EE | * EE | - EE | x | y
Find the: i) LMD; ii. RMD iii. Parse tree for the string ‘+*-xyxy’
Answer:
E → + EE | * EE | - EE | x | y
LMD: RMD:

Athmaranjan K Dept of ISE Page 28


Automata Theory and Computability Derivation and Parse Tree

Parse tree for LMD:

Problem 4:
Show the derivation tree for the string ‘aabbbb’ with grammar:
S → AB |ɛ
A → aB
B → Sb
Give a verbal description of the language generated by this grammar.
Answer: Derivation tree:

Verbal Description of the language generated by this grammar:


From the above grammar we can generate strings of ɛ, abb, aabbbb, aaabbbbbb…….etc.
Therefore the language corresponding to the above grammar is L = { an b2n | n ≥ 0 }.
That means for ‘n’ number of a’s ‘2n’ number of b’s should be generated.
Problem 5:
Consider the following grammar:
E → E+E | E-E
E →E*E | E/E
E → (E)
E →a|b|c
i. Obtain the LMD for the string ( a + b * c)
ii. Obtain the RMD for the string ( a + b )* c)

Athmaranjan K Dept of ISE Page 29


Automata Theory and Computability Derivation and Parse Tree

Answer:

Problem 6:
Consider the following grammar:
S → AbB
A →aA |ɛ
B → aB | bB |ɛ
Give LMD, RMD and parse tree for the string aaabab

LMD: RMD:

Athmaranjan K Dept of ISE Page 30


Automata Theory and Computability Derivation and Parse Tree

Parse tree for LMD:

Obtain the context free grammar for generating integers and derive the integer 1278 by applying
LMD.
The context free grammar corresponding to the language containing set of integers is G = ( V, T,
P, S) where, V = { I, N, D }, T = { 0, 1}, I is the start symbol, and P is the production rule is as
shown below:
I → N | SN
S→+|-|ε
N → D | DN | ND
D → 0 | 1 | 2 | 3 | ……….| 9
LMD for the integer 1278:
I => N
 ND
 NDD
 NDDD
 DDDD
 1DDD
 12DD
 127D
 1278

Athmaranjan K Dept of ISE Page 31


Automata Theory and Computability Ambiguous Grammar

AMBIGUOUS GRAMMAR:
Sometimes a Context Free Grammar may produce more than one parse tree for some (or all) of
the strings it generates. When this happens, we say that the grammar is ambiguous. More
precisely. a grammar G is ambiguous iff there is at least one string in L( G) for which G
produces more than one parse tree.
***What is an ambiguous grammar?
A context free grammar G is an ambiguous grammar if and only if there exists at least one string
„w’ is in L(G) for which grammar G produces more than one parse tree.
Show how ambiguity in grammars are verified with an example.
Testing of ambiguity in a CFG by the following rules:
i. Obtain the string ‘w‟ in L(G) by applying LMD twice and construct the parse tree. If the
two parse trees are different, then the grammar is ambiguous.
ii. Obtain the string ‘w‟ in L(G) by applying RMD twice and construct the parse tree. If the
two parse trees are different, then the grammar is ambiguous.
iii. Obtain the LMD and get a string „w‟. Obtain the RMD and get the same string „w‟
for both the derivations construct the parse tree. If there are two different parse trees
then the grammar is ambiguous.

Show that the following grammar is ambiguous:


S → AB | aaB
A → a | Aa
B→b
Let us take the string w= aab
This string has two parse trees by applying LMD twice so the grammar is ambiguous;

Athmaranjan K Dept of ISE Page 32


Automata Theory and Computability Ambiguous Grammar

Show that the following grammar is ambiguous:


S → SbS
S→ a
Answer:
Take string like w = ababa
This string has two parse trees by applying LMD twice so the grammar is ambiguous;

Techniques for Reducing Ambiguity


Three grammar structures that often lead to ambiguity are
1. ɛ rules like S → ɛ
2. Rules like S→ SS or E→ E + E. In other words recursive rules whose right hand sides are
symmetric and contain at least two copies of the nonterminal on the left-hand side.
3. Rule sets that lead to ambiguous attachment of optional postfixes.
Eliminating ɛ-Rules
Any production rule whose right-hand side is ɛ is an ɛ- production rule.
Algorithm:
a. For the given grammar G identify all the productions are of the form A → ε, then A is a
considered as a nullable variable.
b. Identify the productions are of the form B → C1C2C3…………Ck, where each Ci is must
be a nullable variable, then B is also nullable.
c. Construct a new CFG without ε-productions: In a given grammar identify all non ε-
productions and add these productions to list of new CFG‟s without ε-production. Take all
the combinations of nullable variables in a production by replacing each nullable variable
one by one. Add these productions to list of new CFG‟s without ε-production.

Athmaranjan K Dept of ISE Page 33


Automata Theory and Computability Ambiguous Grammar

Sometimes L(G) contains ɛ and it is important to retain it. The algorithm to handle this situation
is as follows:
1. Let G‟ is the grammar after eliminating ɛ productions
2. If start symbol (S) of G‟ is a Nullable variable then:
2.1 Create a new symbol S‟ in G‟
2.2 Add two production rules in G‟ as S‟→ ɛ and S‟→ S where S is the start symbol of
grammar after eliminating ɛ-rule.
3. Return G‟
Show that the following grammar is ambiguous:
S → SS

S→ ( S) | ɛ over string w = (( ) ( ) ( ) )

The given string has two parse trees by applying LMD twice so the grammar is ambiguous;

After eliminating ɛ-productions from the above grammar:


G" = { {S‟,S, ), ( }, {), (}, P, S‟), where:
P={
S’→ ɛ
S’ → S
S→ (S)
S→()
S→SS
}
But this grammar is still ambiguous, because of the rule S→SS

Athmaranjan K Dept of ISE Page 34


Automata Theory and Computability Ambiguous Grammar

Eliminating Symmetric Recursive Rules


The solution to this problem is to rewrite the grammar so that there is no longer a choice. We
replace the rule S→ SS with one of the following rules:
S→SA ----------------→ Force branching to the left.
S→AS -------------------→force branching to the right.
Then we add the rule S → A and replace the rules S→ (S) and S → () with the rules A→ (S) and
A→ ( ). What we have done is to change the grammar so that branching can occur only in one
direction. Every S that is generated can branch, but no A can. When all the branching has
happened, S rewrites to A and the rest of the derivation can occur.
So one unambiguous grammar for G = { { S, ), (}, {), (}, P, S), where:
P={
S’→ ɛ
S’ → S
S→SA
S→A
A→ (S)
A→()
The technique that we just used for the above grammar G is useful in any situation in which
ambiguity arises from a recursive rule whose right-hand side contains two or more copies of the
left-hand side.
Example:
E → E+E
E →E*E
E →a|b|c
Let us consider the string : a + b + c

Athmaranjan K Dept of ISE Page 35


Automata Theory and Computability Ambiguous Grammar

Parse Tree for LMD1: Parse Tree for LMD2:

The grammar is ambiguous, because we are getting two different parse trees for the same string
by applying LMD twice.
Associativity and Precedence Priority in CFG:
Example:
E → E+E| E-E
E →E*E
E →a|b|c
Associativity:
Let us consider the string : a + b + c
Parse Tree for LMD1: Parse Tree for LMD2:

The two different parse trees exist because of the associativity rules fails. That means for the
given string a + b + c; on either side of the operand „b’, there exist two operators. Which

Athmaranjan K Dept of ISE Page 36


Automata Theory and Computability Ambiguous Grammar

operator should I associate with operand b? This ambiguity results in either I should consider the
operand „b‟ with left side operator (Left associative) or right side (Right associative) operator. So
the first parse tree is correct, where the left most „+‟ is evaluated first.
How to resolve the associativity rules:
E →E+E
E →a|b|c
Here the grammar is not defined in the proper order, ie: the growth of the tree is in either left
direction or right direction.

The growth of the first parse tree is in left direction. That means it is left associative. The growth
second parse tree is in right direction, ie: right associative.
For normal associative rule is left associative, so we have to restrict the growth of parse tree in
right direction by modifying the above grammar as:
E →E+I|I
I→ a | b | c
The parse tree corresponding to the string: a+b+c:

The growth of the parse tree is in left direction since the grammar is left recursive, therefore it is
left associative. There is only one parse tree exists for the given string. So the grammar is
ambiguous.
Note: For the operators to be left associative, grammar should be left recursive. Also for the
operators to be right associative, grammar should be right recursive.

Athmaranjan K Dept of ISE Page 37


Automata Theory and Computability Ambiguous Grammar

Left Recursive grammar: A production in which the leftmost symbol of the body is same as
the non-terminal at the head of the production is called a left recursive production.
Example: E → E + T

Right Recursive grammar: A production in which the rightmost symbol of the body is same as
the non-terminal at the head of the production is called a right recursive production.
Example: E → T + E

Precedence of operators in CFG:


Let us consider the string: a + b * c
LMD 1 for the string: a+b*c LMD 2 for the string: a+b*c

The first parse tree is valid, because the highest precedence operator „*‟ is evaluated first
compared to „+‟. (See the lower level of parse tree, where „*‟ is evaluated first). The second
parse tree is not valid, since the expression containing „+‟ is evaluated first. So here we got two
parse trees because of the precedence is not taken care.
So if we take care of associativity and precedence of operators in CFG, then the grammar is un-
ambiguous.
NOTE:

Normal precedence rule: If we have the operators such as +, -, *, /, , then the highest
precedence operator is evaluated first.
Next highest precedence operator * and / is evaluated. Finally the least precedence operator +
and – is evaluated.
Normal Associativity rule: Grammar should be left associative.

Athmaranjan K Dept of ISE Page 38


Automata Theory and Computability Ambiguous Grammar

How to resolve the operator precedence rules:


E → E+E| E *E
E →a|b|c
If we have two operators of different precedence, then the highest precedence operator (*) is
evaluated first. This can be done by re- writing the grammar with least precedence operator at the
first level and highest precedence operator at the next level.
E →E+T ; + is left associative, because the non-terminal symbol to the left of + is
same as that of non- terminal at the LHS.
At the first level expression containing all „+‟s are generated.
Suppose if no „+‟s are included in the grammar, then we have to bypass the grammar E + T.
This can be done by:
E → T
Finally the first level grammar is
E →E+T|T
Similarly at the second level, we have to generate all „*‟s.
T → T * F ; * is left associative.
If the expression does not contain any „*‟s, then we have to bypass the grammar T → T * F
T → F
Finally the second level grammar is
T →T*F|F
Third level:
F →a|b|c
So the resultant un-ambiguous grammar is:
E →E+T|T
T →T*F|F
F →a|b|c
So the operator which is closest to the start symbol has least precedence and the operator which
is farthest away from start symbol has the highest precedence.

Athmaranjan K Dept of ISE Page 39


Automata Theory and Computability Ambiguous Grammar

Un-Ambiguous Grammar:
For a grammar to be un-ambiguous we have to resolve the two properties such as:
i. Associativity of operators: This can be resolved by writing the grammar recursion.
ii. Precedence of operators: can be resolved by writing the grammar in different levels.
Is the following grammar is ambiguous?
If the grammar is ambiguous, obtain the un-ambiguous grammar assuming normal precedence
and associativity.
E →E+E
E →E*E
E →E/E
E →E-E
E → (E ) | a | b| c
Answer:
Let us consider the string: a + b * c
LMD 1 for the string: a+b*c LMD 2 for the string: a+b*c

For the given string there exists two different parse trees, by applying LMD twice. So the above
grammar is ambiguous.
The equivalent un-ambiguous grammar is obtained by writing all the operators as left associative
and writing the operators +, – at the first level and *, / at the next level.
Equivalent un-ambiguous grammar:
E →E+T|E–T|T
T →T*F|T/F|F
F → ( E) | a | b | c

Athmaranjan K Dept of ISE Page 40


Automata Theory and Computability Ambiguous Grammar

Is the following grammar is ambiguous?


If the grammar is ambiguous, obtain the un-ambiguous grammar assuming the operators + and –
are left associative and * and / are right associative with normal precedence .
E →E+E
E →E*E
E →E/E
E →E-E
E → (E ) | a | b| c
Ambiguous grammar------- see the previous answer.
Equivalent un-ambiguous grammar:
E →E+T|E–T|T
T →F*T|F/T|F
F → ( E) | a | b | c
Is the following grammar is ambiguous?
If the grammar is ambiguous, obtain the un-ambiguous grammar assuming the operators + and /
are left associative and * and - are right associative with + and / has the highest precedence and *
and – has the least precedence.
E →E+E
E →E*E
E →E/E
E →E-E
E → (E ) | a | b| c

Equivalent un-ambiguous grammar:


E →T-E|T*E|T
T →T+F|T/F|F
F → ( E) | a | b | c

Athmaranjan K Dept of ISE Page 41


Automata Theory and Computability Ambiguous Grammar

Problem 1:
Consider the grammar:
S → aS | aSbS | ɛ
Is the above grammar ambiguous? Show in particular that the string „aab‟ has two:
i. Parse trees
ii. Left Most Derivations
iii. Right Most Derivations.
iv. Un-ambiguous grammar

OR
Define ambiguous grammar. Prove that the following grammar is ambiguous.
S → aS | aSbS| ɛ

LMD 1 for the string ‘aab’: LMD 2 for the string ‘aab’:

RMD 1 for the string ‘aab’: RMD 2 for the string ‘aab’:

Athmaranjan K Dept of ISE Page 42


Automata Theory and Computability Ambiguous Grammar

Two Parse trees for LMD:

The above grammar is ambiguous, since we are getting two parse trees for the same string „aab‟
by applying LMD twice.
The equivalent un-ambiguous grammar:
S → aS | aAbS | ɛ
A → aAbA | ɛ
Problem 2:
Consider the grammar:
S → S +S | S * S | (S) | a
Show that string a + a * a has two
i. Parse trees
ii. Left Most Derivations
Find an un-ambiguous grammar G‟ equivalent to G and show that L (G) = L (G‟) and G‟ is un-
ambiguous.
Two different parse trees for the string a+a*a :

Two LMDs:

Athmaranjan K Dept of ISE Page 43


Automata Theory and Computability Ambiguous Grammar

Un-ambiguous grammar Corresponding to the above grammar (G) is:


G‟ = ( V‟, T‟, P‟, S) where V‟ = { S, T, F }, T‟ = { a }, S is the start symbol and P‟ is the
production given by:
S→S+T|T
T → T* F | F
F→(S)|a
Consider the string a + a * a ( Generate this string using G’ using LMD )

So we proved that same language can be generated using G‟

NOTE: Suppose if we have an exponential operator ( ) in an expression such as;

; This can be represented as 2 3 2 where 32 is evaluated first as 9, then 29 is evaluated.

That means the evaluation starts from right side; therefore the operator is right associative.

Any expression containing the operators such as: +, -, *, / and

highest precedence ( Farthest away from start symbol )


*, / next highest precedence. ( next least level)
+, - least precedence ( closest to the start symbol)

Athmaranjan K Dept of ISE Page 44


Automata Theory and Computability Ambiguous Grammar

Show that the following grammar is ambiguous. Also find the un-ambiguous grammar
equivalent to the grammar by normal precedence and associative rules.
E → E+ E | E - E
E → E*E| E / E

E→E E
E → ( E) | a | b
Answer:
We already proved that the above grammar is ambiguous
Equivalent Un-ambiguous grammar:
E→E+T|E–T|T
T→T*F|T/F|F

F→G F|G
G → (E) | a | b

Here the operator is right associative.


Ambiguous Attachment
Third source of ambiguity that we will consider arises when constructs with optional fragments
are nested. The problem in such cases is then, “Given an instance of the optional fragment at
what level of the parse tree should it be attached?'' Probably the most often described instance of
this kind of ambiguity is known as the dangling else problem. Suppose that we define a
programming language with an if statement that can have either of the following forms:
Statement → if Condition then Statement
Statement → if Condition then Statement else Statement
In other words the else clause is optional; Then the following statement with just a single e1se
clause has two parse trees.
Let us consider one statementof programming language:
if cond1 then if cond2 then stmt 1 e1se smt2
In the first parse tree. the single e1se clause goes with the first if. (So it attaches high in the parse
tree.) In the second parse tree the single e1se clause goes with the second if. (In this case it
attaches lower in the parse tree)

Athmaranjan K Dept of ISE Page 45


Automata Theory and Computability Ambiguous Grammar

Show that the following grammar is ambiguous using the string “ ibtibtaea”
S → iCtS | iCtSeS | a
C→ b
Answer:
Here i stands for if, C stands for condition, S stands for statement, t stands for then and e stands
for else.
String w = ibtibtaea (if condition b then if condition b then assignment a else assignment
statement a.
The given string has two parse trees by applying LMD twice so the grammar is ambiguous.

Elimination of Ambiguity:
 Normally else is matched with closest previous if.
 There can be matched pair and unmatched pair
 The matched pair means if statement has else part and unmatched pair means if statement
has no else part.
Hence the un-ambiguous grammar will be:
S →M|U
M → iCtMeM | a
U→ iCtS | iCtMeU
C→b

Athmaranjan K Dept of ISE Page 46


Automata Theory and Computability Proving that a Grammar is Correct

Proving That a Grammar is Correct


To prove that given grammar is correct for language L, there are two things that are essential
1. G generates only strings in L.
2. G generates all strings in L
Prove correctness of language L = {anbn |n ≥ 0}
Solution:
To prove the correctness of given language we need to build the CFG for given grammar:
G = (V, T, P, S) where P is
S →aSb|ɛ
We will derive various strings from given grammar production rules.

S=> aSb S =>ɛ €L anbn when n= 0


 aaSbb
 aaaSbbb
 aaabbb € L = {anbn}
Thus n number of a’s appear before n number of b’s in the above derivation. Hence grammar G
proves to be correct for language L.

Athmaranjan K Page 47
Automata Theory and Computability Simplification of CFG: Elimination of ε production rules

Simplification of Context Free Grammar


Our first task is to simplify context-free grammars; these simplifications make it easier to prove
facts about CFL’s, since we can claim that if a language is CFL, then it has a grammar in some
special form.
The main goal of simplification of grammar is to show that every CFL generated by a CFG in
which all productions are without, ε-productions, unit-productions and useless productions.
Finally grammar is transformed into Chomsky Normal form with productions of the form
A→BC or A→a, where A,B and C are variables and a is a terminal.
A CFG can be simplified by eliminating:
1. All ε- productions.
2. All unit – productions.
3. All useless productions
Eliminating ε- productions:
What is ε- production?
For a context free grammar G = ( V,T, P,S), all productions of the form A → ε is called an ε-
production, where A is a variable defined in V.
What is Nullable variable?

A variable “A” is nullable variable if . If A is nullable then the production of the form
B → A gives another nullable variable B.
Procedure:
1. For the given grammar G identify all the productions are of the form A → ε, then A is a
considered as a nullable variable.
2. Identify the productions are of the form B → C1C2C3…………Ck, where each Ci is must
be a nullable variable, then B is also nullable.
3. Construct a new CFG without ε-productions: In a given grammar identify all non ε-
productions and add these productions to list of new CFG’s without ε-production. Take all
the combinations of nullable variables in a production by replacing each nullable variable
one by one. Add these productions to list of new CFG’s without ε-production.

Athmaranjan K Dept of ISE Page 48


Automata Theory and Computability Simplification of CFG: Elimination of ε production rules

Sometimes the language generated by the CFG L(G) contains ɛ and it is important to retain it.
The algorithm to handle this situation is as follows:
1. Let G’ is the grammar after eliminating ɛ productions
2. If start symbol (S) of G’ is a Nullable variable then:
1.1 Create a new symbol S’ in G’
1.2 Add two production rules in G’ as S’→ ɛ and S’→ S where S is the start symbol of
grammar after eliminating ɛ-rule.
2. Return G’

1.Eliminate all ε- productions for the following grammar:


S → AbaC
A → BC
B → b|ε

C → D|ε
D → d
Nullable variables:
Obviously B and C are directly nullable, since
B → ε
C → ε
Next we find A is nullable variable because of the rule
A → BC
Nullable variables are { A, B, C }
Thus the grammar without ε- productions is given by:
S → AbaC |Aba|baC|ba
A → BC | B | C
B → b|ε
C → D|ε
D → d

Athmaranjan K Dept of ISE Page 49


Automata Theory and Computability Simplification of CFG: Elimination of ε production rules

2. Eliminate all ε- productions for the following grammar:


S → ABC
A → BC | a
B → bAC | ε
C → cAB | ε
Nullable variables:
Obviously B and C are directly nullable, since
B → ε
C → ε
Next we find A is nullable variable because of the rule
A → BC
Finally we find that S is nullable because of the rule
S → ABC
Nullable variables are { S, A, B, C }
Since the start symbol of grammar S is Nullable variable, we create a new Start symbol S’
and add two rules:
S’ → ɛ
S’ → S
Now in order to construct the grammar without ε- productions, let us consider the production
S → ABC
Since all symbols are nullable it becomes( by replacing each nullable symbols one by one)
S → ABC | AB | BC | AC |A | B | C
Next A → BC| a becomes
A → BC | B | C | a
Similarly the productions for B and C becomes
B → bAC | bA | bC | b
C → cAB | cA | cB | c

Athmaranjan K Dept of ISE Page 50


Automata Theory and Computability Simplification of CFG: Elimination of ε production rules

Thus the grammar without ε- productions is given by:


S’ → ɛ
S’ → S
S → ABC | AB | BC | AC |A | B | C
A → BC | B | C | a
B → bAC | bA | bC | b
C → cAB | cA | cB | c
3. Eliminate all ε- productions for the following grammar:
S → AB
A → aAA | ε

B → bBB | ε

Nullable variables are { S, A, B }


S’ → ε

S’ → S
S → AB | B | A

A → aAA | aA |a

B → bBB | bB|b

NOTE:
Sometimes removal ɛ production also adds some production rule of the form A →B, which is of
useless.
Example:
S → A
A → B
B → C
C → d

Athmaranjan K Dept of ISE Page 51


Automata Theory and Computability Eliminating unit production rules

Eliminating Unit Productions


What is unit production?
For a context free grammar G = (V,T, P,S), all productions of the form A → B is called an unit
production, where A and B are variables defined in V.
Procedure:
1. Remove all the productions of the form A → B.
2. Add all non-unit productions of the given grammar to the new list of grammar without unit
productions.
3. For each unit productions of the form:
A→B
B→C
C→ D
we observe that for A → B all non unit productions generated from B can also be generated
from A. That means substitute all non unit productions defined in B for A.
For B → C all non unit productions generated from C can also be generated from B. That
means substitute all non unit productions defined in C for B.
For C → D all non unit productions generated from D can also be generated from C. That
means substitute all non unit productions defined in D for C.
4. Finally the unit productions can be deleted from the given grammar.

Eliminate all unit productions from the following grammar.


S → Aa|B| Ca
B → aB|b
C → Db |D
D → E|d
E → ab

Athmaranjan K Dept. of ISE Page 52


Automata Theory and Computability Eliminating unit production rules

After eliminating unit productions, the resultant grammar:


S → Aa|aB| b| Ca
B → aB|b
C → Db |d |ab
D → ab|d
E → ab
Eliminate all unit productions from the following grammar
S → Aa|B
B → A|bb
A → a |bc|B

After eliminating unit productions, the resultant grammar:


S → Aa|bb |a |bc
B → bb|a |bc
A → a |bc|bb

Eliminate all unit productions from the following grammar


S → XY
X → A
A → B |a
B → b
Y → T
T → Y|c
After eliminating unit productions, the resultant grammar:
S → XY
X → a|b
A → b |a
B → b
Y → c
T → c

Athmaranjan K Dept. of ISE Page 53


Automata Theory and Computability Eliminating unit production rules

Eliminate all unit productions from the following grammar


S → AB
A → a
B → C|b
C → D
D → E
E → a

After eliminating unit productions, the resultant grammar:


S → AB
A → a
B → a|b
C → a
D → a
E → a

Eliminate all unit productions from the following grammar

E → T|E+T

T → F|T*F
F → I | (E)
I → a | b| Ia | Ib | I0 | I1
The unit productions are:
E → T

T → F
F → I
The unit production of the form
F → I
results in a new non unit production for F such as F → a | b| Ia | Ib | I0 | I1 | (E)

Athmaranjan K Dept. of ISE Page 54


Automata Theory and Computability Eliminating unit production rules

The unit production


T → F
results in a new non unit production for T such as T → a | b| Ia | Ib | I0 | I1 | (E) | T * F
The unit production
E → T

results in a new non unit production for E such as E → a | b| Ia | Ib | I0 | I1|(E) | T * F | E + T


Finally the context free grammar without unit productions are given by:
E → E + T | T * F | (E) | a | b | Ia | Ib | I0 | I1
T → T * F | ( E) | a | b| Ia | Ib | I0 | I1
F → (E) | a | b| Ia | Ib | I0 | I1
I → a | b| Ia | Ib | I0 | I1
Eliminate all unit productions from the following grammar
S → A | bb
A → B |b
B → S |a
Unit productions are
S → A gives S→b
A→B gives A→a
B→S gives B → bb
From S → A→ B gives one more production of the form S → B , that is S → a
From A → B→ S gives one more production of the form A → S that is A → bb
From B→ S→ A gives one more production of the form B → A that is B → b
The context free grammar without unit productions are given by:
S → a | b | bb
A → a | bb |b
B → bb | b |a

Athmaranjan K Dept. of ISE Page 55


Automata Theory and Computability Eliminating useless symbols and production rules

Eliminating Useless Productions:


What are useless symbols ? and useless productions?
The variables or terminals that do not appear in any derivation of a terminal string from the start
symbol are called useless symbols. The productions which has useless symbols are called useless
productions.
Example: S → aA | B
A → aA | a
S → B ,a string can never be generated. So the symbol B and the production S → B is useless.
What are generating symbols?
Any symbol X is said to be generating, if X w for some terminal string “w”.
What are reachable symbols?
Any symbol X is said to be reachable if S αXβ for some α and β. Where X is in either V or T
So in a CFG symbols are useful only if the symbols are generating and reachable.
Procedure:
1. For the given grammar identify the Variables which are generating string of terminals
( Generating symbols).
2. From the generating symbols, find all the symbols which are reachable from start symbol.
3. Finally write the grammar without useless symbols and production rules
For the grammar given below eliminate all useless productions
S → aAa | aBC
A → aS |bD
B → aBa |b
C → abb | DD
D → aDa
Initially we find all the terminal symbols used in the grammar a, b, are useful.
Then we have to see the rules which are generating these terminal symbols.
So initially we have B and C be the only non-terminal symbols which directly generate a
terminal string. So B and C are generating symbols.

Athmaranjan K Dept of ISE Page 56


Automata Theory and Computability Eliminating useless symbols and production rules

Variable S is generating from the rule S →aBC and A → aS results in another generating symbol
A. Thus the symbols which are generating string of terminals are{ a, b, S, A, B, C } and D is
not a generating symbol, so we can eliminate that symbol and its production rule.
By eliminating D and those productions involving D we get the grammar:
S → aAa | aBC
A → aS
B → aBa |b
C → abb
Identify the reachable symbols:
From the above grammar we observe that all the variables and terminals S, A, B, C, “a” and “b”
are reachable from S. So the resultant grammar without useless productions are:
S → aAa
A → aS
B → aBa |b
C → abb
For the grammar given below eliminate all useless productions
S → AB | CA
A → a
B → BC |AB
C → aB | b
Identify the generating symbols (Variables)
Initially we have
A → a
C → b
Generates string of terminals a and b, So { A, C } are initial generating symbols.
Again from
S → CA
We can generate sting of terminals, S is also generating symbol.
Finally {S, A, C} are generating symbols, and B is not a generating symbol, so we can eliminate
that symbol.
By eliminating B and those productions involving B we get the grammar:
Athmaranjan K Dept of ISE Page 57
Automata Theory and Computability Eliminating useless symbols and production rules

S → CA
A → a
C → b
Identify the reachable symbols:
From the above grammar we observe that only S, A, C, “a” and “b” are reachable from S. So the
grammar without useless symbols and productions are:
S → CA
A → a
C → b
For the grammar given below eliminate all useless productions
S → abA | bB
A → aA |d
B → bB
D → ab| Ea
E → aC | a
Identify the generating symbols (Variables)
A → d
D → ab
E → a
Initially { A,D, E } are generating symbols, from the rule
S → abA
S is also generating symbols. Finally { S, A, D, E } are generating symbols, and B is not a
generating symbol, so we can eliminate that symbol. Also eliminate symbol C.
By eliminating B, C and those productions involving B and C we get the grammar:

S → abA
A → aA |d
D → ab| Ea
E → a

Athmaranjan K Dept of ISE Page 58


Automata Theory and Computability Eliminating useless symbols and production rules

Identify the reachable symbols:


From the above grammar we observe that only S, A, “a”, “b” and “d” are reachable from S. So
the grammar without useless symbols and productions are:
S → abA
A → aA |d
For the grammar given below eliminate all useless productions
S → aA |a | Bb | cC
A → aB
B → a | Aa
C → cCD
D → ddd
Identify the generating symbols (Variables)
Initial Generating symbols are {S, B, D}, from the rule
A → aB
A is also reachable symbol, Finally {S, A, B, D} are reachable symbols, and C is not a
generating symbol, so we can eliminate that symbol.
By eliminating C and those productions involving C we get the grammar:
S → aA |a | Bb
A → aB
B → a | Aa
D → ddd
Identify the reachable symbols:
From the above grammar we observe that only S, A, B “a” and “b” are reachable from S. So by
eliminating D and all productions involving it we get the grammar without useless symbols and
productions are:
S → aA |a | Bb
A → aB
B → a | Aa

Athmaranjan K Dept of ISE Page 59


Automata Theory and Computability Eliminating useless symbols and production rules

For the grammar given below eliminate all useless productions


S → aAa
A → Sb | bCC | DaA
C → abb | DD
E → ac
D → dDA
From the above grammar we observe that set of generating symbols are {C, E, A,S }
Eliminate the symbol D and those productions involving D we get the grammar:

S → aAa
A → Sb | bCC
C → abb
E → ac

Identify the reachable symbols:


We observe that only S, A, C, “a” and “b” are reachable from S. So by eliminating E and all
productions involving it we get the grammar without useless symbols and productions are:
S → aAa
A → Sb | bCC
C → abb
For the grammar given below eliminate all useless productions

S → ABa | BC
A → aC | BCC
C → a
B → bcc
D → E
E → d
F → e

Athmaranjan K Dept of ISE Page 60


Automata Theory and Computability Eliminating useless symbols and production rules

Identify the generating symbols (Variables):


Generating symbols are { B, C, E, F, S, A, D }
We observe that {S, A, B,C, a, b, c } are reachable symbols from S and eliminate all other
symbols and its productions.
Grammar without useless productions are:
S → ABa | BC
A → aC | BCC
C → a
B → bcc

Note: Incase if the grammar has ε-productions, unit productions and useless productions, then
perform the following operations one after the other.
1. Eliminate all - ε-productions.
2. Eliminate all - Unit-productions.
3. Finally Eliminate all - useless-productions.

Remove all useless productions, unit productions and all ε-productions from the grammar
S → aA | aB
A → aaA| B | ε
B → b | bB
D → B
By eliminating ε-productions, the given grammar becomes,
Nullable variable = { A }
S → aA | a | aB
A → aaA| aa | B
B → b | bB
D → B
It has two unit productions, A → B and D→ B
After eliminating unit-productions, the given grammar becomes,

Athmaranjan K Dept of ISE Page 61


Automata Theory and Computability Eliminating useless symbols and production rules

S → aA | a | aB
A → aaA| aa | b |bB
B → b | bB
D → b | bB
From the above grammar {S, A, B, D} are generating symbols.
The reachable symbols are S, A, B, a and b.
Now by eliminating useless symbol D & its productions, resultant grammar is
S → aA | a | aB
A → aaA| aa | b |bB
B → b | bB

Athmaranjan K Dept of ISE Page 62


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

NORMAL FORMS FOR CONTEXT FREE GRAMMARS:


In a context free grammar the right hand side of a production can be any string of variables and
terminals. When the production rules in the grammar satisfy certain restrictions, then that
grammar is said to be in a Normal form.
Why Normalization is required in CFG?
A context free language may be defined by several context free grammars. For the purpose of
comparison and analysis, it is convenient to define a common form into which all CFGs could be
transformed. This technique is called Normalization and its result is called a normal form.
Two widely used normal forms are:
1. CHOMSKY Normal Form.
2. GREIBACH Normal Form.
CHOMSKY Normal Form. ( CNF )
What is CNF ? Give an example.
A grammar G = ( V, T, P, S) is said to be in CNF, if it has the productions of the form:
1. A → BC where A, B and C are variables.
2. A → a where “A” is variable and “a” is terminal.
Further grammar G has no useless symbols.
*****Conversion of Context Free Grammar to Chomsky Normal Form
1. To put a Context Free Grammar G in CNF, if G has ε, unit and useless productions then
eliminate all.
2. The resulting CFG, whose RHS have length greater than 1 and include a terminal such
as: A → aB or A → BaC Then apply Remove Mixed production algorithm.
3. The resulting CFG, whose RHS have length greater than 2 such as: A → BCDE, then
apply Remove Long production algorithm.
4. Now the CFG contains all the production rules in CNF. (A → BC or A → a)
Remove Mixed production Algorithm:
1. Let G is the given CFG. Set G’ = G
2. Create a new non-terminal Ta for each terminal a in ∑
3. Modify each rule in G' whose right-hand side has length greater than 1 and that contains
a terminal symbol by substituting Ta for each occurrence of the terminal a.
Athmaranjan K Dept of ISE Page 63
Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

4. Add to G', for each Ta the rule Ta → a


5. Return G'.

Remove all mixed- productions for the following grammar


A → a
A → aB
A → BaC | Abb
A → BbC
After removing mixed productions G becomes:
A → a
A → Ta B
A → B TaC |ATbTb
A → B TbC
Ta → a
Tb → b
Remove all mixed- productions for the following grammar
S → a ACa | aAa | aCa | aa
A → a| c | cC
B → c | cC
C → cC| c

After removing mixed productions G becomes:


S → X ACX | XAX | XCX | XX
A → a| c | YC
B → c | YC
C → YC| c
X → a
Y → C

Athmaranjan K Dept of ISE Page 64


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

Remove Long productions Algorithm:


1. Let G is the given CFG. Set G’ = G
2. We must break those productions A → B1B2B3………………….B k, for k > 2, into a group of
productions with two variables in each body.
3. We introduce k – 2 new variables, C1, C2, C3, ……………..Ck-2. The original production
is replaced by the k-1 productions.
A →B1C1,
C1 → B2C2 ……………………
Ck-3 → Bk-2 Ck-2,
Ck-2 → Bk-1Bk
4. Return G’
Remove long- productions for the following grammar
S → BCDEF
After removing long productions G becomes:
S → BR
P → EF
Q → DP
R → CQ

Remove all long- productions for the following grammar

S → X ACX | XAX | XCX | XX


A → a| c | YC
B → c | YC
C → YC| c
X → a
Y → c
After removing long productions G becomes:
S → X E | XF | XD | XX

Athmaranjan K Dept of ISE Page 65


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

D → CX
E → AD
F → AX
X → a
A → a| c | YC
B → c | YC
C → YC| c
X → a
Y → c
CNF Examples:
Convert the following grammar to CNF
S → aSb | ab | Aa
A → aab
There is no ɛ, unit and useless production rules for the given grammar.
After removing mixed productions:
S → XSY | XY | AX
A → XXY
X→a
Y→b

After removing Long productions the resultant grammar is in CNF:


S → XZ | XY | AX
A → XP
X→a
Y→b
Z → SY
P → XY

Athmaranjan K Dept of ISE Page 66


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

Begin with the grammar G:


i. Eliminate all ε - productions
ii. Eliminate any unit - productions in the resulting grammar
iii. Eliminate any useless - productions in the resulting grammar
iv. Put the resulting grammar into Chomsky Normal Form
S → aACa
A → B|a
B → C|c
C→ cC |ɛ
Step 1: Eliminate ɛ productions:
Nullable Variables: {A, B, C}
After eliminating ɛ productions, the resultant grammar:
S → aACa |aCa| aAa| aa
A → B| a
B → C| c
C→ cC |c
Step 2: Eliminate unit productions:
After eliminating unit productions, in the resultant grammar:
S → aACa |aCa| aAa| aa
A → a | c | cC
B → c | cC
C→ cC |c
Step 2: Eliminate useless productions:
Generating symbols: {a, c, A, B. C, S}
Reachable symbols from start symbol S are: {S, A, C} and B is not reachable from S; so we can
remove symbol B and its production rules.
After eliminating useless productions, in the resultant grammar:
S → aACa |aCa| aAa| aa
A → a | c | cC
C→ cC |c

Athmaranjan K Dept of ISE Page 67


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

To convert the resultant grammar into CNF, we need to remove mixed and long production rules:
After eliminating mixed productions, the resultant grammar is:
S → XACX |XCX| XAX| XX
A → a | c | YC
C→ YC |c
X→ a
Y→ c
After eliminating long production rules, the resultant grammar is in Chomsky Norm Form:
S → XF|XD| XE| XX
A → a | c | YC
C→ YC |c
X→ a
Y→ c
D → CX
E → AX
F→ AD
X→ a
Y→ c
Begin with the grammar G:
S → ABC | BaB
A → aA| BaC | aaa
B → bBb | a | D
C → CA | AC
D → ε
i. Eliminate all ε - productions
ii. Eliminate any unit - productions in the resulting grammar
iii. Eliminate any useless - productions in the resulting grammar
iv. Put the resulting grammar into Chomsky Normal Form
After eliminating ε – productions, the resultant grammar is:
Nullable variables: {D, B}

Athmaranjan K Dept of ISE Page 68


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

S → ABC | AC| BaB| Ba|aB|a


A → aA| BaC | aC|aaa
B → bBb | bb| a |D
C → CA | AC
After eliminating unit– productions, the resultant grammar is:
B → D can be directly removed without any substitution, since D production is not defined.
S → ABC | AC| BaB| Ba|aB|a
A → aA| BaC | aC|aaa
B → bBb | bb| a
C → CA | AC
After eliminating useless– productions, the resultant grammar is:
Generating symbols are: {a, b, A, B, S} and eliminate symbol C and its productions
S → BaB| Ba|aB|a
A → aA|aaa
B → bBb | bb| a
From the above grammar, reachable symbols are {S, B, a, b}. A is not reachable, so eliminate A
and its productions. Finally grammar without useless production rules:

S → BaB| Ba|aB|a
B → bBb | bb| a

To convert the resultant grammar into CNF, we need to remove mixed and long production rules:
After eliminating mixed productions, the resultant grammar is:
S → BXB| BX | XB |a
B → YBY |YY| a
X → a
Y → b
After eliminating long production rules, the resultant grammar is in Chomsky Norm Form
S → BZ| BX | XB |a
B → YP |YY| a

Athmaranjan K Dept of ISE Page 69


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

X → a
Y → b
Z → XB
P → BY

Begin with the grammar G:


S → ASB | ε
A → aAS | a
B → SbS |A |bb
i. Eliminate all ε - productions
ii. Eliminate any unit - productions in the resulting grammar
iii. Eliminate any useless - productions in the resulting grammar
iv. Put the resulting grammar into Chomsky Normal Form
After eliminating ε – productions.
Nullable variables = {S} since start symbol S is a Nullable variable, add two new rules as:
S’ → ɛ
S’ → S
S → ASB | AB
A → aAS | aA | a
B → SbS | Sb | bS | b |A |bb
After eliminating unit – productions.
S’ → ɛ
S’ → ASB | AB
S → ASB | AB
A → aAS | aA | a
B → SbS | Sb | bS | b | aAS | aA | a |bb
There is useless production rules.
Resultant grammar after removing mixed production rules:
S’ → ɛ
S’ → ASB | AB

Athmaranjan K Dept of ISE Page 70


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

S → ASB | AB
A → XAS | XA | a
B → SYS | SY | YS | b | XAS | XA | a |YY
X → a
Y → b
After removing long production rules, the resultant grammar is in CNF:
S’ → ɛ
S’ → AC | AB
S → AC | AB
A → XD | XA | a
B → SE | SY | YS | b | XD | XA | a |YY
A → XD | XA | a
X → a
Y → b
C → SB
D → AS
E → YS
Convert the following CFG to CNF:
E → E + T | T * F | (E) | a | b | Ia |Ib | I0 | I1
T → T * F | (E) | a | b | Ia |Ib | I0 | I1
F → (E) | a | b | Ia |Ib | I0 | I1
I → a | b | Ia |Ib | I0 | I1
Solution:
There is no ɛ, unit and useless production for the given grammar; so after removing mixed
production rules the resultant grammar G:
E → E P T | T M F | LER | a | b | IA |IB | IZ | IO
T → T M F | LER | a | b | IA |IB | IZ | IO
F → LER | a | b | IA |IB | IZ | IO
I → a | b | IA |IB | IZ | IO
A→a

Athmaranjan K Dept of ISE Page 71


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

B→b
Z→ 0
O→1
P→+
M→*
L→(
R →)
After removing the long productions, the resultant grammar is in CNF:
E → E C | T D | LP | a | b | IA |IB | IZ | IO
T → T D | LP | a | b | IA |IB | IZ | IO
F → LP | a | b | IA |IB | IZ | IO
I → a | b | IA |IB | IZ | IO
C → PT
D → MF
P →ER
A→a
B→b
Z→ 0
O→1
P→+
M→*
L→(
R →)
Begin with the grammar G:
S → ABaC
A → BC
B → b |ε
C → D|ε
D → d

Athmaranjan K Dept of ISE Page 72


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

i. Eliminate all ε - productions


ii. Eliminate any unit - productions in the resulting grammar
iii. Eliminate any useless - productions in the resulting grammar
iv. Put the resulting grammar into Chomsky Normal Form
After eliminating ε – productions, the resultant grammar is:
Nullable variables: {B, C, A}
S → ABaC | BaC | AaC |ABa |aC| Aa|Ba|a
A → BC |B|C
B → b
C → D
D → d
After eliminating unit – productions, the resultant grammar is:
S → ABaC | BaC | AaC |ABa |aC| Aa|Ba|a
A → BC |b|d
B → b
C → d
D → d
Now eliminating useless– productions, the resultant grammar is:
S → ABaC | BaC | AaC |ABa |aC| Aa|Ba|a
A → BC |b|d
B → b
C → d
After removing mixed productions:
S → ABXC | BXC | AXC |ABX |XC| AX|BX|a
A → BC |b|d
B → b
C → d
X → a

Athmaranjan K Dept of ISE Page 73


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

After removing long productions the resultant grammar is in CNF:


S → AT | BP | AP |AR |XC| AX | BX |a
A → BC | b |d
B → b
C → d
X → a
P → XC
R → BX
T → BP
Begin with the grammar G
S → a | aA | B | C
A → aB |ε
B → Aa
C → cCD
D → add
i. Eliminate all ε - productions
ii. Eliminate any unit - productions in the resulting grammar
iii. Eliminate any useless - productions in the resulting grammar
iv. Put the resulting grammar into Chomsky Normal Form.
After eliminating ε – productions, the resultant grammar is:
Nullable variables: {A}
S → a | aA | B | C
A → aB
B → Aa | a
C → cCD
D → add
After eliminating unit – productions, the resultant grammar is:
S → a | aA | Aa | cCD
A → aB
B → Aa | a

Athmaranjan K Dept of ISE Page 74


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

C → cCD
D → add
Now eliminating useless– productions, the resultant grammar is:
Generating symbols {a, d, B, D, A, S }, so eliminate symbol C and its rule.
Reachable symbols are { S, A, B, a} so eliminate symbol D and its rule. The resultant grammar:
S → a | aA | Aa
A → aB
B → Aa | a
After removing mixed rules:
S → a | XA | AX
A → XB
B → AX | a
X → a
Since there is no long productions, and all the rules are in CNF:
S → a | XA | AX
A → XB
B → AX | a
X → a
Convert the grammar G into CNF:
S → ABa
A → aab
B → Ac
The grammar in CNF:
S → AP
A → XR
B → AZ
X→a
Y→b
Z→ c
P → BX

Athmaranjan K Dept of ISE Page 75


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

R→ XY
Convert the grammar G into CNF:
S → aBa | abba
A → ab | AA
B → aB | a
There is no ɛ and unit productions but it contains A as a useless symbol, which is not reachable
from S. After removing useless productions the resultant grammar G:
S → aBa | abba
B → aB | a
The grammar in CNF:
S → XP | XT
B → XB | a
X→a
Y→b
P → BX
R → YX
T → YR
Convert the grammar G into CNF:
S → ~S | [S∩S] | p |q ( S being the only variable)
After removing mixed productions:
S → NS | LSISR| p | q
N→ ~
I→ ∩
L→[
R →]
Grammar in CNF:
S → NS | LZ | p | q
X→SR
Y →IX
Z→SY

Athmaranjan K Dept of ISE Page 76


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

N→ ~
I→ ∩
L→[
R →]
Exercises
Let L = {a. b} For the languages that are defined by each of the following grammars, do each of
the following:
i. List five strings that are in L.
ii. List five strings that are not in L

Answer:
a)
i. L = { ε, a, b, aaabbbb, ab }
ii. L= {. ba, bbaa, bbbbba, ababab, aba}
b)
i. L = { a, b, aaa, bbabb, aaaabaaaa }
ii. L= {ε, ab, bbbbbbba, bb, bbbaaa}
c)
i .L ={ ε, a, aa, aaa, ba}
ii. There aren’t any over the alphabet {a, b}.
d)
i. L= {ε, a, aaa, aaba, aaaabbbb}
ii. L= {b, bbaa, abba, bb}
Consider the following grammar G:
S→0S1 |SS | 10
Show a parse tree produced by G for the string: 010110.

Athmaranjan K Dept of ISE Page 77


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

Parse tree for string 010110:

Write the CFG for the language:


L = { ai bj ck | i ≠ j or j≠ k where i, j, k ≥ 0}

Convert the following CFG to CNF


S → aSa
S→B
B →bbC
B →bb
C →ɛ
C →cC
After eliminating ɛ production rules:

Athmaranjan K Dept of ISE Page 78


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

Nullable variables = {C}


Resultant grammar G:
S → aSa
S→B
B →bbC | bb
C →cC | c
After eliminating unit production rules:
S → aSa | bbC | bb
B →bbC | bb
C →cC | c
After eliminating useless production rules:
S → aSa | bbC | bb
C →cC | c
After eliminating mixed production rules:
S → XSX| YYC | YY
C →ZC | c
X→ a
Y→b
Z→c
After eliminating long production rules the resultant grammar is in CNF:
S → XD| YE | YY
C →ZC | c
X→ a
Y→b
Z→c
D→ SX
E → YC
Z→c

Athmaranjan K Dept of ISE Page 79


Automata Theory and Computability Normal Forms for CFG: Chomsky Normal Form

GREIBACH NORMAL FORM ( GNF):


What is GNF ? Give an example.
A grammar G = ( V, T, P, S) is said to be in GNF, if it has the productions of the form:
A → aα where “a” is Terminal symbol and A is variable, and “α” is any number of variable. ie:
α € V*
Example: A → aABCC or A→ a or
Note: In GNF, right hand side of the production rule contains one terminal symbol followed by
zero or more number of Variables.
Convert the following grammar to GNF:
S → AB
A → aA | bB | b
B→b
There are no ε, unit and useless productions, so the grammar in GNF:
S → aAB | bBB | bB
A → aA | bB | b
B→b

Athmaranjan K Dept of ISE Page 80


Automata Theory and Computability Push Down Automata (PDA)

PUSH DOWN AUTOMATA


A pushdown automaton or PDA is a finite state machine that has been augmented by a single
stack.
A pushdown automaton (PDA) is a type of automaton that employs a stack. Pushdown
automata are used in theories about what can be computed by machines. They are more capable
than finite-state machines but less capable than Turing machines. Context free language can be
described using context free grammars. These context free languages have a type of automaton
which accepts them. This automaton, called a “pushdown automaton”, is an extension of ε- NFA
with a stack.
A DFA or NFA is not powerful enough to recognize many context free languages, since it has
finite memories, and it cannot count and cannot store the input for future reference. We have a
new machine called Push-Down Automata (PDA) similar to finite automata with an exception
that PDA has an extra stack. So the definition of PDA is similar to the definition of ε- NFA with
slight changes.
Types of PDA:
There are two types of PDA:
1. Deterministic PDA (DPDA)
2. Non deterministic PDA (NPDA)
Deterministic pushdown automata can recognize all deterministic context-free languages while
Non-deterministic PDA can recognize all context-free languages. Mainly the former are used in
parser design.
Explain a PDA with a neat diagram
***Definition of PDA or Non deterministic PDA
A Pushdown automaton has seven components, say M = ( K, Σ, Γ , δ, s, Z 0, A ) where
K : A finite set of states.

Σ : A finite set of input symbols or alphabets.

Γ : A finite stack alphabet.

δ : The transition function, takes as argument in a triplet form as δ ( q, a, X )


where q is a state in K.

Athmaranjan K Dept. of ISE Page 81


Automata Theory and Computability Push Down Automata (PDA)

a is either an input symbol in Σ or a = ε, the empty string, which is assumed not to be an input
symbol. δ which maps from K x {∑ U ε} x Г → 2 K x Г *

X is a stack symbol, that is a member of Γ.


s : The start state.

Z0 : Initial stack symbol.

A : Set of accepting states or final states.


Block diagram of PDA:

A finite state control reads inputs, one symbol at a time. The PDA is allowed to observe the
symbol at the top of the stack and to base its transition on its current state, the input symbol and
the symbol at the top of stack.
1. It consumes the input symbol that it uses in the transition. If ε is used for the input, then
no input symbol is consumed.
2. Goes to a new state, which may or may not be the same as the previous state.
3. Replaces the symbol at the top of the stack by any string. The string could be ε, which
corresponds to a pop of the stack. It could be the same symbol that appeared at the top of
the stack previously.
Pushdown automata choose a transition by indexing a table by input signal, current state, and the
symbol at the top of the stack. This means that those three parameters completely determine the
transition path that is chosen. Finally the given input string can be accepted by some final state or
it can be rejected, decided by the output unit.

Athmaranjan K Dept. of ISE Page 82


Automata Theory and Computability Push Down Automata (PDA)

A Graphical Notation for PDA’s:


The transition diagram for PDA’s in which:
a. The nodes correspond to the states of the PDA.
b. An arrow labeled start indicates the start state, and doubly circled states are accepting
states, as for finite automata.
The arcs correspond to transitions of the PDA in the following sense. An arc labeled a, X/α from
state q to state p means that δ (q, a, X) contains the pair (p, α). It tells what input is used, and
also gives the old and new tops of the stack.
Instantaneous Descriptions of a PDA (I D):
How PDA processes the input string, that means the PDA goes from configuration to
configuration, in response to input symbols (or ε) can be represented using Instantaneous
Descriptions of PDA.
Definition of Instantaneous Descriptions (ID) :
Let M = ( K, Σ, Γ , δ, s, Z0, A ) be a PDA, the Instantaneous Descriptions of a PDA has a triplet
form (q, w, γ ) where q is the state. w is the remaining input, and γ is the stack contents.
Example: let the current configuration of PDA be (q, aw, Zα), it means
q is the current state. aw is the string to be processed. Zα is current content of the stack with Z as
the topmost symbol on the stack.

(q, aw, Zα ) (p, w, βα) means that the current configuration of PDA will be (p, aw, Zα ) and
after applying zero or more number of transitions, the PDA enters into new configuration (p, aw,
Zα ).

Note: means by applying one or more transitions

Athmaranjan K Dept. of ISE Page 83


Automata Theory and Computability Design of PDA

Alternative Equivalent definition of PDA


The Languages of a PDA:
There are two ways in which PDA accept the language:
1. Acceptance by final state: After consuming the input string, if a PDA enters an
accepting state, then we call this approach as acceptance by final state.
2. Acceptance by empty stack: Set of input string that cause the PDA to empty its stack,
starting from initial ID.
****Discuss the Languages accepted by PDA
Acceptance by Final state: let M = ( K, Σ, Γ, δ, s, Z0, A ) be a PDA. Then the language accepted by

PDA M by final state is L(M) = { w | (q0, w, Z0) (q, ε, α ) } for some state q in A, start state s =
q0 and any stack string α.
Acceptance by Empty stack state: let M = ( K, Σ, Γ, δ, s, Z0, A ) be a PDA. Then the language

accepted by PDA M by empty stack is L(M) = { w | (q0, w, Z0) (q, ε, ε ) } from s = q0 to any
state q. That is, L(M) is the set of inputs w that PDA M can consume and at the same time empty
its stack.

PDA design problems


Design a PDA to accept the language L = {anbn | n ≥ 0 } . Draw the graphical representation of
PDA obtained. Also write the ID for the string „aaabbb‟.

Logic: Since language contains strings of „n‟ number of a‟s followed by „n‟ number of b‟s,
machine can read n number of „a‟s in start state. Let us push all the scanned input symbol ‘a’ onto
the stack. When machine encounter input string as „b‟, we should see that for each „b‟ input, there
should be corresponding symbol ‟a‟ on the stack. Finally if there is no input (ε) and stack is empty, it
indicates that the string scanned has n number of „a‟s followed by n number of „b‟s.
PDA to accept L = { anbn | n ≥ 0 } is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, a, a) = (q0, aa)

Athmaranjan K Dept of ISE Page 84


Automata Theory and Computability Design of PDA

δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε) K = { q0, q1, qf }, s = q0 is the start state, Z0 is the initial stack symbol
δ(q1, ε, Z0) = (qf, Z0) Σ = { a, b}, Γ = { a, Z0 } and A = { qf }
δ(q0, ε, Z0) = (qf, Z0) (for minimum value; when n =0)
Graphical representation ( Transition diagram) :

Instantaneous Description for the string “aaabbb”


(q0, aaabbb, Z0 ) (q0, aabbb, aZ0 ) (q0, abbb, aaZ0 ) (q0, bbb, aaaZ0 ) (q1, bb, aaZ0 ) (q1, b, aZ0 )
(q1, ε, Z0 ) (qf, Z0 )

*******Design a PDA to accept the language L = { an bn | n ≥ 0 } by empty stack method.


Note: Procedure remains same as previous problem, only the changes in final state transition
function. That is once the end of input string is encountered (ε) , the stack should be empty. Here the
final state is irrelevant.
Transition function for PDA to accept L = {anbn | n ≥ 0 } by empty stack is given by:
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε) K = { q0, q1 }, s = q0 is the start state, Z0 is the initial stack symbol
δ(q1, ε, Z0) = (q1, ε) Σ = { a, b}, Γ = { a, Z0 } and A = Ø
δ(q0, ε, Z0) = (q1, ε) (for minimum value; when n =0)

Athmaranjan K Dept of ISE Page 85


Automata Theory and Computability Design of PDA

******NOTE: In design of PDA problems, if particular method for language acceptance is


not specified then use Final state acceptance method.

Design a PDA to accept the language L = { w | w € { (, ) }* where w is a balanced parentheses}

Logic: Since language contains strings of „n‟ number of (‟s followed by „n‟ number of )‟s, machine
can read n number of „(‟s in start state. Let us push all the scanned input symbol ‘(’ onto the stack.
When machine encounter input string as „)‟, we should see that for each „)‟ input, there should be
corresponding symbol ‟(‟ on the stack. Finally if there is no input (ε) and stack is empty, it indicates
that the string scanned has n number of „(‟s followed by n number of „)‟s.
PDA to accept L = { w | w € { (, ) }* where w is a balanced parentheses }is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, (, Z0) = (q0, (Z0)
δ(q0, (, ( ) = (q0, (( )
δ(q0, ), ( ) = (q1, ε)
δ(q1, ), ( ) = (q1, ε) K = { q0, q1, qf }, s = q0 is the start state, Z0 is the initial stack symbol
δ(q1, ε, Z0) = (qf, Z0) Σ = { (, )}, Γ = { (, Z0 } and A = { qf }
δ(q0, ε, Z0) = (qf, Z0) (for minimum value; when n =0)

Design a PDA to accept the language L = { anb2n | n ≥ 0 } . Draw the transition diagram and also
write the moves made by PDA for the string „aabbbb‟.

Logic: Since language contains strings of „n‟ number of a‟s followed by „2n‟ number of b‟s, machine
can read n number of „a‟s in start state. For each input symbol ‘a’ push two ‘a’s onto the stack. When
machine encounter input string as „b‟, we should see that for each „b‟ input, there should be
corresponding symbol ‟a‟ on the stack. Finally if there is no input (ε) and stack is empty, it indicates
that the string scanned has n number of „a‟s followed by 2n number of „b‟s.

Athmaranjan K Dept of ISE Page 86


Automata Theory and Computability Design of PDA

PDA to accept L = { an b2n | n ≥ 0 } is given by:


M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aaZ0)
δ(q0, a, a) = (q0, aaa) K = { q0, q1, qf }, s = q0 is the start state, Z0 is the initial stack symbol
δ(q0, b, a) = (q1, ε) Σ = { a, b}, Γ = { a, Z0 } and A = { qf }
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z0) = (qf, Z0)
δ(q0, ε, Z0) = (qf, Z0) (for minimum value; when n =0)
Graphical representation (Transition diagram) :

Moves made by PDA for the string “aabbbb”:


(q0, aabbbb, Z0 ) (q0, abbbb, aaZ0 ) (q0, bbbb, aaaaZ0 ) (q1, bbb, aaaZ0 ) (q1, bb, aaZ0 ) (q1, b, aZ0
) (q1, ε, Z0 ) (qf, Z0 )

Athmaranjan K Dept of ISE Page 87


Automata Theory and Computability Design of PDA

Design a PDA to accept the language L = {02n 1n | n ≥ 1 } . Draw the transition diagram for the
constructed PDA. Also show the moves made by PDA for the string „000011‟.

Logic: Since language contains strings of „2n‟ number of 0‟s followed by „n‟ number of 1‟s, machine
can read 2n number of „0‟s in start state. In start state q0, let us push all the scanned input symbol ‘0’
onto the stack. When it reads „1‟ in q0, change the state to q1 and pop one „0‟ from stack. In state q1
without consuming any input (ε) symbol, change the state to q2 and pop one „0‟ from stack. In state q2
machine reads input symbol as „1‟ and change the state to q1, pop one‟0‟ from stack and this process is
repeated. When machine encounter, there is no more input(ε) in state q2 and stack is empty, change the
state to final state qf. It indicates that the string scanned has 2n number of „0‟s followed by n number of
„1‟s.

PDA to accept L = {02n 1n | n ≥ 1 } is given by:


M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, 0, Z0) = (q0, 0Z0)
δ(q0, 0, 0) = (q0, 00)
δ(q0, 1, 0) = (q1, ε)
δ(q1, ε, 0) = (q2, ε)
δ(q2, 1, 0) = (q1, ε)
δ(q2, ε, Z0) = (qf, Z0) K = { q0, q1, q2, qf }, s = q0 is the start state, Z0 is the initial stack symbol
Σ = { 0, 1}, Γ = { 0, Z0 } and A = { qf }
Transition diagram:

Athmaranjan K Dept of ISE Page 88


Automata Theory and Computability Design of PDA

Moves made by PDA for the string „000011‟:


(q0, 000011, Z0) (q0, 00011, 0Z0) (q0, 0011, 00Z0) (q0, 011, 000Z0) (q0, 11, 0000Z0) (q1, 1, 000Z0)
(q2, 1, 00Z0) (q1, ε, 0Z0) (q1, ε, 0Z0) (q2, ε, Z0) (qf, Z0)

Design a PDA to accept the language L = {w | w € (a + b)* and Na(w) = Nb(w) } . Draw the
transition diagram for the constructed PDA. Also show the moves made by PDA for the string
abbaaabb

Procedure: The first scanned input symbol is either „a‟ or „b‟, push that symbol onto the stack. From
this point onwards, if the scanned input symbol and the top of stack symbol are same, then push that
current input symbol onto the stack. If the input symbol and top of stack symbol are different, then pop
one symbol from stack and repeat the process. Finally, when end of string is encountered, if the stack is
empty, we say that the string w has equal number of „a‟s and „b‟s otherwise number of „a‟s and „b‟s are
different.
PDA to accept L = { w | w € ( a + b)* and Na(w) = Nb(w) } is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, b, Z0) = (q0, bZ0)
δ(q0, a, a) = (q0, aa)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ε)
δ(q0, b, a) = (q0, ε)
δ(q0, ε, Z0) = (qf, Z0) K = { q0, qf }, s = q0 is the start state, Z0 is the initial stack symbol
Σ = { a, b}, Γ = { a, b, Z0 } and A = { qf }

Athmaranjan K Dept of ISE Page 89


Automata Theory and Computability Design of PDA

Transition diagram:

Moves made by PDA for the string “abbaaabb”:


(q0, abbaaabb, Z0) (q0, bbaaabb, aZ0) (q0, baaabb, Z0) (q0, aaabb, bZ0) (q0, aabb, Z0) (q0, abb,
aZ0) (q0, bb, aaZ0) (q0, b, aZ0) (q0, ε, Z0) (qf, Z0)

Design a PDA to accept the language L = {w | w € ( a + b)* and Na(w) > Nb(w)}. Draw the
transition diagram for the constructed PDA. Also show the moves made by PDA for the string
„baaabbaa‟.

Note: Procedure remains same as of previous problem, only the changes in final state transition
function. That is once the end of input string is encountered (ε), the stack should contain at least one
„a‟. From this point onwards change state to q1, keep on popping the symbol „a‟ from stack until stack
gets empty. When stack is empty (Z0), input is already empty, so go to final state and accept the
language.
PDA to accept L = { w | w € ( a+ b)* and Na(w) > Nb(w) } is given by:
M= ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, b, Z0) = (q0, bZ0)
δ(q0, a, a) = (q0, aa)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ε)
δ(q0, b, a) = (q0, ε)

Athmaranjan K Dept of ISE Page 90


Automata Theory and Computability Design of PDA

δ(q0, ε, a) = (q1, ε)
δ(q1, ε, a) = (q1, ε)
δ(q1, ε, Z0) = (qf, Z0) K = { q0, q1, qf }, s = q0 is the start state, Z0 is the initial stack symbol
Σ = { a, b}, Γ = { a, b, Z0 } and A = { qf }
Transition diagram:

Moves made by PDA for the string „baaabbaa‟:


(q0, baaabbaa, Z0 ) (q0, aaabbaa, bZ0 ) (q0, aabbaa, Z0 ) (q0,abbaa, aZ0 ) (q0, bbaa, aaZ0 )
(q0, baa, aZ0 ) (q0, aa, Z0 ) (q0, a, aZ0 ) (q0, ε, aaZ0 ) (q1, ε, aZ0 ) (q1, ε, Z0 ) (qf, Z0 )

Design a PDA to accept the language L = {w | w € ( a + b)* and Na(w) < Nb(w) }. Draw the
transition diagram for the constructed PDA. Also show the moves made by PDA for the string
„aabbbbab‟.

Note: Procedure remains same as that of Na(w) = Nb(w) problem, only the changes in final state
transition function. That is once the end of input string is encountered (ε), the stack should contain
at least one „b‟. From this point onwards change state to q1, keep on popping the symbol „b‟ from stack
until stack gets empty. When stack is empty (Z0), input is already empty, so go to final state and accept
the language.
PDA to accept L = {w | w € ( a + b)* and Na(w) < Nb(w) } is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, b, Z0) = (q0, bZ0)
Athmaranjan K Dept of ISE Page 91
Automata Theory and Computability Design of PDA

δ(q0, a, a) = (q0, aa)


δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ε)
δ(q0, b, a) = (q0, ε)
δ(q0, ε, b) = (q1, ɛ)
δ(q1, ε, b) = (q1, ɛ)
δ(q1, ε, Z0) = (qf, Z0)
Where K = {q0, q1, qf}, s = q0 is the start state, Z0 is the initial stack symbol ;
Σ = { a, b}, Γ = { a, b, Z0 } and A = { qf }
Transition diagram:

Moves made by PDA for the string „aabbbbab‟.


(q0, aabbbbab, Z0 ) (q0, abbbbab, aZ0 ) (q0, bbbbab, aaZ0 ) (q0, bbbab, aZ0 ) (q0, bbab, Z0 )
(q0, bab, bZ0 ) (q0, ab, bbZ0 ) (q0, b, bZ0 ) (q0, ε, bbZ0 ) (q1, ε, bZ0 ) (q1, ε, Z0 ) (qf, Z0 )

Design a PDA to accept the language L = { wCwR | w € ( a+b) * } . Draw the transition diagram and
also write the moves made by PDA for the string “baaCaab”

Logic: To check for palindrome, let us push all scanned input symbols onto the stack till we encounter
the letter C. Once we pass the middle string, if the string is palindrome, for each scanned input symbol,
there should be a corresponding symbol (same as input symbol) on the stack. Finally if there is no
input and stack is empty, we say that the given string is palindrome.

Athmaranjan K Dept of ISE Page 92


Automata Theory and Computability Design of PDA

PDA to accept L = { wCwR | w € ( a + b)* } is given by:


M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, b, Z0) = (q0, bZ0)
δ(q0, a, a) = (q0, aa)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ab)
δ(q0, b, a) = (q0, ba)

δ(q0, C, a) = (q1, a) δ(q0, C, Z0) = (q1, Z0)


δ(q0, C, b) = (q1, b)

δ(q1, a, a) = (q1, ε)
δ(q1, b, b) = (q1, ε)
δ(q1, ε, Z0) = (qf, Z0)
K = { q0, q1, qf }, s = q0 is the start state, Z0 is the initial stack symbol; Σ = { a, b, C}, Γ = { a, b, Z0 }
and A = { qf }
Transition diagram:

Moves made by PDA for the string “baaCaab”:


(q0, baaCaab, Z0 ) (q0, aaCaab, bZ0 ) (q0, aCaab, abZ0 ) (q0, Caab, aabZ0 ) (q1, aab, aabZ0 )
(q1, ab, abZ0 ) (q1, b, bZ0 ) (q1, ε, Z0 ) (qf, Z0 )

Athmaranjan K Dept of ISE Page 93


Automata Theory and Computability Design of PDA

Design an NPDA to accept the language L = { wwR | w € ( a+b) * } . Draw the transition diagram and
also write the moves made by PDA for the string “baaaab”

Logic: To check for palindrome, let us push all scanned input symbols onto the stack till we encounter
the midpoint (through our common sense). Once we pass the middle string, if the string is palindrome,
for each scanned input symbol, there should be a corresponding symbol (same as input symbol) on the
stack. Finally if there is no input and stack is empty, we say that the given string is palindrome.
Here in state q0 if the input symbol and top of stack symbol are same, we can push the input symbol
onto the stack, if the midpoint is not reached otherwise if midpoint is crossed we can change the state
and pop one symbol from stack.
PDA to accept L = { wwR | w € ( a + b)* } is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, b, Z0) = (q0, bZ0) δ(q0, ε, Z0) = (qf, Z0)
δ(q0, a, a) = (q0, aa)
δ(q0, b, b) = (q0, bb )
δ(q0, a, b) = (q0, ab)
δ(q0, b, a) = (q0, ba)
δ(q0, ε, a) = (q1, a)
δ(q0, ε, b) = (q1, b)
δ(q1, a, a) = (q1, ε)
δ(q1, b, b) = (q1, ε)
δ(q1, ε, Z0) = (qf, Z0)
K = { q0, q1, qf }, s = q0 is the start state, Z0 is the initial stack symbol; Σ = { a, b,}, Γ = { a, b, Z0 }
and A= { qf }
Moves made by PDA for the string : baaaab
(q0, baaaab, Z0 ) (q0, aaaab, bZ0 ) (q0, aaab, abZ0 ) (q0, aab, aabZ0 ) (q1, aab, aabZ0 ) (q1, ab,
abZ0 ) (q1, b, bZ0 ) (q1, ε, Z0 ) (qf, Z0 )

Athmaranjan K Dept of ISE Page 94


Automata Theory and Computability Design of PDA

Transition diagram:

Design a PDA to accept the language L = { 0 n1m0n | m, n ≥ 1 } . Draw the transition diagram and
also write the moves made by PDA for the string “0011100”.

Procedure: Initially (q0) machine reads n number of „0‟s, push all the scanned input symbol „0‟ onto
the stack, when machine reads „1‟ in start state, change the state to q1, and do not alter the content of
stack. In q1 state machine reads „1‟s and ignore that symbol. When machine reads „0‟ in q1 state, we
should see that for each scanned input symbol „0‟ there should be a corresponding symbol „0‟ on the
stack, so change the state to q2 and pop one „0‟ from stack. Finally if there is no input (ε) and stack is
empty, we say that string w has n number of „0‟s followed by „m‟ number of „1‟s followed by „n‟
number of „0‟s.
PDA to accept L = { 0n1m0n | m, n ≥ 1 } . is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, 0, Z0) = (q0, 0Z0)
δ(q0, 0, 0) = (q0, 00)
δ(q0, 1, 0) = (q1, 0)
δ(q1, 1, 0) = (q1, 0)
δ(q1, 0, 0) = (q2, ε)
δ(q2, 0, 0) = (q2, ε)

Athmaranjan K Dept of ISE Page 95


Automata Theory and Computability Design of PDA

δ(q2, ε, Z0) = (qf, Z0)


K = { q0, q1, q2, qf }, q0 is the start state, Z0 is the initial stack symbol ; Σ = { 0,1,}, Γ = { 0, Z0 } and
A = { qf }
Moves made by PDA for the string “0011100”:
(q0, 0011100, Z0 ) (q0, 011100, 0Z0 ) (q0, 11100, 00Z0 ) (q1, 1100, 00Z0 ) (q1, 100, 00Z0 )
(q1, 00, 00Z0 ) (q2, 0, 0Z0 ) (q2, ε, Z0 ) (qf, Z0 )
Transition diagram:

Design a PDA to accept the language L = {0n1m0m1n | m, n ≥ 1 } . Draw the transition diagram and
also write the moves made by PDA for the string “0011100011”.
Procedure: Initially (q0) machine reads n number of „0‟s, push all the scanned input symbol „0‟ onto
the stack, when machine reads „1‟ in start state, change the state to q1, and push that input symbol onto
the stack. In q1 state machine reads as many number of „1‟s and push that symbol onto the stack. When
machine reads „0‟ in q1 state, we should see that for each scanned input symbol „0‟ there should be a
corresponding symbol „1‟ on the stack, so change the state to q2 and pop one „1‟ from stack. Again in
q2 machine reads „0‟s and each time we should see that for each scanned input symbol „0‟ there should
be a corresponding symbol „1‟ on the stack and pop one‟1‟ from stack. In q2 if machine reads „1‟s ,
then change state to q3 and we should see that for each scanned input symbol „1‟ there should be a
corresponding symbol „0‟ on the stack and pop one‟0‟ from stack Again in q3 machine reads remaining
„1‟s and each time pop one „0‟ from stack. Finally in q3 if there is no input (ε) and stack is empty, we
say that string w has n number of „0‟s followed by „m‟ number of „1‟s followed by „m‟ number of „0‟s
followed by „n‟ number of „1‟s.
PDA to accept L = { 0n1m0m1n | m, n ≥ 1 } . is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
Athmaranjan K Dept of ISE Page 96
Automata Theory and Computability Design of PDA

δ(q0, 0, Z0) = (q0, 0Z0)


δ(q0, 0, 0) = (q0, 00)
δ(q0, 1, 0) = (q1, 10)
δ(q1, 1, 1) = (q1, 11)
δ(q1, 0, 1) = (q2, ε)
δ(q2, 0, 1) = (q2, ε)
δ(q2, 1, 0) = (q3, ε)
δ(q3, 1, 0) = (q3, ε)
δ(q3, ε, Z0) = (qf, Z0)
K = { q0, q1, q2,q3, qf }, s = q0 is the start state, Z0 is the initial stack symbol; Σ = { 0,1,}, Γ = { 0,1,Z0
} and A = { qf }
Moves made by PDA for the string “0011100011”:
(q0, 0011100011, Z0 ) (q0, 011100011, 0Z0 ) (q0,11100011, 00Z0 ) (q1, 1100011, 100Z0 )
(q1, 100011, 1100Z0 ) (q1, 00011, 11100Z0 ) (q2, 0011, 1100Z0 ) (q2, 011, 100Z0 ) (q2, 11, 00Z0 )
(q3, 1, 0Z0 ) (q3, ε, Z0 ) (qf, Z0 )
Graphical representation of PDA (Transition diagram):

Athmaranjan K Dept of ISE Page 97


Automata Theory and Computability Deterministic PDA

Deterministic Push Down Automata: (DPDA):


A PDA M = ( K, Σ, Γ , δ, s, Z0, A ) to be deterministic PDA, if and only if the following
conditions are met:
1. δ(q, a, X) has at most one member for any state q, where q € K, a € Σ or it can be ε
and X is any stack symbol (γ)
2. For any q € K , X € Γ and a € ∑ . If δ(q, a, X) is nonempty, then δ(q, ε, X) must be
empty.

Is the PDA corresponding to the language L = { an bn | n > = 1 } by final state is deterministic?


PDA to accept L = { an bn | n ≥ 1} is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, a, a) = (q0, aa)
δ(q0, b, a) = (q1, ε)
δ(q1, b, a) = (q1, ε)
δ(q1, ε, Z0) = (qf, Z0)
K = { q0, q1, qf }, q0 is the start state, Z0 is the initial stack symbol; Σ = { a, b}, Γ = { a, Z0 } and A
= { qf }
1. From the above transition functions, we observe that, the first condition to be deterministic is
δ(q, a, X) should have only one component. In this case, for each q in K, a in Σ and X in γ,
there exists only one definition. So the first condition is satisfied.
2. To satisfy the second condition, consider the transition δ(q0, a, a) = (q0, aa) is defined (non-
empty), then δ(q0, ε, a) should not be defined, which is true.
Since both the conditions are satisfied, the given PDA is Deterministic.

Is the PDA corresponding to the language L = { wCwR | w € ( a + b) * } by final state is


deterministic?
PDA to accept L = { wCwR | w € ( a + b)* } is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, b, Z0) = (q0, bZ0)
Athmaranjan K Dept. of ISE Page 98
Automata Theory and Computability Deterministic PDA

δ(q0, a, a) = (q0, aa)


δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ab)
δ(q0, b, a) = (q0, ba)

δ(q0, C, a) = (q1, a) δ(q0, C, Z0) = (q1, Z0)


δ(q0, C, b) = (q1, b)
δ(q1, a, a) = (q1, ε)
δ(q1, b, b) = (q1, ε)
δ(q1, ε, Z0) = (qf, Z0)
K = { q0, q1, qf }, s = q0 is the start state, Z0 is the initial stack symbol; Σ = { a, b, C}, Γ = { a, b, Z0 }
and A = { qf }
1. From the above transition functions, we observe that, the first condition to be deterministic is
δ(q, a, X) should have only one component. In this case, for each q in K, a in Σ and X in γ,
there exists only one transition. So the first condition is satisfied.
2. To satisfy the second condition, consider one transition function δ(q0, a, b) = (q0, ab) is
defined (non-empty), then δ(q0, ε, b) should not be defined, which is true.
Since both the conditions are satisfied, the given PDA is Deterministic.

Is the PDA corresponding to the language L = { w | w € ( a + b)* and Na(w) = Nb(w) } by final
state is deterministic?
PDA to accept L = { w | w € ( a + b)* and Na(w) = Nb(w) } is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, b, Z0) = (q0, bZ0)
δ(q0, a, a) = (q0, aa)
δ(q0, b, b) = (q0, bb)
δ(q0, a, b) = (q0, ε)
δ(q0, b, a) = (q0, ε)
δ(q0, ε, Z0) = (qf, Z0) K = { q0, qf }, s = q0 is the start state, Z0 is the initial stack symbol
Σ = { a, b}, Γ = { a, b, Z0 } and A = { qf }

Athmaranjan K Dept. of ISE Page 99


Automata Theory and Computability Deterministic PDA

1. From the above transition functions, we observe that, the first condition to be deterministic is
δ(q, a, X) should have only one component. In this case, for each q in K, a in Σ and X in γ,
there exists only one transition. So the first condition is satisfied.
2. To satisfy the second condition, consider one transition function δ(q0, a, Z0) = (q0, aZ0) is
defined (non-empty), then δ(q0, ε, Z0) should not be defined, which is not true.
Since the second condition fails, so the given PDA is Non-Deterministic.

Is the PDA corresponding to the language L = { wwR | w € ( a + b) *} by final state is


deterministic?
PDA to accept L = { wwR | w € ( a + b) * } is given by:
M = ( K, Σ, Γ , δ, s, Z0, A ) where δ is given by
δ(q0, a, Z0) = (q0, aZ0)
δ(q0, b, Z0) = (q0, bZ0) δ(q0, ε, Z0) = (qf, Z0)
δ(q0, a, a) = (q0, aa)
δ(q0, b, b) = (q0, bb )
δ(q0, a, b) = (q0, ab)
δ(q0, b, a) = (q0, ba)
δ(q0, ε, a) = (q1, a)
δ(q0, ε, b) = (q1, b)
δ(q1, a, a) = (q1, ε)
δ(q1, b, b) = (q1, ε)
δ(q1, ε, Z0) = (qf, Z0)
K = { q0, q1, qf }, s = q0 is the start state, Z0 is the initial stack symbol; Σ = { a, b,}, Γ = { a, b, Z0 }
and A= { qf }
1. From the above transition functions, we observe that, the first condition to be deterministic is
δ(q, a, X) should have only one component. In this case, for each q in K, a in Σ and X in γ,
there exists only one transition. So the first condition is satisfied.
2. To satisfy the second condition, consider one transition function δ(q0, a, a) = (q0, aa) is defined
(non-empty), then δ(q0, ε, a) should not be defined, which is not true.
Since the second condition fails, so the given PDA is Non-Deterministic.

Athmaranjan K Dept. of ISE Page 100


Automata Theory and Computability CFG to PDA using Top down parsing

Equivalence of Context-Free Grammars and PDAs


*** Building a PDA from CFG or Conversion of CFG to PDA:
We can build a PDA corresponding to CFG by using:
1.Top Down Parsing approach
2.Bottom Up parsing approach.
Conversion of CGF using Top down parsing :
A top-down parser answers the question; Could G generate w?" by starting with S, applying the rules
of P. and seeing whether ‘w can be derived.

Top Down parsing means Deriving the string by applying LMD.


• PDA M has only two states; The only purpose of the first state is to push S onto the stack and
then go to the second state. M's stack will actually do all the work by keeping track of what G
is trying derive.
• Conversion CFG to PDA begins by pushing the start symbol ‘S’ of G onto the stack without
reading any input symbol and then go to second state.
• In second state pop S from stack and replace S with the appropriate rule which is defined on
RHS of S.
• As long as the symbol on the top of the stack is a non terminal in G, this process continues.
effectively applying the rules of G to the top of the stack.

Athmaranjan K Dept. of ISE Page 101


Automata Theory and Computability CFG to PDA using Top down parsing

• The appearance of a terminal symbol c on the top of the stack means that G is attempting to
generate c.
• M only wants to pursue paths that generate its input string w. So at that point, it pops the top
symbol off the stack reads its next input character. and compares the two.
• If they match, the derivation that M is pursuing is consistent with generating-w and the
process continues.
• If they don't match, the path that M is currently following ends without accepting
• So at each step. PDA M either applies a grammar rule, without consuming any input, or it
reads an input character and pops one terminal symbol off the stack.
• When stack is empty, and PDA M has read all the characters of string w, G can generate
string w , so PDA M accepts.
Conversion of CFG to PDA using Top Down Parsing approach: Procedure
Formally, PDA M = ( {p, q, qf }, ∑, Г, δ, Z0 {p}, { qf } )
where δ contains:
1. The start up transition δ (p, ε, Z0) = (q, S) which pushes the start symbol of G onto the stack
and goes to state q.
2. For each production rule in G of the form:
X → γ1 γ2 γ3 …………… γn introduce the transition in
state q as:
δ (q, ε, X) = (q, γ1 γ2 γ3 …………… γn )
3. For each terminal symbol or character c € ∑ introduce the transition of the form:
δ (q, c, c) = (q, ε )
Construct an NPDA corresponding to the grammar given below by using Top down parsing
S → aA
A → aABC | bB | a
B → b
C → c
Show the sequence of moves made by NPDA in processing the string ‘aaabc’
For each Non terminal S, A, B and C, the transition functions are:
δ(q, ε, S) = { (q,aA) }
δ(q, ε, A) = { (q,aABC), (q,bB), (q,a) }

Athmaranjan K Dept. of ISE Page 102


Automata Theory and Computability CFG to PDA using Top down parsing

δ(q, ε, B) = { (q, b) }
δ(q, ε, C) = { (q, c) }
For each terminal a,b and c, the transition functions are:
δ(q, a, a) = { (q, ε) }
δ(q, b, b) = { (q, ε) }
δ(q, c, c) = { (q, ε) }
Sequence of moves for the string aaabc:
ID: (q, aaabc, S ) (q, aaabc, aA ) (q, aabc, A ) (q, aabc, aABC ) (q, abc, ABC ) (q, abc, aBC )
(q, bc, BC ) (q, bc, bC ) (q, c, C ) (q, c, c ) (q, ε, ε )
Obtain a PDA equivalent to the following grammar using Top down parsing approach.
E → E+E
E → E*E
E → E/E
E → E–E
E → (E) | a | b
For each Non terminal E the transition functions are:
δ(q, ε, E) = { (q, E + E), (q, E * E), (q, E / E), (q, E - E), (q, (E), (q, a), (q, b) }
For each terminal +, *, /, -, (, ), a and b the transition functions are:
δ(q, , +, + ) = { (q, ε) }

δ(q, , *, * ) = { (q, ε) }

δ(q, , /, / ) = { (q, ε) }
δ(q, , -, - ) = { (q, ε) }

δ(q, , (, ( ) = { (q, ε) }

δ(q, , ), ) ) = { (q, ε) }

δ(q, , a, a ) = { (q, ε) }

δ(q, , b, b ) = { (q, ε) }

Athmaranjan K Dept. of ISE Page 103


Automata Theory and Computability CFG to PDA using Top down parsing

Obtain a PDA equivalent to the following grammar.


S → aA
A → aA |bA|a|b
For each Non terminal S and A, the transition functions are:
δ(q, ε, S) = { (q, aA) }
δ(q, ε, A) = { (q, aA) , (q, bA), (q, a) , (q, b) }
For each terminal a and b the transition functions are:
δ(q, , a, a ) = { (q, ε) }

δ(q, , b, b ) = { (q, ε) }
Obtain a PDA equivalent to the following grammar.
S → aABB
A → aBB | a
B → bBB | A
C → a
For each Non terminal S, A,B and C, the transition functions are:
δ(q, ε, S) = { (q, aABB) }
δ(q, ε, A) = { (q, aBB) , (q, a) }
δ(q, ε, B) = { (q, bBB) , (q, A) }
δ(q, ε, C) = { (q, a) }
For each terminal a and b the transition functions are:
δ(q, , a, a ) = { (q, ε) }

δ(q, , b, b ) = { (q, ε) }
Obtain a PDA equivalent to the following grammar that accept the same language by empty stack.
S → 0S1 | A
A → 1A0 | S | ε
For each Non terminal S, and A, the transition functions are:
δ(q, ε, S) = { (q, 0S1) , (q, A) }
δ(q, ε, A) = { (q, 1A0) , (q, S), (q, ε) }
For each terminal 0 and 1 the transition functions are:
δ(q, , 0, 0 ) = { (q, ε) }

δ(q, , 1, 1 ) = { (q, ε) }
Athmaranjan K Dept. of ISE Page 104
Automata Theory and Computability CFG to PDA using Bottom up parsing

Conversion of CGF using Bottom Up parsing:


A bottom-up parser answers the question. "Could G generate w'?" by starting with w, applying the
rules of P backwards, and seeing whether S can be reached
Bottom Up parsing means Deriving the string by applying RMD in reverse

Parse tree for the string aab using Bottom Up parsing

• PDA M can read an input symbol and shift it onto the stack.
• Whenever a sequence of elements at the top of the stack matches, in reverse the right-hand
side of some rule in P. M can pop that sequence of and replace it by the left-hand side of P.
When this happens, we say that M has reduced by rule P

Conversion of CFG to PDA using Bottom Up parsing approach: Procedure


Formally, PDA M = ( {p, q}, ∑, Г, δ, Z0 {p}, { q} ) where δ contains:
1. The shift transition corresponding to input symbol (terminal symbol) δ (p, c, Y) = (p, cY) for
each c € ∑ and Y is any stack symbol including Z0. (either terminal or non-terminal symbol
of G)
2. The reduce transitions for each rule in P as; X → γ1 γ2 γ3 … γn introduce the transition of
the form:
δ (p, ε, (γ1 γ2 γ3 … γn)R )= (p, X)
3. The finish up transition : δ (p, ε, SZ0) = (q, Z0 )

Athmaranjan K Dept. of ISE Page 105


Automata Theory and Computability CFG to PDA using Bottom up parsing

Obtain a PDA equivalent to the following grammar using Bottom up parsing approach.
S → aA
A → aA | bA | a | b
PDA M = ( {p, q}, ∑, Г, δ, Z0 {p}, { q } ) where δ contains:

Obtain a PDA equivalent to the following grammar using Bottom Up parsing approach.

S → aABB
A → aBB | a
B → bBB | A
C → a|ε
PDA M = ( {p, q}, ∑, Г, δ, Z0 {p}, { q } ) where δ contains

Obtain a PDA equivalent to the following grammar using Bottom Up parsing approach
E → E+E
E → E*E
E → E/E
E → E–E
E → (E) | a | b

Athmaranjan K Dept. of ISE Page 106


Automata Theory and Computability CFG to PDA using Bottom up parsing

PDA M = ({p, q}, ∑, Г, δ, Z0 {p}, { q } ) where δ contains:

******NOTE: In conversion of CFG to PDA problem, if particular method is not specified


then better to use Top Down Parsing approach.

Athmaranjan K Dept. of ISE Page 107


Automata Theory and computability PDA to CFG

Building a Grammar from a PDA or Conversion of PDA to CFG:


Procedure:
1. The input symbols of PDA will be the terminal of CFG
2. If the PDA moves from state qi to qj on consuming the input a € Σ, when Z is the top of the
stack, then non-terminals of CFG are the triplets of the form (qiZqj )
3. If q0 is the start state and qf is the final state then (q0Zqf) is the start symbol of CFG.
4. The productions of CFG can be obtained from transitions of PDA as shown below
a. For each transition of the form δ(qi, , a, Z ) = (qj, AB) , introduce the productions of the
form
qiZqk → a (qjAql ) (qlBqk) where ql and qk will take all possible values from Q.
b. For each transition of the form δ(qi, , a, Z ) = (qj, ε) , introduce the production of the
form
qiZqj → a

Convert the following PDA to CFG


δ(q0, a, Z) = (q0, AZ)
δ(q0, a, A) = (q1, ε)
δ(q0, b, Z) = (q1, ε)
Solution: for transition function δ(q0, a, A) = (q1, ε) and δ(q0, b, A) = (q1, ε) introduce the
production as
q0Aq1 → a
q0Zq1 → b
Q = { q0, q1} ql and qk will take all values in Q.
δ(q0, a, Z) = (q, AZ) introduce the productions as
q0Zq0 → a (q0Aq0) (q0Zq0) | a (q0Aq1) (q1Zq0)
q0Zq1 → a (q0Aq0) (q0Zq1) | a (q0Aq1) (q1Zq1)

Athmaranjan K Dept. of ISE Page 108


Automata Theory and computability PDA to CFG

Find a CFG that generates the language accepted by NPDA P = ( {90, 91}, {a, b}, {A,Z}, δ, 90, Z,
{91})
With transitions δ( 90, a, Z) = { (90, AZ)}
δ( 90, b, A) = { (90, AA)}
δ( 90, a, A) = { (91, ε)}
Solution: For transition function δ( 90, a, A) = { (91, ε)} introduce the production as
90A91 → a
With transitions δ( 90, a, Z) = { (90, AZ)}
δ( 90, b, A) = { (90, AA)}introduce the productions as
90Z90→ a (90A90) (90Z90) | a (90A91) (91Z90)
90Z91→ a (90A90) (90Z91) | a (90A91) (91Z91)

90A90→ a (90A90) (90A90) | a (90A91) (91A90)


90Z91→ a (90A90) (90A91) | a (90A91) (91A91)

Athmaranjan K Dept. of ISE Page 109


Automata Theory and Computability Non-determinism and Halting

Non-determinism and Halting:


The computation of PDA halts on two conditions:
1. The computation C is an accepting computation.
2. The computation C ends in a configuration from which no δ transition is possible.
For accepting every regular language there exists finite state machine. But this is not true for context
free language and PDA. That means there are CFLs for which no DPDA exists.
It is possible that a PDA may:
1.Not halt
2.Not ever finish reading input.
It is also possible that some CFGs are acceptable only by NPDA, but not every NPDA be converted to
DPDA. For creating a PDA that accepts CFG, sometimes the rules of CFG are restricted in some form.
That means the CFG are converted to normal forms.
Alternatives that are not Equivalent to PDA
PDA is FSM with a stack, which allows push and pop operation. (LIFO). The two alternatives that
are not Equivalent to PDA:
1. Post Machine: Instead of stack, Queue data structure is used. (FIFO)
2. Turing Machine: Which allows to read the string in forward and backward directions and allow
to write the corresponding output string.

Athmaranjan K Dept. of ISE Page 110

You might also like