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

At Module-4

The document discusses pushdown automata and properties of context-free languages. It covers topics such as: 1) How pushdown automata can accept languages by final state or empty stack. 2) The formal definition of a pushdown automaton and its components. 3) How pushdown automata and context-free grammars are equivalent in the languages they accept. 4) Normal forms for context-free grammars including eliminating useless symbols and Chomsky normal form. 5) The pumping lemma for context-free languages and closure properties of context-free languages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

At Module-4

The document discusses pushdown automata and properties of context-free languages. It covers topics such as: 1) How pushdown automata can accept languages by final state or empty stack. 2) The formal definition of a pushdown automaton and its components. 3) How pushdown automata and context-free grammars are equivalent in the languages they accept. 4) Normal forms for context-free grammars including eliminating useless symbols and Chomsky normal form. 5) The pumping lemma for context-free languages and closure properties of context-free languages.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT – 4

Pushdown automata: The languages of a PDA – acceptance by final


state, acceptance by empty stack Informal introduction, formal definition
of PDA, a graphical notation for PDA’s, The languages of a PDA – from
empty stack to final state, from final state to empty stack, Equivalence of
PDA’s and CFG’s – from grammars to PDA, from PDA’s to grammars.

Properties of context free languages: Normal forms for context free


grammars – eliminating useless symbols, computing the generating and
reachable symbols, Eliminating -productions, eliminating unit
productions, Chomsky normal form (CNF), exercise problems, The
pumping lemma for context free languages, Closure properties of context
free languages.

Pushdown Automata:
4.1 The languages of a PDA – acceptance by final state,
4.2 acceptance by empty stack Informal
4.3 introduction formal definition of PDA,
4.4 a graphical notation for PDA’s,
4.5 The languages of a PDA – from empty stack to final state, from final state to empty stack,
4.6 Equivalence of PDA’s and CFG’s – from grammars to PDA, from PDA’s to grammars.

Properties of context free languages:


4.7 Normal forms for context free grammars – eliminating useless symbols, computing the
generating and reachable symbols
4.8 Eliminating -productions, eliminating unit productions
4.9 Chomsky normal form (CNF)
4.10 exercise problems
4.11 The pumping lemma for context free languages
4.12 Closure properties of context free languages.
Pushdown Automata:
4.1 The languages of a PDA – acceptance by final state,
4.2 acceptance by empty stack Informal introduction,
4.3 formal definition of PDA,
4.4 a graphical notation for PDA’s,
4.5 The languages of a PDA – from empty stack to final state, from final state to empty stack,
4.6 Equivalence of PDA’s and CFG’s – from grammars to PDA, from PDA’s to grammars.

Pushdown Automata(PDA)
o Pushdown automata is a way to implement a CFG in the same way we design
DFA for a regular grammar. A DFA can remember a finite amount of information,
but a PDA can remember an infinite amount of information.
o Pushdown automata is simply an NFA augmented with an "external stack
memory". The addition of stack is used to provide a last-in-first-out memory
management capability to Pushdown automata. Pushdown automata can store
an unbounded amount of information on the stack. It can access a limited
amount of information on the stack. A PDA can push an element onto the top of
the stack and pop off an element from the top of the stack. To read an element
into the stack, the top elements must be popped off and are lost.
o A PDA is more powerful than FA. Any language which can be acceptable by FA
can also be acceptable by PDA. PDA also accepts a class of language which
even cannot be accepted by FA. Thus PDA is much more superior to FA.
PDA Components:
Input tape: The input tape is divided in many cells or symbols. The input head is read-
only and may only move from left to right, one symbol at a time.

Finite control: The finite control has some pointer which points the current symbol
which is to be read.

Stack: The stack is a structure in which we can push and remove the items from one
end only. It has an infinite size. In PDA, the stack is used to store the items temporarily.

Formal definition of PDA:


The PDA can be defined as a collection of 7 components:

Q: the finite set of states

∑: the input set

Γ: a stack symbol which can be pushed and popped from the stack
q0: the initial state

