0% found this document useful (0 votes)
21 views62 pages

BIL308 - Biçimsel Diller Ve Otomata Teorisi Lecture 7: Pushdown Automata

dawdawda

Uploaded by

Ze Phys
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)
21 views62 pages

BIL308 - Biçimsel Diller Ve Otomata Teorisi Lecture 7: Pushdown Automata

dawdawda

Uploaded by

Ze Phys
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/ 62

BIL308 - Biçimsel Diller ve

Otomata Teorisi

Lecture 7: Pushdown Automata


Assist. Prof. Dr. Duygu SİNANÇ TERZİ

Amasya University
Faculty of Engineering
Department of Computer Engineering
In-class Test - 6

2
Answer of In-class Test - 6

3
Answer of In-class Test - 6

4
Answer of In-class Test - 6

5
Pushdown
Automata

6
Pushdown Automata
◎ Pushdown automata (PDA) is the machine format of the context-free language.
◎ It is the same as finite automata with the attachment of an auxiliary amount of
storage as stack.
◎ Already, we have discussed the limitations of finite automata. Pushdown
automata is designed to remove those limitations.
◎ Remember the example of 𝑎𝑛 𝑏𝑛 , which is not recognizable by the finite
automata due to the limitation of memory, but pushdown automata can
memorize the number of occurrences of ‘a’ and match it with the number of
occurrences of ‘b’ with the help of the stack.

7
Pushdown Automata

8
Mechanical Diagram of the PDA

9
Mechanical Diagram of the PDA
◎ Input tape: The input tape contains the input symbols. The tape is divided into a number of
squares. Each square contains a single input character. The string placed in the input tape is
traversed from left to right.
◎ Reading head: The head scans each square in the input tape and reads the input from the
tape. The head moves from left to right. The input scanned by the reading head is sent to the
finite control of the PDA.
◎ Finite control: The finite control can be considered as a control unit of a PDA. An automaton
always resides in a state.
◎ A two-way head is also added with the finite control to the stack top. Depending on the input
taken from the input tape and the input from the stack top, the finite control decides in which
state the PDA will move and which stack symbol it will push to the stack or pop from the stack
or do nothing on the stack.
◎ Stack: A stack is a temporary storage of stack symbols. Every move of the PDA indicates one of
the following to the stack: One stack symbol may be added to the stack (push), One stack
symbol may be deleted from the top of the stack (pop)
10
Instantaneous Description (ID)
◎ It describes the configuration of the PDA at a given instance.
◎ ID remembers the information of the state and the stack content at a given
instance of time.

11
Acceptance by a PDA
There are two ways to declare a string to be accepted by a PDA:

1. The string is finished (totally traversed) and the stack is empty


2. The string is finished (totally traversed) and the PDA enters into its final state

12
Acceptance by a PDA – Example1

13
Acceptance by a PDA – Example1
The remarks we can draw from the string 𝑎𝑛 𝑏𝑛 , where n ≥ 1:

○ The string consists of two alphabets ‘a’ and ‘b’.


○ All ‘a’ appear before ‘b’.
○ The number of ‘a’ is equal to the number of ‘b’.
○ There is at least one ‘a’ and one ‘b’ in the string.

14
Acceptance by a PDA – Example1
At the beginning, the PDA is in state q0 and
the stack top is 𝑧0 . In this state, it gets an
input ‘a’ from the input tape. So, a stack
symbol 𝑧1 is pushed to the stack.

From the next input ‘a’ from the input tape,


the PDA is in state 𝑞0 and the stack top is 𝑧1 ,
so another 𝑧1 is pushed into the stack.

15
Acceptance by a PDA – Example1
When the first ‘b’ occurs on the input tape, at that
time the PDA is in state 𝑞0 with the stack top 𝑧1
(Just before it, the ‘a’s are traversed.). When ‘b’ is
traversed, the stack top 𝑧1 will be popped and
the PDA will change its state to 𝑞1 . (So that there
will be no chance to push 𝑧1 in the stack if any ‘a’
occurs. The PDA is designed only for string 𝑎𝑛 𝑏𝑛 ,
but the input string may not be in the form of
𝑎𝑛 𝑏𝑛 . In that case, the string will not be accepted
by the PDA.)

