Equivalence of Pushdown Automata With Context-Free Grammar
Equivalence of Pushdown Automata With Context-Free Grammar
Equivalence of Pushdown Automata With Context-Free Grammar
Motivation
CFG and PDA are equivalent in power: a CFG generates a context-free language and a PDA recognizes a context-free language. We show here how to convert a CFG into a PDA that recognizes the language specied by the CFG and vice versa this equivalence allows a CFG to be used to specify a programming language and the equivalent PDA to be used to implement its compiler.
Application:
Theorem 2.20
A language is context-free iff some pushdown automaton recognizes it. Note: This means that:
1. if a language recognizes it
that
Lemma 2.21
If a language is context-free then some pushdown automaton recognizes it
Proof idea:
1. Let be a CFL. From the denition we know that that generates it 2. We will show how to convert if generates
has a CFG
into a PDA .
3.
What is a derivation?
Recall:
For
!
,
%$ & '
:
,
) %$ & ( & 0 1 2
A derivation of
is a sequence of substitutions
" #
&
&
Each step in the derivation yields an intermediate string of variables and terminals Hence, will determine whether some series of substitutions using rules in can lead from start variable to
&
%$
Difculties expected
How should we gure out which substitution to make? Nondeterminism allows us to guess.
At each step of the derivation one of the rules for a particular variable is selected nondeterministically.
How does
starts?
More questions
The initial string (the start variable) is on the stack. How does store the other intermediate strings? Using the stack doesnt quite work because the PDA needs to nd the variables in the intermediate string and make substitutions.
stack does not support this because only the top is accessible
Note:
Keep only part of the intermediate string on the stack starting with the rst variable in the intermediate string. Any terminal symbol appearing before the rst variable can be matched with symbols in the input. is in Figure 1
An intermediate string
Assume that
Intermediate string 01
A1A0
stack $ 0 A 1 A
Input
Control
0 1 1 0 0 1
Figure 1:
representing 01A1A0
Informal description of
Place the marker symbol $ and the start variable on the stack Repeat 1. If the top of the stack is a variable symbol , nondeterministically select a rule such that . substitute by the string
and
2. If the top of the stack is a terminal symbol, , read the next input symbol and compare it with . If they match pop the stack; if they dont match reject on this branch of nondeterminism 3. If the top of the stack is the symbol $, enter the accept state: accept state: if all text has been read accept, otherwise reject. until accept or reject
First we introduce an appropriate notation for transition function that provides a way to write on the stack in one an entire string step of the machine
this action can be simulated by introducing additional states to write the string symbol by symbol
Simulation:
Formal construction
Let , and . to
Assume that we want to go from when it reads and pops In addition, we want the string
Implementation
This construction can be implemented by introducing the new states , , and setting transition function as follows:
Setting
&
&
&
&
0 0
&
&
&
&
&
&
&
&
on the stack
Notation
means that when is in state , is the next input symbol, and is the symbol on top of the stack, reads , pop , pushes on the stack, and go to state , as seen in Figure 2
Hence:
is equivalent with
&
Graphic
Figure 2:
&
&
&
Construction of
The states of
are
The transition function is dened as follows: Initialize the stack to contain $ and , i.e.,
&
&
2. Then we handle the case where the top of the stack is a terminal, setting:
&
&
&
&
&
&
&
is in Figure 3
State diagram of
Example
We use the procedure developed during the proof of Lemma 2.21 to construct the PDA that recognizes the language generated by the CFG with the rules:
State diagram of
Lemma 2.27
If a language is recognized by a pushdown automaton then that language is a context-free language.
Proof idea
Here we have a PDA and want to construct a CFG that generates all strings recognized by .
&
&
&
&
&
to do somewhat more:
, will have a variable that For each pair of states generates all strings that can take from with an empty stack to with an empty stack.
&
. Then
from
to
Assumptions on
1. 2. has a single accept state denoted empties its stack before accepting
3. At each transition either pushes a symbol on the stack or pops of a symbol from the stack, but does not do both at the same time
Note 1
Giving features (1) and (2) to
is easy.
1. To give feature (1) to add a new state say to set of nal states, and add the new tranbsitions:
, set
the
&
&
&
&
&
where
is a new reject-state in
&
Nopte 2
To give feature (3) to
Replace each transition that simultaneously pops and pushes with a two transitions sequence that goes through a new state; In addition, replace each transition which neither pop nor push with a two transitions sequence that pushes and then pops an arbitrary stack symbol
See Figure 5
Making it uniform
Replace
Replace
by
by
Making it happens
How is working on a string while moving from with an empty stack to with an empty stack?
1.
s rst move on must be a push, because each move is either a push or a pop, and the stack is empty.
2. Similarly, last move must be a pop because stack ends up empty. 3. Two possibilities occur during s computation on :
(a) symbol popped at the end is the symbol pushed at the beginning; (b) symbol popped at the end is not the symbol pushed at the beginning
Note
In case (a), the stack is empty only at the beginning and end of s computation; In case (b) initially pushed symbol must get popped before end and thus stack becomes empty at that point.
Grammar simulation of
We simulate the case (a) of s computation where is the input by the rule symbol read at the rst move, is the symbol read at the last move, is the state following , and is the state preceding
We simulate the case (b) of s computation by the rule where is the state where the stack becomes empty
Let
. Construct where
is
constructed as follows:
1. For each
,
1
, if
(i.e.,
& & &
&
&
&
&
) and
(i.e.,
) then put
&
&
in
&
in
&
&
in
s computation corresponding to
stack hight
(0,0)
generated by
generated by
Input string
Figure 6:
s computation for
s computation corresponding to
stack hight
(0,0)
string generated by
Input string
Figure 7:
s computation for
Claim 2.30
If generates then with empty stack
brings
from
to
Induction basis
Derivation has 1 step
1. A derivation with a single step must use a rule whose no variables 2. The only rules in whose
contains
from
with
Induction step
Assume claim 2.30 true for derivations of length , , and prove it for
1. Suppose is either
with or
portion of
generated by
, i.e.,
&
4. Because
, it follows that
&
) and
&
&
&
&
it can go
6. Then, reading
it can go to
&
8. Hence,
brings
from
&
to
and ,
of
and
2. Because and are derivations containing at most steps, and respectively bring from to , and from to respectively, with empty stack
3. Hence,
can bring
from
to
Claim 2.31
If
to
by induction on the number of steps in the computation of going from to with empty stack on the input
Proof:
Induction basis
Computation has 0 steps
1. With 0 steps, computation starts and ends with the same state .
in
steps.
4. By construction,
, hence
Induction step
Assume claim 2.31 true for computations of , and show that it remains length at most , true for computations of length
from
wherein
brings
Case b:
has a computation of length wherein the stack is empty at the begin and end, and stacks may become empty also during computation
Case (a)
stack is empty only at the beginning and end.
1. The symbol that is pushed at the rst move must be the same as the symbol popped at the last move. Let it be 2. Let be the input read in the rst move, the input read at the last move, be the state after the rst move, and be the state before the last move
3. Then
and
, and
&
&
&
&
&
4. Let be the portion of without and , i.e., . Using induction we know that brings from to without touching because we can remove the rst and the last step of computation, hence .
&
5. But then
to
and from
to , both
2. Let be the input read during the computation from be the input read during the computation from to
to
and .
and
4. Because
is result that
Note
We have proved that pushdown automata recognize the class of context free languages Every regular language is recognized by a nite automaton Every nite automaton is a pushdown automaton that ignores its stack
Conclusion: