5 PDA Tutorial Answers
5 PDA Tutorial Answers
Irek Ulidowski
Pushdown Automata
Tutorial Answers
Question 1 Answer
Let Σ = {𝑎, 𝑏}. Construct a pushdown automaton M such that
𝑎 ,λ; 𝑎
𝜆,𝑎; 𝜆 𝜆 ,#; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3 𝑠4
𝜆 ,λ; # 𝑏,𝑎; 𝜆
𝑏,𝑎; 𝜆
Alternative answer: push every second 𝑎 onto the stack and match every 𝑏
read by popping 𝑎 from the stack: see two slides down.
Question 1: Explanation
𝐿(𝑀) = 𝑎2𝑛 𝑏 𝑛 𝑛 ∈ ℕ}.
𝑎 ,λ; 𝑎
𝜆,𝑎; 𝜆 𝜆 ,#; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3 𝑠4
𝜆 ,λ; # 𝑏,𝑎; 𝜆
𝑏,𝑎; 𝜆
• We accept 𝜆 ∈ 𝐿 𝑀 by making the state 𝑠0 is accepting.
• Since we have to exactly match one 𝑏 for every two 𝑎’s, initially the transition
from 𝑠0 to 𝑠1 pushes # on the stack, and the transition to the accepting state 𝑠4
has to pop # from the stack, to ensure the stack is empty when entering 𝑠4 .
• The loop at state 𝑠1 pushes all the read 𝑎’s on the stack, and the loop between
states 𝑠2 and 𝑠3 pops two 𝑎’s for each 𝑏 read from the input string.
Alternative answer to Question 1
Push every second 𝑎 onto the stack and match every 𝑏 read by popping 𝑎 from
the stack.
𝑏,𝑎; 𝜆
𝑎 ,λ; λ
𝑎 ,λ; 𝑎
𝑠2
Question 2:
Let Σ = {𝑎, 𝑏}. Using the answer to the previous question construct a
𝜆 ,#; 𝜆
𝑠0 𝑠1 𝑠2 𝑠4
𝜆,𝜆; 𝜆
𝜆 ,λ; # 𝜆,𝑎; 𝜆
𝑏,𝑎; 𝜆 𝑠3
𝜆,𝑎; 𝜆
𝑠5
Question 3 Answer
Let Σ = {𝑎, 𝑏}. Construct a pushdown automaton M such that
𝑎 ,λ; 𝑎 𝑏,𝑎; 𝜆
𝜆 ,#; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3
𝜆 ,λ; # 𝑎 , 𝜆; 𝜆
𝜆 ,#; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3
𝜆 ,λ; # 𝑎 , 𝜆; 𝜆
• Since we have to exactly match 𝑛 + 1 𝑎’s with 𝑛 𝑏’s, initially the transition from 𝑠0 to
𝑠1 pushes # on the stack, and the transition to the accepting state 𝑠3 has to pop #
from the stack, to ensure the stack is empty when entering 𝑠3 .
• The loop at state 𝑠1 pushes 𝑎’s on the stack, and the transition from 𝑠1 to 𝑠2 reads
one 𝑎 without modifying the stack. The loop in state 𝑠2 pops one 𝑎 for each 𝑏 read
from the input string. Since all but the last 𝑎 from the 𝑛 + 1 𝑎’s were put on the
stack, this makes sure that we have to have exactly 𝑛 𝑏’s to accept the string.
Variation on Question 3: Question 4
Let Σ = {𝑎, 𝑏}. Construct a pushdown automaton M such that
𝐿(𝑀) = 𝑎𝑛+3 𝑏 𝑛 𝑛 ∈ ℕ}
Use the solution to Question 3 and
• Add two additional states between 𝑠0 and s1 , say 𝑠4 and s5 ,
• Add transitions from s0 to 𝑠4 , from 𝑠4 to s5 , and from 𝑠5 to s1
where you push #, read the first and the second 𝑎, and where
you do not pop anything,
• Remove transition between 𝑠0 and s1 .
Question 4 Answer
Let Σ = {𝑎, 𝑏}. Construct a pushdown automaton M such that
𝑎 ,λ; 𝑎 𝑏,𝑎; 𝜆
𝜆 ,#; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3
𝑎,𝜆; 𝜆
𝜆,λ; # 𝑎,𝜆; 𝜆
𝑠4 𝑠5
𝑎, 𝜆; 𝜆
Question 5: Answer
Let Σ = {𝑎, 𝑏}. Construct a pushdown automaton M such that
𝐿 𝑀 = 𝑎𝑛 𝑏 𝑚 𝑎𝑛 𝑚, 𝑛 ∈ ℕ}.
𝑎 ,λ; 𝑎 𝑏 , λ ;λ 𝑎,𝑎; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3 𝑠4
𝜆 ,λ; # 𝑎 , 𝜆 ;𝑎 𝑎,𝑎; 𝜆 𝜆 ,#; 𝜆
𝑏 , λ ;λ
𝑠5 𝑏 , λ ;λ
Question 5: Explanation
𝑎 ,λ; 𝑎 𝑏 , λ ;λ 𝑎,𝑎; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3 𝑠4
𝜆 ,λ; # 𝑎 , 𝜆 ;𝑎 𝑎,𝑎; 𝜆 𝜆 ,#; 𝜆
𝑏 , λ ;λ
𝐿 𝑀 = 𝑎𝑛 𝑏 𝑚 𝑎𝑛 𝑚, 𝑛 ∈ ℕ}.
𝑠5 𝑏 , λ ;λ
• We accept 𝜆 ∈ 𝐿 𝑀 by making 𝑠0 accepting. If the first letter is 𝑏 we go to state 𝑠5
where we allow reading only 𝑏’s. Note that we cannot have the self-loop reading 𝑏’s at state 𝑠0
as then we could accept strings starting with 𝑏’s followed by 𝑎’s.
• Similarly, we cannot have the loop at state 𝑠2 at some other sate, as then we could mix the
𝑎’s and the 𝑏’s.
• We can only reach state 𝑠4 by reading two or more 𝑎’s , and an arbitrary number (possibly
0) of 𝑏’s in the middle. The same number of 𝑎’s that is pushed in 𝑠1 have to be popped in 𝑠3
in order to reach 𝑠4 and accept the input string.
Question 9:
Let Σ = {𝑎, 𝑏}.
𝐿 𝑀 = 𝑠 ∈ Σ ∗ 𝑛𝑢𝑚𝑏𝑒𝑟_𝑎(𝑠) = 𝑛𝑢𝑚𝑏𝑒𝑟_𝑏(𝑠)},
where
𝐿 𝑀 = 𝑠 ∈ Σ ∗ 𝑛𝑢𝑚𝑏𝑒𝑟_𝑎(𝑠) = 𝑛𝑢𝑚𝑏𝑒𝑟_𝑏(𝑠)},
Γ = {#, 𝑐} 𝜆 ,λ; # 𝑏 , 𝑐 ;λ
𝑎 , 𝜆 ;𝑐
𝑠 𝑠0 𝑠+
𝜆 , # ;# 𝑎 , 𝜆 ;𝑐
𝜆 , # ;#
𝑏 , λ ;c
𝑎,𝑐; λ 𝑠− 𝑏 , λ ;c
Question 9: Explanation
𝜆, λ ; # 𝑏, 𝑐 ; λ
𝑎, 𝜆 ; 𝑐
𝑠 𝑠0 𝑠+
𝜆 , # ;# 𝑎, 𝜆 ; 𝑐
𝜆, # ; #
𝑏, λ ; c
𝑎, 𝑐 ; λ 𝑠− 𝑏, λ ; c
𝜆 , # ;# 𝑎 , 𝜆 ;𝑐
𝜆 , # ;#
𝑏 , λ ;c
𝑎,𝑐; λ 𝑠− 𝑏 , λ ;c
• In state 𝑠+ we push a 𝑐 for every 𝑎 we read (+1) and pop a 𝑐 for every
𝑏 we read (-1). When 0 is reached (pop #), we go back to state 𝑠0 .
• In state 𝑠− we push a 𝑐 for every 𝑏 we read (-1) and pop 𝑐 c for every 𝑎
we read (+1). When 0 is reached (pop #), we go back to state 𝑠0 .
Question 9: Alternative solution
Γ = {#, 𝑎, 𝑏}
𝜆 ,λ; # 𝜆 , # ;𝜆
𝑠0 𝑠1 𝑠2
𝑎 ,λ; 𝑎
𝑎,𝑏; λ
𝑏 ,λ; 𝑏
𝑏,𝑎; λ
Blue transitions correspond to those in 𝑠+ , green to those in 𝑠− .
Consider 𝑎𝑏𝑏𝑎. After reading and pushing 𝑎, we read 𝑏 and must pop 𝑎. If
we push 𝑏 instead (as above), we shall fail. Why?
Question 10 of Chapter 5
Let Σ = { , }, namely open and close brackets.
( , λ ;(
),(; λ
Recall Type 2 question from Lecture Slides on PDAs. If you replace 𝑎 by “(“
and 𝑏 by “)”, then this question and Type 2 question are quite similar.
𝑎 ,λ; 𝑎 𝑎,𝑎; 𝜆
𝑏, λ ; 𝑏 𝑏,𝑏; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3
𝜆, 𝜆; # 𝜆 ,λ; 𝜆 𝜆 ,#; 𝜆
𝑎 ,λ; 𝜆
𝑏 ,λ; 𝜆
Answer: 𝑎 ,λ; 𝑎 𝑎,𝑎; 𝜆
𝑏, λ ; 𝑏 𝑏,𝑏; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3
𝜆, 𝜆; # 𝜆 ,λ; 𝜆 𝜆 ,#; 𝜆
𝑎 ,λ; 𝜆
𝑏 ,λ; 𝜆
The language accepted by this PDA is
𝑤 ∈ 𝑎, 𝑏 ∗ 𝑤 = 𝑤 𝑅 },
where for a string 𝑤 we define 𝑤 𝑅 to be the string obtained by
reversing 𝑤:
𝜆𝑅 = 𝜆, and (𝑎1 … 𝑎𝑛 )𝑅 = 𝑎𝑛 … 𝑎1
A string 𝑤 such that 𝒘 = 𝒘𝑹 is called a palindrome. Some
palindromes in English: level, bob, racecar, radar, kayak, redder, or
22022022.
Explanation
𝑎 ,λ; 𝑎 𝑎,𝑎; 𝜆
𝑏, λ ; 𝑏 𝑏,𝑏; 𝜆
𝑠0 𝑠1 𝑠2 𝑠3
𝜆, 𝜆; # 𝜆 ,λ; 𝜆 𝜆 ,#; 𝜆
𝑎 ,λ; 𝜆
𝑏 ,λ; 𝜆
• In state 𝑠1 we push each of the letters that we read onto the stack.
• In state 𝑠2 for each letter we read we have to pop the same letter from
the stack. This means, that in state 𝑠2 we have to read the same letters
that we have read in state 𝑠1 but in reverse order.
• The transition from state 𝑠1 to state 𝑠2 takes care of the palindromes that
have odd length, by allowing to read a letter that is not stored on the
stack. If the length is even, we use transition 𝜆 , λ ; 𝜆 .