490 likes | 518 Views
Pushdown Automata. Lecture 9 Section 2.2 Wed, Oct 4, 2006. Grammars vs. Machines. Given a regular language, we can describe it using A regular grammar, or A DFA. A context-free language can be described by a context-free grammar. Can it be described by a machine?. Machines for CFLs.
E N D
Pushdown Automata Lecture 9 Section 2.2 Wed, Oct 4, 2006
Grammars vs. Machines • Given a regular language, we can describe it using • A regular grammar, or • A DFA. • A context-free language can be described by a context-free grammar. • Can it be described by a machine?
Machines for CFLs • If a machine is to process the string anbn, then it must be able to “remember” the number of a’s. • We will use a stack to do this. • As each a is read, push it onto the stack. • When b is read, pop an a.
Machines for CFLs • Each transition will include three parts: • The symbol read. • The symbol popped. • The symbol pushed. • (Each of these could be .)
a,; a b, a; , ; Machine for {anbn | n 0} • First attempt:
Machine for {anbn | n 0} • This machine has one shortcoming. • There are several we could define acceptance of a string. • They all involve the final “state” of the machine.
Acceptance of Strings • Acceptance by final state: • Accept w if the final state is in F. • Acceptance by empty stack: • Accept w if the stack is empty. • Acceptance by final state and empty stack: • Accept w if the final state is in F and the stack is empty.
Acceptance of Strings • We will use acceptance by final state. • Thus, our machine actually accepts {anbm | n m 0}.
Testing for an Empty Stack • We need to check that the stack is empty after reading the last symbol. • To do this, we will use a bottom marker on the stack. • Let $ be the bottom marker.
Testing for an Empty Stack • At the beginning, push $ onto the stack. • At the end, pop $ off the stack. • That guarantees that the stack is empty.
a,; a b, a; , ; , ; $ , $; Machine for {anbn | n 0}
Pushdown Automata • Such a machine is called a pushdown automaton, or PDA.
Definition of a PDA • A pushdown automaton is a 6-tuple {Q, , , , q0, F}, where • Q is a finite set of states. • is a finite input alphabet. • is a finite stack alphabet. • : Q(Q) is the transition function. • q0Q is the start state. • F Q is the set of accept states.
Example • The PDA that accepts {anbn | n 0} is • Q = {q0, q1, q2, q3} • = {a, b} • = {a, $} • F = {q3}
Example and is given by • (q0, , ) = {(q1, $)} • (q1, a, ) = {(q1, a)} • (q1, , ) = {(q2, )} • (q2, b, b) = {(q2, )} • (q2, , $) = {(q3, )}
Examples • Design a PDA that accept the following language. • {w | w contains an equal number of a’s and b’s}
Examples • Let = {a, b, c, +, *, (, )}. • Design a PDA that accepts {w | w contains balanced parentheses} • Design a PDA that accepts {w | w is a valid algebraic expression}
Equivalence of PDAs and CFGs • Theorem: A language is context-free if and only if it is accepted by some PDA.
Equivalence of PDAs and CFGs • Proof (): • We are given a CFG G and we will construct a PDA M. • Let the PDA have three states: Q = {q0, q1, q2}. • q0 is the start state. • q2 is the accept state.
Equivalence of PDAs and CFGs • The transitions are • (q0, , ) = {(q1, $)} • (q1, , $) = {(q2, )} • (q1, , A) = {(q1, w)}, where A w is a rule. • (q1, a, a) = {(q1, )}, where a .
Equivalence of PDAs and CFGs • Proof (): • Given a PDA M, we must construct a grammar G that generates L(M). • Modify M so that • M has a single accept state. • M empties its stack before accepting. • Each transition either pushes one symbol or pops one symbol, but not both.
The Variables • For every pair of states p and q in M, we create a variable Apq. • The variable Apq is interpreted to mean “get from state p with an empty stack to state q with an empty stack.” • Since the stack starts and ends empty, we must push a symbol at the beginning and pop a symbol at the end.
The Grammar Rules • There are two possibilities in going from p to q. • The first symbol pushed does not match the last symbol popped. • The first symbol pushed matches the last symbol popped.
The Grammar Rules • In the first case, we write the grammar rule ApqAprArq where r is an intermediate state, reached when the stack is empty.
The Grammar Rules • In the second case, we write the grammar rule ApqaArsb where • a is the first symbol read, • b is the last symbol read, • r is the next state after p, • s is the state just before q.
The Grammar Rules • Finally, we add the rules App for every state p.
The Start Symbol • Let qstart be the start state and qaccept be the accept state. • Then the start symbol is Aqstartqaccept. • If we can eventually replace the start symbol with all terminals, then the string of terminals must be in the language.
a, ; a , ; $ , $; p q r b, a; Example • Find a grammar for the PDA
Example • The variables are App, Apq, Apr, Aqp, Aqq, Aqr, Arp, Arq, and Arr.
The Grammar Rules (Group 1) • We get the first group of rules: • AppAppApp • AppApqAqp • AppAprArp • ApqAppApq • ApqApqAqq • ApqAprArq • AprAppApr • AprApqAqr • AprAprArr
The Grammar Rules (Group 1) • AqpAqpApp • AqpAqqAqp • AqpAqrArp • AqqAqpApq • AqqAqqAqq • AqqAqrArq • AqrAqpApr • AqrAqqAqr • AqrAqrArr
The Grammar Rules (Group 1) • ArpArpApp • ArpArqAqp • ArpArrArp • ArqArpApq • ArqArqAqq • ArqArrArq • ArrArpApr • ArrArqAqr • ArrArrArr
The Grammar Rules (Group 2) • Then we get the second group of grammar rules, based on the transitions. • (p, , ) = (q, $) • (q, a, ) = (q, a) • (q, b, a) = (a, ) • (q, , $) = (r, )
The Grammar Rules (Group 2) • For every stack symbol s and for every pair of transitions, one of which pushes s and the other of which pops s, we write a grammar rule AxyaAzwb where a is the symbol read when s is pushed and b is the symbol read when s is popped.
The Grammar Rules (Group 2) • The pair (p, , ) = (q, $) (q, , $) = (r, ) gives the rule AprAqq = Aqq
The Grammar Rules (Group 2) • The pair (q, a, ) = (q, a) (q, b, a) = (q, ) gives the rule AqqaAqqb
The Grammar Rules (Group 3) • Finally, add the rules • App • Aqq • Arr
The Grammar Rules • This gives us the grammar rules • AppAppApp • ApqAppApq • ApqApqAqq • AprAppApr • AprApqAqr • AprAprArr • AqqAqqAqq
The Grammar Rules • AqrAqqAqr • AqrAqrArr • ArrArrArr • AprAqq • AqqaAqqb • App • Aqq • Arr
The Grammar • The start symbol is Apr.
Simplifying the Grammar • We may simplify these grammar rules considerably. • Eliminate any rule involving a variable that never appears on the left. • Eliminate any rule involving a variable, other than the start symbol, that never appears on the right.
Simplifying the Grammar • Eliminate any rule involving a variable that is purely recursive. • Eliminate any unnecessary unit rules. • Use common sense to eliminate any other rules that are useless.
Simplifying the Grammar • The grammar rules are reduced to • AppAppApp | • AprAppApr | AprArr | Aqq • AqqAqqAqq | aAqqb | • ArrArrArr |
Simplifying the Grammar • Then we notice that the rules • AppAppApp | • ArrArrArr | are useless since the only other choice for App and Arr is . • We should eliminate them and replace App and Arr with everywhere they occur.
Simplifying the Grammar • Now the grammar rules are reduced to • AprApr | Apr | Aqq • AqqAqqAqq | aAqqb |
Simplifying the Grammar • Now it is obvious that the rule • AprApr is pointless, so eliminate it. • We now have • AprAqq • AqqAqqAqq | aAqqb |
Simplifying the Grammar • Now it is obvious that the unit rule • AprAqq serves no purpose. • Eliminate it, making Aqq the new start symbol. • We now have • AqqAqqAqq | aAqqb |
Simplifying the Grammar • We now have • AqqAqqAqq | aAqqb | or, more simply, • SSS | aSb |
a,; a b, a; , ; , ; $ , $; p q r s Exercise • Find a context-free grammar for the language of the PDA