You are on page 1of 57

Gaurav singh(2008ecs01)

Siddharth(2008ecs05)
Ranjan jha(2008ecs10)

1
 CFG’s were introduced by Noam Chomsky in
1956.
 Oettinger introduced PDA’s for parsing
applications in 1961.
 Chomsky, Schutzenberger, and Evey showed
equivalence of CFG’s and PDA’s in 1962.

2
 The finite automata(FA) is used only for finite number
of states and when there is bound number of states and
the type of combinations for the states and the input
symbol because finite automata has only limited
memory for processing the strings.
 So a more powerful machine is needed which not only
have unbounded values in certain cases but also
remember the symbol read from the input string.
 Hence for this purpose we use PDA.
 A push-down automaton (PDA) is a seven tuple
(Q, Σ, Γ, δ, q0, z0, F,) where
• Q is a finite set of states.
• Σ is a finite alphabet of tape symbols.
• Γ is a finite alphabet of stack symbols.
• q0 Q is the start state.
• F  Q is the set of final states.
• Z0 is start stack symbol.
• δ is the transition relation, i.e., δ is a finite subset of
δ :(Q  (Σ  ᴧ)  Γ*) —›(K  Γ*).
 Read current symbol and advance head;

Read and pop top-of-stack symbol

Push in a string of symbols on the stack.


Change state.

Each transition Looks like


(q0, a,X) —› (q1,Y1Y2 · · · Yk ).
General Principle in Computer Science: Any
recursive algorithm can be turned into a non-
recursive one using a stack and a while-loop which
exits only when stack is empty.
EG: JVM keeps stack of activation records
generated at each method call. Consider:
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}
What does the JVM do for factorial(5)?

6
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

Compute 5!

7
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

f(5)=
5·f(4)

8
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

f(4)= f(5)=
4·f(3) 5·f(4)

9
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

f(3)= f(4)= f(5)=


3·f(2) 4·f(3) 5·f(4)

10
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

f(2)= f(3)= f(4)= f(5)=


2·f(1) 3·f(2) 4·f(3) 5·f(4)

11
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

f(1)= f(2)= f(3)= f(4)= f(5)=


1·f(0) 2·f(1) 3·f(2) 4·f(3) 5·f(4)

12
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

f(0)= f(1)= f(2)= f(3)= f(4)= f(5)=


1 1·f(0) 2·f(1) 3·f(2) 4·f(3) 5·f(4)

13
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

1·1= f(2)= f(3)= f(4)= f(5)=


1 2·f(1) 3·f(2) 4·f(3) 5·f(4)

14
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

2·1= f(3)= f(4)= f(5)=


2 3·f(2) 4·f(3) 5·f(4)

15
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

3·2= f(4)= f(5)=


6 4·f(3) 5·f(4)

16
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

4·6= f(5)=
24 5·f(4)

17
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

5·24=
120

18
long factorial(int n){
if (n<=0) return 1;
return n*factorial(n-1);
}

Return 5!
= 120

19
By general principles, can carry out any
recursive computation on a stack. Can do it
on a restricted version of an activation record
stack, called a “Pushdown (Stack)
Automaton” or PDA for short.
Q: What is the language generated by
S  # | aSa | bSb ?

20
A: Palindromes in {a,b,#}* containing exactly
one #-symbol.
Q: Using a stack, how can we recognize such
strings?

21
A: Use a three phase process:
1. Push mode: Before reading “#”, push everything
on the stack.
2. Reading “#” switches modes.
3. Pop mode: Read remaining symbols making sure
that each new read symbol is identical to symbol
popped from stack.
Accept if able to empty stack completely. Otherwise
reject, and reject if could not pop somewhere.

22
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

23
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
aaab#baa
24
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a
aaab#baa
25
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a a
aaab#baa
26
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a a a
aaab#baa
27
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
b a a a
aaab#baa
28
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
b a a a
aaab#baa
29
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a a a
aaab#baa
30
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a a
aaab#baa
31
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a
aaab#baa
REJECT (nonempty stack)
32
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a a
aaab#baaa
33
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a
aaab#baaa
34
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
aaab#baaa
Pause input
35
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
aaab#baaa
ACCEPT
36
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a a
aaab#baaaa
37
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
a
aaab#baaaa
38
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
aaab#baaaa
Pause input
39
read == peek ?
read a or b ? Pop
Push it Else: CRASH!

(2) empty
(1) read # ? (3) stack?
(ignore stack) ACCEPT
PUSH POP

Input:
aaab#baaaa
CRASH
40
To aid analysis, theoretical stack machines restrict the
allowable operations. Each text-book author has his
own version. Sipser’s machines are especially simple:
 Push/Pop rolled into a single operation: replace top
stack symbol
 No intrinsic way to test for empty stack
 Epsilon’s used to increase functionality, result in
default nondeterministic machines.

41
read == peek ?
read a or b ? Pop
Push it Else: CRASH!
(2)
read # ? empty
(1) (ignore stack) (3) stack?
ACCEPT
PUSH POP
Becomes:
aa aa
bb bb

$ # $

42
x, y z
p q
Meaning of labeling convention:
If at p and next input x and top stack y,
then go to q and replace y by z on stack.
 x = : ignore input, don’t read
 y = : ignore top of stack and push z
 z = : pop y

43
aa aa
bb bb

$ # $

push $ to
detect
empty
stack

44
aa aa
bb bb

$ # $

Input:
aaab#baaa

45
aa aa
bb bb

$ # $

Input:
$
aaab#baaa

46
aa aa
bb bb

$ # $

Input:
a $
aaab#baaa

47
aa aa
bb bb

$ # $

Input:
a a $
aaab#baaa

48
aa aa
bb bb

$ # $

Input:
a a a $
aaab#baaa

49
aa aa
bb bb

$ # $

Input:
b a a a $
aaab#baaa

50
aa aa
bb bb

$ # $

Input:
b a a a $
aaab#baaa

51
aa aa
bb bb

$ # $

Input:
a a a $
aaab#baaa

52
aa aa
bb bb

$ # $

Input:
a a $
aaab#baaa

53
aa aa
bb bb

$ # $

Input:
a $
aaab#baaa

54
aa aa
bb bb

$ # $

Input:
$
aaab#baaa

55
aa aa
bb bb

$ # $

Input:
aaab#baaa
ACCEPT!

56
57

You might also like