Theory of
Computation
Automata Theory
Dr. Ayman Srour
4. Introduction
• We had two ways to describe regular languages
regular
expression
syntactic
NFA
DFA
computational
• How about context-free languages?
CFG
pushdown automaton
syntactic
computational
4.1 Introduction
• Pushdown automata versus NFA
• Since context-free is more powerful than regular,
pushdown automata must generalize NFAs
state control
NFA
input
0 1
0
0
4.1 Introduction
• Pushdown automata
• A pushdown automaton has access to a stack, which is a
potentially infinite supply of memory
state control
input
0 1
0
0
…
stack
pushdown automaton (PDA)
4.1 Introduction
• Pushdown automata
• As the PDA is reading the input, it can push / pop
symbols in / out of the stack
state control
input
0 1
10
stack
Z0
0
0
1
pushdown automaton (PDA)
…
4.2 Rules for pushdown automata
• The transitions are nondeterministic
• Stack is always accessed from the top
• Each transition can pop a symbol from the stack
and / or push another symbol onto the stack
• Transitions depend on input symbol and on last
symbol popped from stack
• Automaton accepts if after reading whole input, it
can reach an accepting state
4.3 Formal definition
•
4.3 Example 1
4.3 Example 1
4.3 Example 1
4.3 Example 1
• We write "a,b -> c" to signify that when the machine is
reading an a from the input it may replace the symbol b
on the top of the stack with a c.
• Any of a, b, and c may be ε. If a is ε, the machine may
make this transition without reading any symbol from
the input.
• If b is ε, the machine may make this transition without
reading and popping any symbol from the stack.
• If c is ε, the machine does not write any symbol on the
stack when going along this transition.
4.3 Example 2
• Describe PDAs for the following languages:
•
•
•
•
L = {w#wR: w *}, S = {0, 1, #}
L = {wwR: w S*}, S = {0, 1}
L = {w: w has same number of 0s and 1s}, S = {0, 1}
L = {0i1j: i ≤ j ≤ 2i}, S = {0, 1}
4.4 Main theorem
A language L is context-free if and only if it is
accepted by some pushdown automaton.
context-free grammar
pushdown automaton
4.4.1 From CFGs to PDAs
• Step 1: e, e->$ : push the stack symbol at the start state
qstart .
• Step 2; push the stating variable at the top of the stack.
• Step 3: at qloop push the variable and terminals at the
right hand side of all production starting from the
rightmost symbols. First replace the start symbol with
the rightmost symbol and keep pusing the rset of the
symbols from right to left.
• Step 4: keep poping terminals from the productions.
• Step 5: at qaccept pub the stack symbol e ,$->e.
4.4.1 From CFGs to
PDAs/Example
• construct a PDA PI from the following CFG G.
S - >aTb | b
T – >Ta | E
4.4.1 From CFGs to
PDAs/Example
• construct a PDA PI from the following CFG G.
S - >aTb | b
T – >Ta | e
4.4.1 From CFGs to PDAs
• Idea: Use PDA to simulate (rightmost) derivations
CFG:
A → 0A1
A→B
B→#
A 0A1 00A11 00B11 00#11
PDA control:
stack:
input:
write start variable
e, e / A
Z0A
00#11
replace production in reverse
e, A / 1A0
Z01A0
00#11
pop terminals and match
0, 0 / e
Z01A
0#11
replace production in reverse
e, A / 1A0
Z011A0
0#11
pop terminals and match
0, 0 / e
Z011A
#11
replace production in reverse
e, A / B
Z011B
#11
4.4.1 From CFGs to PDAs
• If, after reading whole input, PDA ends up with an
empty stack, derivation must be valid
• Conversely, if there is no valid derivation, PDA will
get stuck somewhere
• Either unable to match next input symbol,
• Or match whole input but stack non empty
4.4.1 From CFGs to PDAs
• Repeat the following steps:
• If the top of the stack is a variable A:
Choose a rule A → a and substitute A with a
• If the top of the stack is a terminal a:
Read next input symbol and compare to a
If they don’t match, reject (die)
• If top of stack is Z0, go to accept state
4.4.1 From CFGs to PDAs
a, a / e
e, A / ak...a1
for every production A → a1...ak
for every terminal a
q0
e, e / S
q1
e, Z0 / e
q2
4.4.2 From PDAs to CFGs
✓
context-free grammar
pushdown automaton
• First, we simplify the PDA:
• It has a single accept state qf
• Z0 is always popped exactly before accepting
• Each transition is either a push, or a pop, but not both
4.4.2 From PDAs to CFGs
input
state
e
0
1
e
1
0
e
1
e
0
q0 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7 qf
a
b
a
stack
0
a
a
a
a
a
c
c
c
a
a
a
a
Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0
A11 → 0A03e
0
A11
A03
e
4.4.2 From PDAs to CFGs
input
state
e
0
1
e
1
0
e
1
e
0
0
q0 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7 qf
a
b
a
stack
a
a
a
a
a
c
c
c
a
a
a
a
Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0 Z0
A10
A03
A13
A13 → A10A03
4.4.2 From PDAs to CFGs
variables: Aij
qi’
qi
start variable: A0f
qj
Aij → aAi’j’b
qj’
qi
qj
qi
qi a, Z0 /e qf
qk
Aik → AijAjk
Aii → e
A0f → A0ia
4.4.2 From PDAs to CFGs
0, e / a
1, a / e
q0
q1
#, e / e
0, e / a
e, Z0 / e
q2
q0
1, a / e
#, e / $
q3
e, $ / e
q1
e, Z0 / e
q2
start variable: A02
productions:
A00 → A00A00
A00 → A01A10
A00 → A03A30
A01 → A01A11
A01 → A02A21
...
A00 → e
A11 → e
A22 → e
A33 → e
A01 → 0A011
A01 → #A33
A02 → A01
© Copyright 2026 Paperzz