When the next ‘b’ appears, the PDA is in state 𝑞1


with the stack top 𝑧1. That 𝑧1 is popped.
16
Acceptance by a PDA – Example1
If the input string is in the form of 𝑎𝑛 𝑏𝑛 , where
n≥1, then after traversing the last ‘b’ the PDA will
be in state 𝑞1 with the stack top 𝑧0 .

If we have to design the PDA accepted by the


empty stack

If we have to design the PDA accepted by the


final state

17
Acceptance by a PDA – Example1

18
Acceptance by a PDA – Example2

19
Acceptance by a PDA – Example2
The remarks we can draw from the string:

○ The string is any combination of ‘a’ and ‘b’ including null and the number
of ‘a’ is equal to the number of ‘b’.
○ In the language set, a string may start with ‘a’ or a string may start with
‘b’.
○ ‘a’ can come after ‘a’ or ‘b’ can come after ‘a’, and similarly ‘a’ can come
after ‘b’ or ‘b’ can come after ‘b’.

20
Acceptance by a PDA – Example2
If a string starts with ‘a’, then push a stack
symbol 𝑧1 in the stack.

If the string starts with ‘a’ and ‘a’ comes after ‘a’,
then the PDA is in state 𝑞0 with the stack top 𝑧1.
Another 𝑧1 will be pushed to the stack.

If the string starts with ‘a’ and ‘b’ comes after ‘a’,
then one match of ‘a’ and ‘b’ is got and the stack
top, i.e., 𝑧1 will be popped from the stack.
21
Acceptance by a PDA – Example2
If a string starts with ‘b’, then push a stack
symbol 𝑧2 in the stack.

If the string starts with ‘b’ and ‘b’ comes after


‘b’, then the PDA is in state 𝑞0 with the stack
top 𝑧2 . Another 𝑧2 will be pushed to the stack.

If the string starts with ‘b’ and ‘a’ comes after


‘b’, then one match of ‘a’ and ‘b’ is got and the
stack top, i.e., 𝑧2 will be popped from the
stack.

22
Acceptance by a PDA – Example2
If we have to design the PDA accepted by the
empty stack

If we have to design the PDA accepted by the


final state

If a null string occurs in the language set, then


it will also be accepted by the these two
functions directly.

23
Acceptance by a PDA – Example2

24
Acceptance by a PDA – Example3

25
Acceptance by a PDA – Example3
◎ W is a string which consists of any combination of ‘a’ and ‘b’ not including null.
◎ The string W may start with ‘a’ or may start with ‘b’.
◎ In the string, ‘a’ may come after ‘a’ (for example, aa) or ‘b’ may come after ‘a’
(for example, ab). Or, ‘a’ may come after ‘b’ (ba) or ‘b’ may come after ‘b’ (for
example, bb).
◎ 𝑊 𝑅 is the reverse of W. It means that in 𝑊 𝑅 if ‘a’ occurs at the ith place from
the beginning, then ‘a’ will occur at the ith place from the end in W.
◎ C is the marker which denotes the end of W and the beginning of 𝑊 𝑅 .

26
Acceptance by a PDA – Example3
The PDA is in state 𝑞0 with the stack top 𝑧0 . In
the string W, if ‘a’ is traversed as the start
symbol, a stack symbol 𝑧1 will be pushed and
the PDA will change its state to 𝑞1 (to avoid
accepting the null string).

If ‘b’ is traversed a stack symbol 𝑧2 will be


pushed, the PDA will change its state to 𝑞1 (to
avoid accepting the null string).

27
Acceptance by a PDA – Example3
If ‘a’ occurs after ‘a’, then the state is 𝑞1 and
the stack top is 𝑧1 . One 𝑧1 will be pushed to
the stack.

If ‘b’ occurs after ‘a’, then the state is 𝑞1 and