Z: a start symbol which is in Γ.

F: a set of final states

δ: mapping function which is used for moving from current state to next state.

Instantaneous Description (ID)


ID is an informal notation of how a PDA computes an input string and make a decision
that string is accepted or rejected.

An instantaneous description is a triple (q, w, α) where:

q describes the current state.

w describes the remaining input.

α describes the stack contents, top at the left.

Turnstile Notation:
⊢ sign describes the turnstile notation and represents one move.

⊢* sign describes a sequence of moves.

For example,

(p, b, T) ⊢ (q, w, α)

In the above example, while taking a transition from state p to q, the input symbol 'b' is
consumed, and the top of the stack 'T' is represented by a new string α.

Example 1:
Design a PDA for accepting a language {anb2n | n>=1}.

Solution: In this language, n number of a's should be followed by 2n number of b's.


Hence, we will apply a very simple logic, and that is if we read single 'a', we will push
two a's onto the stack. As soon as we read 'b' then for every single 'b' only one 'a'
should get popped from the stack.

The ID can be constructed as follows:

1. δ(q0, a, Z) = (q0, aaZ)


2. δ(q0, a, a) = (q0, aaa)

Now when we read b, we will change the state from q0 to q1 and start popping
corresponding 'a'. Hence,

1. δ(q0, b, a) = (q1, ε)

Thus this process of popping 'b' will be repeated unless all the symbols are read. Note
that popping action occurs in state q1 only.

1. δ(q1, b, a) = (q1, ε)

After reading all b's, all the corresponding a's should get popped. Hence when we read
ε as input symbol then there should be nothing in the stack. Hence the move will be:

1. δ(q1, ε, Z) = (q2, ε)

Where

PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2})

We can summarize the ID as:

1. δ(q0, a, Z) = (q0, aaZ)


2. δ(q0, a, a) = (q0, aaa)
3. δ(q0, b, a) = (q1, ε)
4. δ(q1, b, a) = (q1, ε)
5. δ(q1, ε, Z) = (q2, ε)

Now we will simulate this PDA for the input string "aaabbbbbb".

1. δ(q0, aaabbbbbb, Z) ⊢ δ(q0, aabbbbbb, aaZ)


2. ⊢ δ(q0, abbbbbb, aaaaZ)
3. ⊢ δ(q0, bbbbbb, aaaaaaZ)
4. ⊢ δ(q1, bbbbb, aaaaaZ)
5. ⊢ δ(q1, bbbb, aaaaZ)
6. ⊢ δ(q1, bbb, aaaZ)
7. ⊢ δ(q1, bb, aaZ)
8. ⊢ δ(q1, b, aZ)
9. ⊢ δ(q1, ε, Z)
10. ⊢ δ(q2, ε)
11. ACCEPT

Example 2:
Design a PDA for accepting a language {0n1m0n | m, n>=1}.

Solution: In this PDA, n number of 0's are followed by any number of 1's followed n
number of 0's. Hence the logic for design of such PDA will be as follows:

Push all 0's onto the stack on encountering first 0's. Then if we read 1, just do nothing.
Then read 0, and on each read of 0, pop one 0 from the stack.

For instance:

This scenario can be written in the ID form as:


1. δ(q0, 0, Z) = δ(q0, 0Z)
2. δ(q0, 0, 0) = δ(q0, 00)
3. δ(q0, 1, 0) = δ(q1, 0)
4. δ(q0, 1, 0) = δ(q1, 0)
5. δ(q1, 0, 0) = δ(q1, ε)
6. δ(q0, ε, Z) = δ(q2, Z) (ACCEPT state)

Now we will simulate this PDA for the input string "0011100".

1. δ(q0, 0011100, Z) ⊢ δ(q0, 011100, 0Z)


2. ⊢ δ(q0, 11100, 00Z)
3. ⊢ δ(q0, 1100, 00Z)
4. ⊢ δ(q1, 100, 00Z)
5. ⊢ δ(q1, 00, 00Z)
6. ⊢ δ(q1, 0, 0Z)
7. ⊢ δ(q1, ε, Z)
8. ⊢ δ(q2, Z)
9. ACCEPT

