CHAPTER 2 Context-Fee Languages Contents Context-Fee Gammas definitions, examples, designing, ambiguity, Chomsky nomal fom Pushdown Automata definitions, examples, euivalence with context-fee gammas Non-Context-Fee Languages the pumping lemma fo context-fee languages Theoy of Computation, Feodo F. Dagan, Kent State Univesity 1 Pushdown Automata (PDAs) A new type of computational model. It is like a NFA but has an exta component called stack. The stack povides additional memoy beyond the finite amount available in the contol. The stack allows pushdown automata to ecognize some non-egula languages. Pushdown automata ae euivalent in powe to context-fee gammas. State contol a a b b Schematic of a finite automaton input a a b b Theoy of Computation, Feodo F. Dagan, Kent State Univesity 2 x yz State contol stack A PDA can wite symbols on stack and ead them back late Witing a symbol is pushing, emoving a symbol is popping input Schematic of a pushdown automaton Access to the stack, fo both eading and witing, may be done only at the top (last in, fist out) A stack is valuable because it can hold an unlimited amount of infomation. 1
Example Conside the language n n { 0 1 : n 0}. Finite automaton is unable to ecognize this language. A PDA is able to do this. Infomal desciption how the PDA fo this language woks. Read symbols fom the input. As each 0 is ead, push it into the stack. As soon as 1s ae seen, pop a 0 off the stack fo each 1 ead. If eading the input is finished exactly when the stack becomes empty of 0s, accept the input. If the stack becomes empty while 1s emain o if the 1s ae finished while the stack still contains 0s o if any 0s appea in the input following 1s, eject the input. Theoy of Computation, Feodo F. Dagan, Kent State Univesity 3 A pushdown automaton (PDA) is specified by a 6-tuple Q Σ ( Q, Σ, Γδ, 0, F ) Γ δ : Q Σ Γ Ρ( Q Γ ) 0 Q F Q Fomal Definition of PDAs, whee is a finite set of states, is a finite input alphabet, is a finite stack alphabet, is the tansition function, is the initial state, is the set of final states. Non-deteministic Is a collection of all subsets Σ = ΣU{ } Γ = ΓU{ } It computes as follows: it accepts input w if w can be witten as w = w,,..., 1 w2 wn, whee each w and a seuence of states,,,..., i Σ 0 1 2 n Q and stings * s0, s1, s2,..., s n Γ exist that satisfy the next thee conditions (the stings si epesent the seuence of stack contents that PDA has on the accepting banch of the computation. 1. 0 = 0, s0 = 2. ( i + 1, b) δ ( i, wi + 1, a), i = 0,..., n 1, * whee si = at, si+ 1 = bt fo some b Γ and t Γ. 3. n F Theoy of Computation, Feodo F. Dagan, Kent State Univesity 4 2
Example Conside the language n n { 0 1 : n 0}. M = ( Q, Σ, Γδ,, F) Q = {,, 3, 4} Input: 0 1 Σ = {0,1} Stack: 0 $ 0 $ 0 $ Γ = {0,$} δ : {(,0)} {(3, )} F = {, 4} 3 {(3, )} {(4, )} 4 {(,$)} b c : when the machine is eading an a fom the input it may eplace the symbol b on the top of stack with a c. Any of b, and c may be. a is, the machine may take this tansition without eading any input symbol. b is, the machine may take this tansition without eading and popping any stack symbol. c is, the machine does not wite any symbol on the stack when going along this tansition. 0, 0 1,0 $ 1,0 3 4 Theoy of Computation, Feodo F. Dagan, Kent State Univesity 5 Moe Examples Language i j k { a b c : i, j, k 0 and i = j o i = k}. a b,a c, $ 3 4 b, 5 c,a 6 7 Language { ww R : w {0,1 }*}. $ 0, 0 1,1 1, 1 0,0 3 4 Theoy of Computation, Feodo F. Dagan, Kent State Univesity 6 3
Euivalence with Context-fee Gammas Context-fee gammas and pushdown automata ae euivalent in thei desciptive powe. Both descibe the class of context-fee languages. Any context-fee gamma can be conveted into a pushdown automaton that ecognizes the same language, and vice vesa. We will pove the following esult Theoem. A language is context-fee if and only if some pushdown automaton ecognizes it. This theoem has two diections. We state each diection as a sepaate lemma. Lemma 1. If a language is context-fee, then some pushdown automaton ecognizes it. We have a context-fee gamma G descibing the context-fee language L. We show how to convet G into an euivalent PDA P. The PDA P will accept sting w iff G geneates w, i.e., if thee is a leftmost deivation fo w. Recall that a deivation is simply the seuence of substitution made as a gamma geneates a sting. S S AS A1S 011S 011AS 0110A1S 0110011 0110011 lm lm lm lm lm lm lm S AS A 0A1 A1 01 Theoy of Computation, Feodo F. Dagan, Kent State Univesity 7 How do we check that G geneates 0110011? S A AS 0A1 A1 01 0A11 S 0A111S S AS A1S 011S 011AS 0110A1S 0110011S 0110011 lm lm lm lm lm lm lm A11S 0A1 S 0A11 S 00A11S 0011S 01S 011 01AS... Pomising vaiants Idea We can use stack to stoe an intemediate sting of vaiables and teminals. It is bette to keep only pat (suffix) of the intemediate sting, the symbols stating with the fist vaiable. Any teminal symbols appeaing befoe the fist vaiable ae matched immediately with symbols in the input sting. Use non-deteminism, make copies. Theoy of Computation, Feodo F. Dagan, Kent State Univesity 8 4
Infomal desciption of PDA P 1. Place the make symbol $ and the stat symbol on the stack. 2. Repeat the following steps foeve. a) If top of stack is a vaiable symbol A, non-deteministically select one of the ules fo A and substitute A by the sting on the ight-hand side of the ule. b) If the top of stack is teminal symbol ead the next symbol fom the input and compae it to a. If they match, pop a and epeat. If they do not match, eject on this banch of the non-deteminism. c) If the top of the stack is the symbol $, ente the accept state. Doing so accepts the input if it has all been ead. 0A11 S 0A111S S AS A1S 011S 011AS 0110A1S 0110011S 0110011 lm lm lm lm lm lm lm A11S 0A1 S 0A11 S 00A11S 0011S Theoy of Computation, Feodo F. Dagan, Kent State Univesity 9 Constuction of PDA P stat S$ loop A w fo each ule A w a, a fo each teminal a Sting of teminals and vaiables accept Fo w >1 use extensions s z 1 s xyz y 2 x Theoy of Computation, Feodo F. Dagan, Kent State Univesity 10 5
S T atb b Ta Example stat $ S S b T a T T a loop accept, S b, T a, a b, b Theoy of Computation, Feodo F. Dagan, Kent State Univesity 11 Euivalence with Context-fee Gammas We ae woking on the poof of the following esult Theoem. A language is context-fee if and only if some pushdown automaton ecognizes it. We have poved Lemma 1. If a language is context-fee, then some pushdown automaton ecognizes it. We have shown how to convet a given CFG G into an euivalent PDA P. Now we will conside the othe diection Lemma 2. If a pushdown automaton ecognizes some language, then it is context-fee. We have a PDA P, and want to ceate a CFG G that geneates all stings that P accepts. That is G should geneate a sting if that sting causes the PDA to go fom its stat state to an accept state (takes P fom stat state to an accept state). Theoy of Computation, Feodo F. Dagan, Kent State Univesity 12 6
Example 4 $ 1,0 3 0, 0 1,0 sting 000111 takes P fom stat state to a finite state; sting 00011 does not. Let P be an abitay PDA. Design a Gamma Fo each pai of states p and in P the gamma will contain a vaiable This vaiable will geneate all stings that can take P fom state p with empty stack to with an empty stack Clealy, all those stings can also take P fom p to, egadless of the stack contents at p, leaving the stack at in the same condition as it was at p. A p Theoy of Computation, Feodo F. Dagan, Kent State Univesity 13 Design a Gamma (cont.) Fist we modify P slightly to give it the following thee featues. 1. It has a single accept state, accept....... 2. It empties its stack befoe accepting. 3. Each tansition eithe pushes a symbol onto stack (a push move) o pops one off the stack (a pop move), but does not do both at the same time. b c Theoy of Computation, Feodo F. Dagan, Kent State Univesity 14 b c b b 7
Design a Gamma (ideas) Fo any sting x that take P fom p to, stating and ending with an empty stack, P s fist move on x must be a push; the last move on x must be a pop. (Why?) If the symbol pushed at the beginning is the symbol popped at the end, the stack is empty only at the beginning and the end of P s computation on x. We simulate this by the ule A p aasb, whee a is the input symbol ead at the fist move, b is the symbol ead at the last move, is the state following p, and s the state peceding. t p Else, the initially pushed symbol must get popped at some point befoe the end of x, and thus the stack becomes empty at this point. We simulate this by the ule Ap Ap A, is the state when the stack becomes empty. p Theoy of Computation, Feodo F. Dagan, Kent State Univesity 15 s b,t Let P = Q, Σ, Γ, δ,,{ We constuct G as follows. The vaiables ae The stat vaiable is Rules: ( 0 accept Fomal Design A 0 accept Fo each p,,,s fom Q,, t Γ, b Σ if we have p put the ule A aa b in G. }). { : p, Q} A p t b,t p... s s Fo each p,, fom Q,, put the ule A A A in G. Fo each p fom Q,, put the ule in G. A pp p p Theoy of Computation, Feodo F. Dagan, Kent State Univesity 16 8