the stack top is 𝑧1. One 𝑧2 will be pushed to the
stack.

If ‘a’ occurs after ‘b’, then the state is 𝑞1 and


the stack top is 𝑧2 . One 𝑧1 will be pushed to
the stack.

If ‘b’ occurs after ‘b’, then the state is 𝑞1 and


the stack top is 𝑧2 . One 𝑧2 will be pushed to the
stack.
28
Acceptance by a PDA – Example3
At the end of W, the input head will come to the symbol C to traverse it. Before
traversing C, either ‘a’ or ‘b’ is traversed. When C is going to be traversed, at the
stack top there will be either 𝑧1 (if just before ‘a’ is traversed) or 𝑧2 (if just before ‘b’
is traversed). Upon traversing C only there will be a state change from 𝑞1 to 𝑞2 , but
no operation will be done on the stack.

29
Acceptance by a PDA – Example3
After traversing C, the reverse string of W, i.e., 𝑊 𝑅 will come. If ‘a’ comes at the ith
place from the beginning, then ‘a’ will come at the ith place from the end in W. If
for the string 𝑊 𝑅 as an input ‘a’ comes, the stack top will be 𝑧1 and the state will
be 𝑞2 . If for the string 𝑊 𝑅 as an input ‘b’ comes, the stack top will be 𝑧2 and the
state will be 𝑞2 . This stack top will be popped from the stack.

30
Acceptance by a PDA – Example3

31
Acceptance by a PDA – Example3

32
Acceptance by a PDA – Example4

33
Acceptance by a PDA – Example4
The remarks we can draw from the string:

○ The string consists of a, b, c, and d.


○ ‘b’ will follow ‘a’, ‘c’ will follow ‘b’, and ‘d’ will follow ‘c’.
○ The number of ‘a’ is equal to the number of ‘d’ and the number of ‘b’ is
equal to the number of ‘c’.
○ In all the strings of the language set, there will be at least one ‘a’, one ‘b’,
one ‘c’, and one ‘d’.

34
Acceptance by a PDA – Example4
The string starts with ‘a’, so in the input tape ‘a’ will come
first with state 𝑞0 and the stack top 𝑧0 .

From the next appearance of ‘a’ in the input tape, the stack
top will be 𝑧1 . Another 𝑧1 will be pushed to the stack.

At the time of traversal of the first ‘b’ in the input tape, the
PDA will be in state 𝑞0 with the stack top 𝑧1 . A state change
from 𝑞0 to 𝑞1 will occur and a stack symbol 𝑧2 will be
added to the stack.

From the next appearance of ‘b’ in the input tape, the state
will be 𝑞1 and the stack top will be 𝑧2 . Another 𝑧2 will be
pushed to the stack.
35
Acceptance by a PDA – Example4
At the time of traversal of the first ‘c’ in the input tape, the PDA
will be in state 𝑞1 with the stack top 𝑧2 . A state change from 𝑞1
to 𝑞2 will occur and the stack top symbol 𝑧2 will be popped
from the stack.

From the next appearance of ‘b’ in the input tape, the state will
be 𝑞2 and the stack top will be 𝑧2 . The symbol 𝑧2 will be
popped from the stack.

At the time of traversal of the first ‘d’ in the input tape, the PDA
will be in state 𝑞2 with the stack top 𝑧1 . A state change from 𝑞2
to 𝑞3 will occur and the stack top symbol 𝑧1 will be popped
from the stack.

From the next appearance of ‘d’ in the input tape, the state will
be 𝑞3 and the stack top will be 𝑧1 . The symbol 𝑧1 will be
popped from the stack. 36
Acceptance by a PDA – Example4
If we have to design the PDA accepted by the
empty stack

If we have to design the PDA accepted by the


final state

37
Acceptance by a PDA – Example4

38
DPDA and NPDA
There are two types of PDA:

1. Deterministic pushdown automata (DPDA)


2. Non-deterministic pushdown automata (NPDA)