PDA Acceptance
A language can be accepted by Pushdown automata using two approaches:

1. Acceptance by Final State: The PDA is said to accept its input by the final state if it
enters any final state in zero or more moves after reading the entire input.

Let P =(Q, ∑, Γ, δ, q0, Z, F) be a PDA. The language acceptable by the final state can
be defined as:

L(PDA) = {w | (q0, w, Z) ⊢* (p, ε, ε), q ∈ F}

2. Acceptance by Empty Stack: On reading the input string from the initial
configuration for some PDA, the stack of PDA gets empty.

Skip Ad
Let P =(Q, ∑, Γ, δ, q0, Z, F) be a PDA. The language acceptable by empty stack can be
defined as:

N(PDA) = {w | (q0, w, Z) ⊢* (p, ε, ε), q ∈ Q}

Equivalence of Acceptance by Final State and Empty Stack


o If L = N(P1) for some PDA P1, then there is a PDA P2 such that L = L(P2). That
means the language accepted by empty stack PDA will also be accepted by final
state PDA.
o If there is a language L = L (P1) for some PDA P1 then there is a PDA P2 such
that L = N(P2). That means language accepted by final state PDA is also
acceptable by empty stack PDA.

Example:
Construct a PDA that accepts the language L over {0, 1} by empty stack which accepts
all the string of 0's and 1's in which a number of 0's are twice of number of 1's.

Solution:

There are two parts for designing this PDA:

o If 1 comes before any 0's


o If 0 comes before any 1's.

We are going to design the first part i.e. 1 comes before 0's. The logic is that read single
1 and push two 1's onto the stack. Thereafter on reading two 0's, POP two 1's from the
stack. The δ can be

1. δ(q0, 1, Z) = (q0, 11, Z) Here Z represents that stack is empty


2. δ(q0, 0, 1) = (q0, ε)

Now, consider the second part i.e. if 0 comes before 1's. The logic is that read first 0,
push it onto the stack and change state from q0 to q1. [Note that state q1 indicates that
first 0 is read and still second 0 has yet to read].
Being in q1, if 1 is encountered then POP 0. Being in q1, if 0 is read then simply read
that second 0 and move ahead. The δ will be:

1. δ(q0, 0, Z) = (q1, 0Z)


2. δ(q1, 0, 0) = (q1, 0)
3. δ(q1, 0, Z) = (q0, ε)
(indicate that one 0 and one 1 is already read, so simply read the second 0)
4. δ(q1, 1, 0) = (q1, ε)

Now, summarize the complete PDA for given L is:

1. δ(q0, 1, Z) = (q0, 11Z)


2. δ(q0, 0, 1) = (q1, ε)
3. δ(q0, 0, Z) = (q1, 0Z)
4. δ(q1, 0, 0) = (q1, 0)
5. δ(q1, 0, Z) = (q0, ε)
6. δ(q0, ε, Z) = (q0, ε) ACCEPT state

4.7 Eliminating useless symbols from the productions


in a Context Free Grammar

In this tutorial we define those symbols that do not participate in derivation of any string, i.e.
the useless symbols, and remove the useless productions from the grammar.
A symbol X is useful if:

1. If X is generating, i.e., X => w, where w ϵ L(G) and w in V *, this means that the
*
t

string leads to a string of terminal symbols.


2. If X is reachable If there is a derivation S =>* αXβ => w, w ϵ L(G), for same α and β,
*

then X is said to be reachable.

A number that is useful is both generating and reachable. For reduction of a given grammar
G:

1. Identify non-generating symbols in the given CFG and eliminate those productions
which contains non-generating symbols.
2. Identify non-reachable symbols and eliminate those productions which contain the
non-reachable symbols
3.
Example: Remove the useless symbol from the given context free grammar:
S -> aB / bX
A -> Bad / bSX / a
B -> aSB / bBX
X -> SBD / aBx / ad

