Professional Documents
Culture Documents
0
'
-rom S1 we take the only transition by consuming the bit 1 from the input string,
popping the 0 from the stack, and pushing nothing onto the stack$ At this point we are
in state S2 with the following on the stack:
Since we ha)e now consumed all of the input string, the 01, we cannot take the loop$
0ur only choice is to take the transition to S by consuming no input, popping the '
from the stack, and pushing nothing onto the stack$ &e are now in state S (which is a
final state since it is double circled) and the stack is empty$ (hus string 01 is accepted$
.n a similar way you can show that the string 0011 is accepted by this machine$
So is any string of the form 0
n
1
n
where n is any positi)e integer$
"ote that the string 001 is not accepted$
.f we try 001, it takes us to state S1 where we consume the two 0,s and end up with the
following stack:
%y consuming the 1, we reach state S2 with the following remaining on the stack:
%ut then there is no transition from this state that we can use$ &e ha)e no input left,
and the only transition that consumes no input needs to pop a ' from the top of stack$
1owe)er, we ha)e 0 on the top of the stack$ &e are stuck$ 0ur machine does not
accept the string 001$
.t can be shown that the language accepted by this pushdown automaton is precisely the set of
strings 2 0
n
1
n
3 n is a positi)e integer 4$
'
0
'
0
0
'
Homework problem: &hat language is accepted by the following pushdown automaton5
Theorem: (he languages accepted by the non6deterministic pushdown automata are precisely those
languages generated by conte7t6free grammars$
(hus there is a close connection between this type of grammar and this type of automaton$
"ote that there are some conte7t6free languages that are not accepted by any deterministic pushdown
automaton$
Main Application of Pushdown Automata:
&e know from the abo)e theorem that for any conte7t6free language there must be a nondeterministic
pushdown automaton that accepts e7actly that language$ 1ow do we create this pushdown automaton5
+uckily, there is a standard method to construct the desired machine$ +et,s assume that our conte7t free
grammar is for a programming language$ (hus the o)erall goal of our machine is to show that a gi)en hunk
of te7t is (or is not) a )alid program in this language$ (hen we begin with the following outline for our
pushdown automaton and add at state S2 a loop for each grammar rule$
(o be specific, for each grammar rule of the form A 7, where A is a nonterminal and 7 is any string of
terminal and8or nonterminal symbols, we add at S2 a loop with the transition #, A/ 7 and for each terminal
symbol s we add a loop with the transition s, s/ #$ "ote that it is understood that when we use a transition of
the form #, A/ 7, we push the symbols of string 7 onto the stack in backwards order$ (hat is, we take the
symbols from 7 in right to left order$
Example of the construction of a pushdown automaton to accept a context-free language:
Suppose we ha)e the following conte7t6free grammar:
9program: 9block:
9block: begin 9assignment: end
9assignment: identifier :; number
&e will assume that identifier and number are terminal symbols, items that we readily recogni<e by
some other means$ (he other terminal symbols used here are :;, begin, and end$ "ote that :; is seen
as a single =symbol=$ (he nonterminal symbols are in angle brackets$
S
0
start
0, #/ #
0, #/ #
2, #/ 2
S
1
1, 2/ #
S
2
#, #/ '
S
0
S
1
start
#, #, 9program:
S
2
#, '/ #
S
(he desired pushdown automaton that recogni<es e7actly the language generated by this grammar is
as follows:
>7ample of a program accepted by this pushdown automaton:
begin
7 :; 2!
end
&e will assume that we use a tokeni<er and that it recogni<es 7 as an identifier and 2! as a
number$ (hus we can take identifier and number as terminal symbols$
1ere is a trace showing that the abo)e 6line program is accepted by our machine$ >ach
picture shows the current state and the stack$
.n the remaining pictures of the trace, note that 7 in the input matches with identifier and 2!
matches with number$
#, #/ '
S
0
S
1
start
#, #, 9program:
S
2
#, '/ #
S
#, 9program:/ 9block:
:;, :;/ # end, end/ #
#, 9block:/ begin 9assignment: end
number, number/ #
identifier, identifier/ #
begin, begin/ #
#, 9assignment:/ identifier :; number
' S
1
'
9program:
S
2
'
9block:
S
2
'
end
S
2
9assignment:
begin
Since we ha)e reached a final state and an empty stack, the program has been accepted by our
machine as )alid$ (he parse tree could be constructed as we go through the abo)e algorithm$
(he final parse tree would look like this:
' S
2
'
end
S
2
S
2
S
2
end
9assignment: 9number:
:;
9identifier:
'
end
9number:
:;
'
end
9number:
S
2
'
end
' S
2
S
9program:
9block:
9assignment: begin end
identifier
7
:; number
2!