39
Deterministic Pushdown Automata (DPDA)
◎ A PDA is said to be a DPDA if all derivations in the design give only a single
move.
◎ If a PDA being in a state with a single input and a single stack symbol gives a
single move, then the PDA is called DPDA.

40
DPDA – Example

41
Non-deterministic Pushdown Automata (NPDA)
◎ A pushdown automata is called non-deterministic if one of the derivations
generates more than one move.
◎ If a PDA being in a state with a single input and a single stack symbol gives
more than one move for any of its transitional functions, then the PDA is called
NPDA.

42
NPDA - Example

43
NPDA - Example
◎ W is a string which consists of ‘a’ and ‘b’. 𝑊 𝑅 is the reverse string of W. Let W =
abaa, then 𝑊 𝑅 will be aaba. W𝑊 𝑅 will be abaaaaba. Traversing ‘a’ in the input
tape, 𝑧1 will be pushed into the stack, and traversing ‘b’ in the input tape 𝑧2
will be pushed into the stack. When 𝑊 𝑅 will start, for the traversal of the first
symbol, the state of the PDA will be changed and the stack top will be popped.
From the next input symbols belonging to 𝑊 𝑅 , the stack top will be popped.
◎ Here, there is a problem. To the PDA, it is not assigned where W is ended and
𝑊 𝑅 is started. From the beginning state, the PDA will traverse the total string
assuming it as W.
◎ It is clear that a DPDA cannot be designed for W𝑊 𝑅 . But, this is possible for an
NPDA.

44
NPDA - Example
◎ There is a chance to be the middle of the string where two ‘a’s or two ‘b’s appear,
because the last alphabet of W is the first alphabet of 𝑊 𝑅 . Make two transitional
functions when the stack top is 𝑧1 and ‘a’ is traversed as input and the stack top is 𝑧2 and
‘b’ is traversed as input. All the other functions are the same as WC𝑊 𝑅 .

45
Construction of PDA from CFG
◎ PDA is the machine accepting a context-free language. A context-free language is
generated from context- free grammar.
◎ The following section describes the process of generating a PDA directly from the CFG.

46
Construction of PDA from CFG – Example1

47
Construction of PDA from CFG – Example1

48
Construction of PDA from CFG – Example1

49
Construction of PDA from CFG – Example1

50
Construction of PDA from CFG – Example2

51
Construction of PDA from CFG – Example2

52
Construction of PDA from CFG – Example2

53
Graphical Notation for PDA
◎ In the graphical notation of the PDA, there are states.
◎ Among them a circle with an arrow indicates a beginning state and a state with
double circle indicates a final state.
◎ The state transitions are denoted by arrows.
◎ The labels of the state transitions consists of input symbol, previous stack top
symbol and the current stack top symbol which is added after the transitions
or null symbol (if a symbol is popped).

54
Graphical Notation for PDA –Example1

55
Graphical Notation for PDA –Example1

56
Graphical Notation for PDA –Example2

57
Graphical Notation for PDA –Example2

58
Two-stack PDA
◎ Finite automata recognize regular languages such as {𝑎𝑛 , 𝑛 ≥ 0}.
◎ Adding one stack to a finite automata, it becomes PDA which can recognize
context-free language {𝑎𝑛 𝑏𝑛 , 𝑛 ≥ 0}.
◎ In the case of context-sensitive language such as {𝑎 𝑛 𝑏𝑛 𝑐 𝑛 , 𝑛 ≥ 0}. the PDA is
helpless because of only one auxiliary storage.
◎ Now the question arises—if more than one stack is added in the form of
auxiliary storage with PDA, does its power increase or not. From this question,
the concept of a two-stack PDA has come. Not only two stacks, but more than
two stacks can be added to a PDA.

59
Two-stack PDA

60
Two-stack PDA - Example

61
Two-stack PDA - Example
While scanning ‘a’, push X into stack 1. While scanning ‘b’, push ‘Y’ into stack 2. While
scanning ‘c’ with stack top X in 1 and stack top Y in 2, pop X and Y from stack 1 and 2,
respectively. The transitional functions are as follows:

62

You might also like