Solution:
A and X directly derive string of terminals a and ad, hence they are useful. Since X is a
useful symbol so S is also a useful symbol as S -> bX. But B does not derive any string w in
V* so clearly B is a non-generating symbol.So eliminating those productions with B in them
t

we get

S -> bX
A -> bSX / a
X -> ad

In the reduced grammar A is a non-reachable symbol so we remove it and the final


grammar after elimination of the useless symbols is

S -> bX
X -> ad

Question: Find the equivalent useless grammar from the given grammar

A -> xyz / Xyzz

X -> Xz / xYz

Y -> yYy / Xz

Z -> Zy / z

4.8 Eliminating unit productions from the productions


in the Context Free Grammar
A unit production is a production A -> B where both A and B are non-terminals. Unit
productions are redundant and hence should be removed. Follow the following steps to
remove the unit production.

Repeat the following steps while there is a unit production

1. Select a unit production A -> B, such that there exist a production B -> α, where α is
a terminal

2. For every non-unit production, B -> α repeat the following step


1. Add production A -> α to the grammar
3. Eliminate A -> B from the grammar

Example: Remove the unit productions from the following grammar

S -> AB

A -> a

B -> C / b

C -> D

D -> E

E -> a

Solution:

There are 3 unit production in the grammar

B -> C

C -> D

D -> E

For production D -> E there is E -> a so we add D -> a to the grammar and add D -> E from
the grammar. Now we have C -> D so we add a production C -> a tp the grammar and
delete C -> D from the grammar.
Similarly we have B -> C by adding B -> a and removing B -> C we get the final grammar
free of unit production as:

S -> AB

A -> a

B -> a / b

C -> a

D -> a

E -> a

We can see that C, D and E are unreachable symbols so to get a completely reduced
grammar we remove them from the CFG. The final CFG is :
S -> AB

A -> a
B -> a / b

Question: Identify and remove the unit productions from the following CFG

S -> S + T/ T

T -> T * F/ F

F -> (S)/a

4.9 Chomsky's Normal Form (CNF)


CNF stands for Chomsky normal form. A CFG(context free grammar) is in
CNF(Chomsky normal form) if all production rules satisfy one of the following
conditions:

o Start symbol generating ε. For example, A → ε.


o A non-terminal generating two non-terminals. For example, S → AB.
o A non-terminal generating a terminal. For example, S → a.

For example:
1. G1 = {S → AB, S → c, A → a, B → b}
2. G2 = {S → aA, A → a, B → c}

The production rules of Grammar G1 satisfy the rules specified for CNF, so the
grammar G1 is in CNF. However, the production rule of Grammar G2 does not satisfy
the rules specified for CNF as S → aZ contains terminal followed by non-terminal. So
the grammar G2 is not in CNF.

4.10 Steps for converting CFG into CNF


Step 1: Eliminate start symbol from the RHS. If the start symbol T is at the right-hand
side of any production, create a new production as:

1. S1 → S

Where S1 is the new start symbol.


32.6M
599
History of Java

Step 2: In the grammar, remove the null, unit and useless productions. You can refer to
the Simplification of CFG.

Step 3: Eliminate terminals from the RHS of the production if they exist with other non-
terminals or terminals. For example, production S → aA can be decomposed as:

1. S → RA
2. R → a

Step 4: Eliminate RHS with more than two non-terminals. For example, S → ASB can
be decomposed as:

1. S → RS
2. R → AS

Example:
Convert the given CFG to CNF. Consider the given grammar G1:

1. S → a | aA | B
2. A → aBB | ε
3. B → Aa | b

Solution:

Step 1: We will create a new production S1 → S, as the start symbol S appears on the
RHS. The grammar will be:

1. S1 → S
2. S → a | aA | B
3. A → aBB | ε
4. B → Aa | b

Step 2: As grammar G1 contains A → ε null production, its removal from the grammar
yields:

1. S1 → S
2. S → a | aA | B
3. A → aBB
4. B → Aa | b | a

