12.07.2015 Views

pushdown automata (pda)

pushdown automata (pda)

pushdown automata (pda)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Pushdown AutomataNotes on Automata and Theory of ComputationChia-Ping ChenDepartment of Computer Science and EngineeringNational Sun Yat-Sen UniversityKaohsiung, Taiwan ROCPushdown Automata – p. 1


IntroductionRegular languages correspond to the class of dfa’s. Isthere a class of <strong>automata</strong> for context-free languages?The class of <strong>automata</strong> for context-free languagesshould be able to, among other things, count withoutlimit (for language such as {a n b n : n ≥ 0}) and storeand match a sequence of symbols in the reverse order(for {ww R : w ∈ Σ ∗ }). This suggests using stack asstorage.The class of <strong>automata</strong> using stack as storage is calledthe <strong>pushdown</strong> <strong>automata</strong> (<strong>pda</strong>).Pushdown Automata – p. 2


Pushdown AutomataA nondeterministic <strong>pushdown</strong> automaton (n<strong>pda</strong>) isdefined by a septuple (7-tuple)M = (Q, Σ, Γ,δ,q 0 ,z,F),Q is a finite set of statesΣ is a finite set called input alphabetΓ is a finite set called stack alphabetδ : Q × (Σ ∪ {λ}) × Γ → 2 Q×Γ∗ is the transitionfunctionq 0 ∈ Q is the initial statez ∈ Γ is the stack start symbolF ⊆ Q is the set of final statesPushdown Automata – p. 3


Transition FunctionDepending on the current state, the stack symbol ontop, and optionally the input symbol, a <strong>pda</strong> makes astate transition and pushes a string of stack symbolsback to the stack.Let’s note a few things about δ,δ : Q × (Σ ∪ {λ}) × Γ → 2 Q×Γ∗ .The value is a finite subset of Q × Γ ∗ .A move is possible without using an input symbol.No move is possible with an empty stack.The reading of input never goes “backwards”.Pushdown Automata – p. 4


Examples of δSuppose δ(q 1 ,a,b) = {(q 2 ,cd), (q 3 ,λ)} for n<strong>pda</strong> M.When M is in state q 1 , the input symbol is a and the topof stack is b, one of two things can happen:M transits to state q 2 and replace b by cd (c on topof d).M transits to state q 2 and remove b.A transition function can be represented by a graph,where an edge from q i to q j labeled by a,b,y means(q j ,y) ∈ δ(q i ,a,b).Pushdown Automata – p. 5


Instantaneous DescriptionThe relevant information at any instant is the currentstate, q, the unread part of input string, w, and thecontent of stack, u. The triplet (q,w,u) is called aninstantaneous description (ID) of an n<strong>pda</strong>.A move (or step) from an ID to another is denoted bythe symbol ⊢,(q,aw,bx) ⊢ (p,w,yx) ⇔ (p,y) ∈ δ(q,a,b).A sequence of moves is denoted by ∗ ⊢, e.g.,(q 1 ,w 1 ,x 1 ) ∗ ⊢ (q 2 ,w 2 ,x 2 ).Pushdown Automata – p. 6


Language Accepted by An n<strong>pda</strong>Let M = (Q, Σ, Γ,δ,q 0 ,z,F) be an n<strong>pda</strong>. The languageaccepted by M is the set of stringsL(M) = {w ∈ Σ ∗ : (q 0 ,w,z) ∗ ⊢ (p,λ,u),p ∈ F,u ∈ Γ ∗ }L(M) is the set of input strings that can make M ina final state at the end of the input string.The final stack content is irrelevant.It turns out the set of languages acceptable by n<strong>pda</strong> isequal to the set of context-free languages.Pushdown Automata – p. 7


An ExampleConsider the language in which each string has anequal number of a’s and b’s,L = {w ∈ {a,b} ∗ : n a (w) = n b (w)}.The transition graph is drawn in Figure 7.3.M = ({q 0 ,q f }, {a,b}, {0, 1,z},δ,q 0 ,z, {q f }).Initially, the stack contains z. For an input a, either 0 ispushed or 1 is popped, depending on the top stacksymbol. Similarly, for an input b, either 1 is pushed or 0is popped. M transits to q f when it finds input emptyand the stack top is z.Pushdown Automata – p. 8