Now, as grammar G1 contains Unit production S → B, its removal yield:

1. S1 → S
2. S → a | aA | Aa | b
3. A → aBB
4. B → Aa | b | a

Also remove the unit production S1 → S, its removal from the grammar yields:

1. S0 → a | aA | Aa | b
2. S → a | aA | Aa | b
3. A → aBB
4. B → Aa | b | a

Step 3: In the production rule S0 → aA | Aa, S → aA | Aa, A → aBB and B → Aa,


terminal a exists on RHS with non-terminals. So we will replace terminal a with X:

1. S0 → a | XA | AX | b
2. S → a | XA | AX | b
3. A → XBB
4. B → AX | b | a
5. X → a

Step 4: In the production rule A → XBB, RHS has more than two symbols, removing it
from grammar yield:

1. S0 → a | XA | AX | b
2. S → a | XA | AX | b
3. A → RB
4. B → AX | b | a
5. X → a
6. R → XB

Hence, for the given grammar, this is the required CNF.


4.11 Pumping Lemma for CFG

Lemma
If L is a context-free language, there is a pumping length p such that any string w ∈
L of length ≥ p can be written as w = uvxyz, where vy ≠ ε, |vxy| ≤ p, and for all i ≥ 0,
uv xy z ∈ L.
i i

Applications of Pumping Lemma


Pumping lemma is used to check whether a grammar is context free or not. Let us take
an example and show how it is checked.

Problem
Find out whether the language L = {x y z | n ≥ 1} is context free or not.
n n n

Solution
Let L is context free. Then, L must satisfy pumping lemma.
At first, choose a number n of the pumping lemma. Then, take z as 0 1 2 .
n n n

Break z into uvwxy, where


|vwx| ≤ n and vx ≠ ε.
Hence vwx cannot involve both 0s and 2s, since the last 0 and the first 2 are at least
(n+1) positions apart. There are two cases −
Case 1 − vwx has no 2s. Then vx has only 0s and 1s. Then uwy, which would have to
be in L, has n 2s, but fewer than n 0s or 1s.
Case 2 − vwx has no 0s.
Here contradiction occurs.
Hence, L is not a context-free language.
4.12 CFL Closure Property
Context-free languages are closed under −

 Union
 Concatenation
 Kleene Star operation

Union
Let L and L be two context free languages. Then L ∪ L is also context free.
1 2 1 2

Example
Let L = { a b , n > 0}. Corresponding grammar G will have P: S1 → aAb|ab
1
n n
1

Let L = { c d , m ≥ 0}. Corresponding grammar G will have P: S2 → cBb| ε


2
m m
2

Union of L and L , L = L ∪ L = { a b } ∪ { c d }
1 2 1 2
n n m m

The corresponding grammar G will have the additional production S → S1 | S2

Concatenation
If L and L are context free languages, then L L is also context free.
1 2 1 2

Example
Union of the languages L and L , L = L L = { a b c d }
1 2 1 2
n n m m

The corresponding grammar G will have the additional production S → S1 S2

Kleene Star
If L is a context free language, then L* is also context free.

Example
Let L = { a b , n ≥ 0}. Corresponding grammar G will have P: S → aAb| ε
n n

Kleene Star L = { a b }* 1
n n

The corresponding grammar G will have additional productions S1 → SS | ε


1 1

Context-free languages are not closed under −


 Intersection − If L1 and L2 are context free languages, then L1 ∩ L2 is not
necessarily context free.
 Intersection with Regular Language − If L1 is a regular language and L2 is a
context free language, then L1 ∩ L2 is a context free language.
 Complement − If L1 is a context free language, then L1’ may not be context
free.

Properties of context free languages:


4.7 Normal forms for context free grammars – eliminating useless symbols, computing the
generating and reachable symbols
4.8 Eliminating -productions, eliminating unit productions
4.9 Chomsky normal form (CNF)
4.10 exercise problems
4.11 The pumping lemma for context free languages
4.12 Closure properties of context free languages.

You might also like