Another ExampleConsider the set L = {ww R : w ∈ {a,b} + }.The idea for an n<strong>pda</strong> to accept L is to guess the middleof the input string and matches the front substring andthe rear substring. The front substring is pushed intothe stack, and the rear substring is matched againstthe stack content (in the reverse order).Specifically, we construct an n<strong>pda</strong>M = ({q 0 ,q 1 ,q 2 }, {a,b}, {a,b,z},δ,q 0 ,z, {q 2 }),where state q 0 signals that the input position is in thefront part, and state q 1 signals that the input position isin the rear.Pushdown Automata – p. 9


n<strong>pda</strong> for cfgIf L is a context-free language and λ /∈ L, then thereexists an n<strong>pda</strong> M such thatL = L(M).Here we are given a cfg for L and want to construct ann<strong>pda</strong>. W.l.o.g., we can assume the grammar is inGreibach normal form, say G = (V,T,S,P). The idea isto make the n<strong>pda</strong> simulate the leftmost derivation of G.The terminal prefix of the sentential form matchesthe corresponding prefix of the input string.The unprocessed part of sentential form is in thestack.Pushdown Automata – p. 10


Proof L(G) ⊆ L(M)Consider partial derivationS ∗ ⇒ a 1 ...a n AA 2 ...A m ⇒ a 1 ...a n bB 1 ...B k A 2 ...A mBy construction of M, if the stack content is AA 2 ...A mafter reading a 1 ...a n then it is B 1 ...B k A 2 ...A m afterreading a 1 ...a n b.That is, the stack content matches the variable string inthe sentential form, and the input position matches theterminal prefix of the sentential form. This can beformally proved by induction on the number of steps.Thus, if S ∗ ⇒ w, then eventually M will empty the stackand end up in q f using w as input.Pushdown Automata – p. 12


cfg for n<strong>pda</strong>If L = L(M) for an n<strong>pda</strong> M, then L is context-free. Thatis, there exists a cfg G such that L(G) = L.First, we state without proof that for any n<strong>pda</strong> thereexists an equivalent one (accepting the samelanguage) with the following properties.It has a single final state q f that is entered if andonly if the stack is empty.Each move either increases or decreases the stackby a single symbol. That is,δ(q i ,a,A) contains objects like (q j ,λ) or (q j ,BC).We are given one such n<strong>pda</strong> (δ) and we want toconstruct a grammar.Pushdown Automata – p. 14


Basic IdeaHere we want the sentential form to represent stackcontent.The grammar we construct uses variables of the form(q i Aq j ). We require the following relation betweengrammar and n<strong>pda</strong>.(q i Aq j ) ∗ ⇒ v if and only if the n<strong>pda</strong> erases A from thestack while reading v and going from q i to q j .Here “erasing” means bringing up the variable under Ain stack.Pushdown Automata – p. 15


ConstructionFor δ(q i ,a,A) = (q j ,λ), we add to the production(q i Aq j ) → a.For δ(q i ,a,A) = (q j ,BC), we add to the production(q i Aq k ) → a(q j Bq l )(q l Cq k ),where q l ,q k take on all values in Q. That is, to erase Afrom stack, we first replace A by BC and subsequentlyerase B and C.The start symbol is (q 0 zq f ).Pushdown Automata – p. 16


ProofWe show that for all q i ,q j ∈ Q,u,v ∈ Σ ∗ ,A ∈ Γ,X ∈ Γ ∗ ,(q i ,uv,AX) ∗ ⊢ (q j ,v,X) ⇔ (q i Aq j ) ∗ ⇒ u,which implies (q 0 ,w,z) ∗ ⊢ (q f ,λ,λ) ⇔ (q 0 zq f ) ∗ ⇒ w.Suppose |u| = n. Then there are n − 1 increments andn decrements of stack size. For each of theseincrement/decrement we have a corresponding rule.Stringing these rules together we have (q i Aq j ) ∗ ⇒ u.Pushdown Automata – p. 17


Deterministic CaseA deterministic <strong>pushdown</strong> acceptor (d<strong>pda</strong>) never has achoice for its move. We require for d<strong>pda</strong> thatδ(q,a,b) contains at most one element.If δ(q,λ,b) is non-empty, then δ(q,c,b) is empty forall c ∈ Σ,Note that for d<strong>pda</strong> we allow λ-transition or no move,unlike the case of dfa.d<strong>pda</strong> and n<strong>pda</strong> are not equal in their descriptivepowers. There are languages that can be accepted byn<strong>pda</strong> but not by any d<strong>pda</strong>. This is also different fromthe case of dfa and nfa.Pushdown Automata – p. 18


Deterministic cfgA language L is said to be a deterministic context-freelanguage if there exists a d<strong>pda</strong> M such that L = L(M).For example, {a n b n : n ≥ 0} is a deterministic cfg.The other familiar example {ww R : w ∈ Σ ∗ } is known tobe non-deterministic.A context-free language L can be shown, indirectly, tobe non-deterministic by assuming it to be deterministicand reaching a contradiction.Pushdown Automata – p. 19


{a n b n : n ≥ 0} ∪ {a n b 2n : n ≥ 0}This language is a cfl but not dcfl.It is a cfl since L = L 1 ∪ L 2 and both L 1 = {a n b n : n ≥ 0}and L 2 = {a n b 2n : n ≥ 0} are cfls.Consider ̂L = L ∪ {a n b n c n : n ≥ 0}. It can be shown bypumping lemma for cfg that ̂L cannot be a cfl.Suppose L is dcfl. Then we can construct an n<strong>pda</strong> ̂Mfor ̂L from a d<strong>pda</strong> M for L, as depicted in Figure 7.4.That’s a contradiction.Pushdown Automata – p. 20


Grammars for Compiler DesignThe decision whether a string is in the language of ad<strong>pda</strong> can be made quickly as there is no backtrackinginvolved.In order to decide if a string is in a context-freegrammar, if there were some way to pinpoint the nextrule to use in the derivation, then the parsing processwould also be very fast.The material introduced here is important for the studyof compilers, but not directly related to later subjects inthis course.Pushdown Automata – p. 21


LL GrammarRecall that in the s-grammar, the parsing is linear-time.There is only one candidate rule to use when we lookat the first variable in sentential form and the firstsymbol in the unmatched part of input string.s-grammar is a special case of the class of LLgrammars. LL means we are scanning the symbolsfrom left-to-right and we are constructing the leftmostderivation.More generally, with a LL(k) grammar, we can identifythe correct rule given the next k symbols in the input.Apparently the parsing can be done very quickly.Pushdown Automata – p. 22


Pumping LemmaLet L be an infinite context-free language. Then thereexists some positive integer m such that any w ∈ Lwith |w| ≥ m can be decomposed asw = uvxyz,withsuch that|vxy| ≤ m, |vy| ≥ 1,uv i xy i z ∈ L for i = 0, 1, 2,... .This is known as the pumping lemma for context-freelanguages.Pushdown Automata – p. 23


ProofAssume the grammar is in the Chomsky normal form,so there is no λ-production.Consider a very big derivation tree where the depth ismore than the number of variables in V . Then at leastone of the variables, say A, is repeated in the longestpath from root.Suppose the terminal string under the last appearanceof A is x. Let the partial derivation tree under the firstappearance A be vxy, so uAz is a sentential form in thederivation of w. Then clearly uv i xy i z is in the language.Pushdown Automata – p. 24


Closure PropertiesThe set of context-free languages is closed underunion, concatenation, and star closure.The set of context-free languages is not closed underintersection and complementation.Pushdown Automata – p. 25


ProofSuppose that L 1 ,L 2 are context-free languages.U = L 1 ∪ L 2 , C = L 1 · L 2 , and S = L ∗ 1 . The variable setsare disjoint.The grammar for U is to add a new start variable S Uand a rule S U → S 1 |S 2 .The grammar for C is to add a new start variable S Cand a rule S C → S 1 S 2 .The grammar for S is to add a new start variable S Uand a rule S S → S 1 S S |λ.Counter-examples can be given to prove thenon-closedness of intersection and complementation.Pushdown Automata – p. 26


Regular IntersectionLet L 1 be context-free and L 2 be regular. Then L 1 ∩ L 2is context-free.To prove, we construct an n<strong>pda</strong> given the n<strong>pda</strong> M 1 forL 1 and dfa M 2 for L 2 , which simulates simultaneousexecution of M 1 and M 2 .Pushdown Automata – p. 27


Decidable PropertiesThere exists an algorithm for deciding whetherL(G) = ∅. Recall that an algorithm has to answercorrectly for every instance of the problem.We remove the useless symbols and rules. S isuseless iff L(G) is empty.There exists an algorithm for deciding whether L(G) isinfinite.Remove useless variables, rules, unit productions andλ-productions. L(G) is infinite iff there is a cycle in thedependency graph, where an edge from A to B meansthere is a ruleA → xBy.Pushdown Automata – p. 28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!