ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK. 17 april Classrum Edition

Similar documents
Section 1 (closed-book) Total points 30

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

Fundamentele Informatica II

Theory of Computation - Module 3

Theory of Computation

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

CS500 Homework #2 Solutions

Homework 4. Chapter 7. CS A Term 2009: Foundations of Computer Science. By Li Feng, Shweta Srivastava, and Carolina Ruiz

Theory of Computation (IV) Yijia Chen Fudan University

CS375: Logic and Theory of Computing

CISC4090: Theory of Computation

Introduction to Theory of Computing

Automata Theory CS F-08 Context-Free Grammars

CS 341 Homework 16 Languages that Are and Are Not Context-Free

Computational Models - Lecture 4

Concordia University Department of Computer Science & Software Engineering

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

CS5371 Theory of Computation. Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL, DPDA PDA)

Languages, regular languages, finite automata

CSE 105 THEORY OF COMPUTATION

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX

CSE 355 Test 2, Fall 2016

download instant at Assume that (w R ) R = w for all strings w Σ of length n or less.

CS375: Logic and Theory of Computing

Homework 5 - Solution

CS5371 Theory of Computation. Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)

Pushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Solution. S ABc Ab c Bc Ac b A ABa Ba Aa a B Bbc bc.

SFWR ENG 2FA3. Solution to the Assignment #4

1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u,

Theory of Computation (Classroom Practice Booklet Solutions)

Sheet 1-8 Dr. Mostafa Aref Format By : Mostafa Sayed

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Computational Models - Lecture 4 1

Context-Free and Noncontext-Free Languages

EXAMPLE CFG. L = {a 2n : n 1 } L = {a 2n : n 0 } S asa aa. L = {a n b : n 0 } L = {a n b : n 1 } S asb ab S 1S00 S 1S00 100

INSTITUTE OF AERONAUTICAL ENGINEERING

Pushdown Automata. Reading: Chapter 6

The Pumping Lemma for Context Free Grammars

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

2.1 Solution. E T F a. E E + T T + T F + T a + T a + F a + a

CpSc 421 Final Exam December 15, 2006

CSE 105 Homework 5 Due: Monday November 13, Instructions. should be on each page of the submission.

Automata: a short introduction

DM17. Beregnelighed. Jacob Aae Mikkelsen

Theory Bridge Exam Example Questions

Before We Start. The Pumping Lemma. Languages. Context Free Languages. Plan for today. Now our picture looks like. Any questions?

Properties of Context Free Languages

Pushdown Automata. Chapter 12

PUSHDOWN AUTOMATA (PDA)

NODIA AND COMPANY. GATE SOLVED PAPER Computer Science Engineering Theory of Computation. Copyright By NODIA & COMPANY

MA/CSSE 474 Theory of Computation

Theory of Computation Turing Machine and Pushdown Automata

TAFL 1 (ECS-403) Unit- IV. 4.1 Push Down Automata. 4.2 The Formal Definition of Pushdown Automata. EXAMPLES for PDA. 4.3 The languages of PDA

Chapter 1. Formal Definition and View. Lecture Formal Pushdown Automata on the 28th April 2009

An automaton with a finite number of states is called a Finite Automaton (FA) or Finite State Machine (FSM).

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Fundamentele Informatica 3 Antwoorden op geselecteerde opgaven uit Hoofdstuk 7 en Hoofdstuk 8

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

ECS 120: Theory of Computation UC Davis Phillip Rogaway February 16, Midterm Exam

CS481F01 Solutions 6 PDAS

5.A Examples of Context-Free Grammars 3.1 and 3.2 of Du & Ko s book(pp )

Problem 2.6(d) [4 pts] Problem 2.12 [3pts] Original CFG:

Einführung in die Computerlinguistik

CS311 Computational Structures More about PDAs & Context-Free Languages. Lecture 9. Andrew P. Black Andrew Tolmach

CPS 220 Theory of Computation Pushdown Automata (PDA)

Introduction to Formal Languages, Automata and Computability p.1/42

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

Foundations of Informatics: a Bridging Course

Automata and Computability. Solutions to Exercises

Computability and Complexity

SCHEME FOR INTERNAL ASSESSMENT TEST 3

Chapter 6. Properties of Regular Languages

5 Context-Free Languages

Computational Models - Lecture 5 1

Properties of Context-free Languages. Reading: Chapter 7

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

Solution Scoring: SD Reg exp.: a(a

3130CIT Theory of Computation

CSE 105 THEORY OF COMPUTATION

Context-free Languages and Pushdown Automata

CPS 220 Theory of Computation

CS Pushdown Automata

V Honors Theory of Computation

FABER Formal Languages, Automata. Lecture 2. Mälardalen University

Pushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata.

PS2 - Comments. University of Virginia - cs3102: Theory of Computation Spring 2010

Lecture 17: Language Recognition

Context-Free Languages (Pre Lecture)

Context-Free Grammars and Languages. Reading: Chapter 5

Automata Theory. CS F-10 Non-Context-Free Langauges Closure Properties of Context-Free Languages. David Galles

5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where:

Automata and Computability. Solutions to Exercises

Pushdown Automata (2015/11/23)

Transcription:

ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK 7 april 23 Classrum Edition

CONTEXT FREE LANGUAGES & PUSH-DOWN AUTOMATA CONTEXT-FREE GRAMMARS, CFG Problems Sudkamp Problem. (3.2.) Which language generates the grammar G given by the productions S asa aba B bb b Problem 2. (3.2.2) Find a CFG that generates the language: Problem 3. (3.2.4) Find a CFG that generates the language Problem 4. (3.2.5) Consider the grammar What language does it generate? L(G) = { a n b m c m d 2n n, m > }. L(G) = { a n b m n m 2n}. S abscb λ B bb b Problems Lewis-Papadimitriou Construct context free grammars to accept the following languages. Problem 5. Problem 6. (2.4b) {w w starts and ends with the same symbol} (2.4c) {w w is odd} Problem 7. (2.4d) {w w is odd and its middle symbol is } 2

Problem 8. {w#x w R is a substring of x, where w, x {a, b} * } Problem 9. { n n n>} U { n 2n n>} Problem. { i j 2 k i j or j k} Problem. Binary strings with twice as many s as s. Problems Linz Problem 2. (34/8c): Find a Context-Free Grammar for the following language: Problem 3. (34/8c): Ambiguity L = {a n b m c k : k = n + m } L = {a n b m c k : k n + m } (Variation on a theme). Problem 4. Explain why the grammar below is ambiguous. S A B A AA S B BB S Problem 5. Given the following ambiguous context free grammar S Ab aab A a Aa B b (a) Find the string s generated by the grammar that has two leftmost derivations. Show the derivations. (b) Show the two derivation trees for the string s. (c) (d) Find an equivalent unambiguous context-free grammar. Give the unique leftmost derivation and derivation tree for the string s generated from the unambiguous grammar above. ATT REDOVISA ORDINARIE Deadline Ö5 Problem 4 & Problem 8 ATT REDOVISA EXTRA Deadline Ö7 Problem 9 & Problem 5 3

PUSH-DOWN AUTOMATA, PDA Sudkamp Problem. CH8 8..3 Let M be the PDA that accepts even-length palindromes over {a, b}. That is, L(M) = { w w R w {a, b}*} Where non-determinism allows the machine to guess when the middle of the string has been reached. b λ/b a λ/a b B/λ a A/λ q λλ/λ q which in Linz notation looks like this: b, λ B a, λ A b, B λ a, A λ λ, λ λ q q (a, b/c stands for a, b c) a) Give the transition table of M. b) Trace all computation of the strings ab, abb, abbbb in M. c) Show that aaaa, baab L (M). d) Show that aaa, ab L (M). 4

Problem 2. CH8 3k Construct a PDA that accepts the set of palindromes over {a, b}. Problem 3. CH8 5a Let M be the PDA in example 8... the PDA that accepts the language R L = { wcw w { a, b} }. The stack is used to record the string w as it is processed. Stack symbols A and B represent input a and b respectively. b λ/b a λ/a q c λ/λ b B/λ a A/λ q A successful computation records the string w on the stack as it is processed. Once the c is encountered, the accepting state q is entered and the stack contains a string representing w R. The computation is completed by matching the remaining input with the elements of the stack. Trace the computation that accepts bbcbb. 5

NPDA EXAMPLES Sipser Construct non-deterministic pushdown automata to accept the following languages. (Note: ε has the same meaning as λ) Example. { n n n > } Example 2. { n 2n n } Example 3. { n n n>} U { n 2n n } Example 4. (+)* - {ww w in {,}*} (complement of ww) Linz Example 5. L = {a n b 2n : n } Example 6. Example 7. L = {ωσω R : ω {a,b}*, σ {a,b}} Construct the NPDA corresponding to the grammar S aabb aaa A abb a B bbb A (Problem 5 on page 94, Linz) DPDA EXAMPLES Sipser Construct deterministic pushdown automata to accept the following languages. Example 8. { n n n>} U { n 2n n>} Example 9. Binary strings that contain an equal number of s and s. 6

Example. Binary strings with twice as many s as s. Example. Binary strings that start and end with the same symbol and have the same number of s as s. Linz Example 2. Show that L = {a n b m : m n + 2} is deterministic. (Problem 2 on page 99.) Problems Linz Problem 6. (34/8c): Find a Context-Free Grammar for the following language: L = {a n b m c k : k = n + m } Problem 7. (34/8c): L = {a n b m c k : k n + m } (Variation on a theme). Ambiguity Problem 8. Explain why the grammar below is ambiguous. S A B A AA S B BB S ATT REDOVISA ORDINARIE Deadline Ö6 Example 6 & Example 8 ATT REDOVISA EXTRA Deadline Ö8 Problem 2 & Problem 7

PUMPING LEMMA: CONTEXT-FREE OR NOT? Problem. Show that the following language on Σ = {a, b, c} is not context-free. L = {a n b j c k : k = jn}. Problem 2. Show that the following language on Σ = {a, b} is not context-free: L = { ww R a w : w {a, b}*} Problem 3. (CH8 7c Sudkamp) Using the pumping lemma prove that the language below is not context-free. L i 2i = { a b a i i } Problem 4. Problem 5. (CH8 8a Sudkamp) Prove that the language i 2i j L = { a b c i, j } is context-free. (7 page 22, Linz) We want to prove that the language L = {a n b n : n and n is not a multiple of 5} is a context-free language. Problem 6. { k i i j j k i, j, k > } Problem 7. {w#x w is a substring of x, where w, x are in {,}*}. Problem 8. { i i j i i, j > } Problem 9. The complement of {( n n ) m m, n > }. Problem. 2.8 a. (Lewis-Papadimitriou): A = { n n n n n >= }. Problem. 2.8 b (Lewis-Papadimitriou): B = { n # 2n # 3n n >= }. Problem 2. 2.8 c (Lewis-Papadimitriou): C = { w#x w is a substring of x, where w, x {a, b}*}. ATT REDOVISA ORDINARIE DEADLINE Ö7 Problem 4 & Problem ATT REDOVISA EXTRA DEADLINE Ö9 Problem 2 & Problem 8

SOLUTIONS CONTEXT-FREE GRAMMARS, CFG Sudkamp examples Problem. 3.2. Which language generates the grammar G given by the productions S asa aba B bb b L(G) = { a n b m a n n >, m > }. The rule S asa recursively builds an equal number of a s on each end of the string. The recursion is terminated by the application of the rule S aba, ensuring at least one leading and one trailing a. The recursive B rule then generates any number of b s. To remove the variable B from the string and obtain a sentence of the language, the rule B b must be applied, forcing the presence of at least one b. Problem 2. 3.2.2 Find a CFG that generates the language L(G) = { a n b m c m d 2n n, m > }. The relationship between the number of leading a s and trailing d s in the language indicates that the recursive rule is needed to generate them. The same is true for b s and c s. Derivations in the grammar S asdd A A bac bc 9

generate strings in an outside-to-inside manner. The S rules produce the a s and d s while the A rules generate b s and c s. The rule A bc, whose application terminates the recursion, ensures the presence of the substring bc in every string in the language. Problem 3. 3.2.4 Find a CFG that generates the language L(G) = { a n b m n m 2n}. S asb asbb λ The first recursive rule of G generates a trailing b for every a, while the second generates two b s for each a. Thus there is at least one b for every a and at most two, as specified in the language. Problem 4. REDOVISNINGSUPPGIFT Problems Lewis-Papadimitriou Construct context free grammars to accept the following languages. Σ = {, } Problem 5. (2.4b) {w w starts and ends with the same symbol} S A A A A A λ Problem 6. (2.4c) {w w is odd} S A A S S λ

Problem 7. (2.4d) {w w is odd and its middle symbol is } S S S S S Problem 8. REDOVISNINGSUPPGIFT Problem 9. REDOVISNINGSUPPGIFT Problem. { i j 2 k i j or j k} S AC BC DE DF A A A B B B C 2 2C D D E E E2 F 2 F2 F2 Problem. Binary strings with twice as many s as s. S λ SSS SSS SSS

Problem 2. Find a Context-Free Grammar for the following language: L = {a n b m c k : k = n + m } Let G be the grammar with productions: S asc B B bbc λ Claim: L(G) = L Proof: Consider the following derivation: S * a n Sc n a n Bc n * a n b m Bc m c n a n b m (n + m) c (where the first * applies S asc n times, the second B bbc m times) Since all words in L(G) must follow this pattern in their derivations, it is clear that L(G) L Consider w L, w = a n b m c (n+m) for some n, m The derivation S * a n Sc n a n Bc n * a n b m Bc m c n a n b m (n + m) c clearly produces w for any n, m. L L(G) L = L(G) G is a CFG for L 2

Problem 3. L = {a n b m c k : k n + m } (Variation on a theme). Let G be the grammar with productions: S EcC aae AU A aa λ B bb λ C cc λ E aec F F bfc λ U auc V V bvc bb Note: L(E) = {a n b m c k : k = n + m } (from 4(a) in Linz book) Let L = {a n b m c k : k < n + m }, L2 = {a n b m c k : k > n + m } Claim: L(G) = L = L L2 Proof: Consider the leftmost productions following S EcC S EcC * a n b m c (n+m) cc * a n b m c (n+m) cc k C a n b m c (n+m) cc k = a n b m c (n+m+k+) (where the first * follows from 4(a), the second from k repetitions of { C cc }) Since (n+m) < (n+m+k+), L(EcC) L2 L 3

Consider the leftmost productions following S aae S aae * aa k AE aa k E * aa k a n b m c (n+m) c = a (n+k+) b m c (n+m) (where the first * follows from k repetitions of { A aa }, the second from 4(a)) Since (n+m+k+) > (n+m), L(aAE) L L Consider the leftmost productions following S AU S AU * a k AU a k U * a k a n Uc n a k a n Vc n * a k a n b m Vc m c n a k a n b m bbc m c n * a k a n b m bb j Bc m c n a k a n b m bb j c m c n = a (k+n) b (m+j+) c (m+n) (where the first, second, third and fourth * are k, n, m, and j repetitions of { A aa }, { U auc }, { V bvc }, and { B bb }, respectively) Since ((k+n)+(m+j+)) > (m+n), L(AU) L L L(G) = L(S) = L(EcC) L(aAE) L(AU) L L2 = L Consider w L, w = a n b m c (n+m+k) for some n, m, k, with k > Then w has the leftmost derivation: S EcC * a n b m c (n+m) cc * a n b m c (n+m) cc (k-) C a n b m c (n+m) cc k(k-) = a n b m c (n+m+k) = w L2 L(G) 4

Consider w L2, w = a (n+j) b (m+k) c (n+m) for some j, k, m, n, with j+k > If k =, w has the leftmost derivation: S aae * aa (j-) AE aa (j-) E * aa (j-) a n Ec n aa (j-) a n Fc n * aa (j- ) a n b m Fc m c n aa (j-) a n b m c m c n = a (j+n) b (+m) c (m+n) = w L (k = ) L(G) If k >, then w has the leftmost derivation: S AU * a j AU a j U * a j a n Uc n a j a n Vc n * a j a n b m Vc m c n a j a n b m bbc m c n a j a n b m bb (k-) Bc m c n a j a n b m bb (k-) c m c n = a (j+n) b (m+k) c (m+n) = w L (k > ) L(G) L L(G) L = L L2 L(G) L = L(G) Ambiguity Problem 4. Explain why the grammar below is ambiguous. S A B A AA S B BB S The grammar is ambiguous because we can find strings which have multiple derivations: S A AA S B S A AA S A Problem 5. REDOVISNINGSUPPGIFT 5

PUSH-DOWN AUTOMATA, PDA Problem. CH8 8..3 Sudkamp Let M be the PDA that accepts even-length palindromes over {a, b}. That is, L(M) = { w w R w {a, b}*} Where non-determinism allows the machine to guess when the middle of the string has been reached. b λ/b a λ/a b B/λ a A/λ q λλ/λ q which in Linz notation looks like this: b, λ B a, λ A b, B λ a, A λ λ, λ λ q q (a, b/c stands for a, b c) a) Give the transition table of M. b) Trace all computation of the strings ab, abb, abbbb in M. c) Show that aaaa, baab L (M). d) Show that aaa, ab L (M). 6

: a) Give the transition table of M. δ ( q δ ( q δ ( q, a, λ) = {[ q, b, λ) = {[ q, A]}, B]}, λ, λ) = {[ q, } δ ( q, a, A) = {[ q, } δ ( q, b, B) = {[ q, } δ a b λ q q λ /A q λ /B q λ / λ q q A/ λ q B/ λ φ Notation: The PDA consults the current state, input symbol, and the symbol on the top of the stack to determine the machine transition. The transition function δ lists all possible transitions for a given state, symbol, and stack top combination. The value of the transition function δ ( q, a, A) {[ q, B],[ q, C]} i = j k indicates that two transitions are possible when automaton is in state qi scanning an A with A on top of the stack. The transition [ q, B] δ ( q, a, A) j i [new state, new stack top] δ(current state, current input, current stack top) q i a, A B q j 7

b) Trace all computation of the strings ab, abb, abbbb in M. Computations for ab: [ q, ab, [ q, ab, [ q, ab, - [ q, b, A] - [ q, λ, BA], λ, BA] Rejected. - [ q, b, A], b, A] Rejected., ab, Rejected. Computations for abb: [ q, abb, [ q, abb, [ q, abb, [ q, abb, - [ q, bb, A] - [ q, bb, A] - [ q, bb, A], abb, - [ q, b, BA] - [ q, b, BA], bb, A] Rejected. - [ q, λ, BBA], b, BA] Rejected., λ, BBA], λ, A] Rejected. Rejected. Computations for abbb: c) Show that aaaa, baab L (M). Both aaaa, baab are accepted by this PDA, so they are in L (M). [ q, aaaa, [ q, baab, - [ q, aaa, A] - [ q, aa, AA], aa, AA], a, A], λ, Accepted. - [ q, aab, B] - [ q, ab, AB], ab, AB], b, B], λ, Accepted. 8

d) Show that aaa, ab L (M). The computations for string aaa: [ q, aaa, [ q, aaa, [ q, aaa, [ q, aaa, - [ q, aa, A] - [ q, aa, A] - [ q, aa, A], aaa, - [ q, a, AA] - [ q, a, AA], aa, A] Rejected. - [ q, λ, AAA], λ, AAA] Rejected., a, AA], λ, A] Rejected., a, Rejected. The computations for ab are shown in part b). There is none of the computations accepting the strings, so they are not in L (M). 9

Problem 2. CH8 3k Sudkamp p. 252 Construct a PDA that accepts the set of palindromes over {a, b}. : Construct the PDA: M: Q = q, q } F = { { = { a, b} Γ = { A, B} q } b λ/b a λ/a q b λ/λ a λ/λ λλ/λ b B/λ a A/λ q δ ( q δ ( q δ ( q, a, λ) = {[ q, b, λ) = {[ q, λ, λ) = {[ q, } δ ( q, a, A) = {[ q, } δ ( q, b, B) = {[ q, }, A],[ q, }, B],[ q, } 2

Problem 3. CH8 5a Let M be the PDA in example 8... the PDA that accepts the language R L = { wcw w { a, b} }. The stack is used to record the string w as it is processed. Stack symbols A and B represent input a and b respectively. b λ/b a λ/a q c λ/λ b B/λ a A/λ q A successful computation records the string w on the stack as it is processed. Once the c is encountered, the accepting state q is entered and the stack contains a string representing w R. The computation is completed by matching the remaining input with the elements of the stack. Trace the computation that accepts bbcbb. : [ q, bbcbb, - [ q, bcbb, B] - [ q, cbb, BB], bb, BB], b, B], λ, Accepted. 2

NPDA EXAMPLES (Sipser) Construct non-deterministic pushdown automata to accept the following languages. (Note: ε has the same meaning as λ) Example. { n n n>} Example 2. REDOVISNINGSUPPGIFT Example 3. { n n n>} U { n 2n n>=} 22

Example 4. (+)* - {ww w in {,}*} (complement of ww) Linz Equivalent with Sipser Example 2, in different notation. Remark: The notation a, b/c stands for a, b c Example 5. L = {a n b 2n : n } a,a/aaa b,a/λ a,$/aa b,a/λ λ,$/$ Example 6. REDOVISNINGSUPPGIFT Example 7. corresponding to the grammar (Problem 5 one page 94, Linz) S aabb aaa A abb a B bbb A 23

λ,$/s λ,s/aabb λ,s/aaa λ,a/abb λ,a/a a,a/λ λ,b/bbb b,b/λ λ,$/$ DPDA EXAMPLES (Sipser) Construct deterministic pushdown automata to accept the following languages. Example 8. REDOVISNINGSUPPGIFT Example 9. Binary strings that contain an equal number of s and s. Example. REDOVISNINGSUPPGIFT 24

Example. Binary strings that start and end with the same symbol and have the same number of s as s. Example 2. Show that L = {a n b m : m n + 2} is deterministic. (Problem 2 on page 99, Linz a,λ/a b,a/λ b,a/λ b,$/$ b,$/$ b,$/$ b,$/$ Since there is a DPDA that accepts L, L is deterministic. 25

PUMPING LEMMA: CONTEXT-FREE OR NOT? Problem. Show that the following language on Σ = {a, b, c} is not context-free. L = {a n b j c k : k = jn}. Assume that L = {a n b j c k : k = jn} is a context free language. Let m m m w = a b c, w L 2 By the Pumping Lemma w can be decomposed as w = uvxyz with vxz m and vy such that uv xy z L, i i i case If i=, aaa 4243 K aab 4243 L bc L c uvxy z uv xy z = a m vy 2 b m c m L case 2 aaa 4243 K aab {{{ KbcK uv x y z If i=, uv xy z = a m 2 v b m y c m L case 3 If i=, aaa 4243 K abb{ K bbc 2 K 3c u vxy z uv xy z = a m b m 2 vy c m L 26

case 4 If i=, aaa 4243 K abb{{{ KbbccKc uv u xy z = a v m b x m v c yz m 2 y L case 5 aaa 42 4 K ab43 K4 bcc{ Kc u vxyz If i=, uv xy z = a m b m c 2 m vy L case 6 v or y containing ab or bc If I >, uv i xy i z would be a ab ba ab b c c Or a ab bc cb c Contrary to the assumption. Language is not context free. Problem 2. REDOVISNINGSUPPGIFT Problem 3. CH8 7c Sudkamp Using the pumping lemma prove that the language below is not context-free. i 2i L = { a b a i i } Proof: k 2k k Assume that the language is context-free. By the pumping lemma, the string w = a b a, where k is the number specified by the pumping lemma, can be decomposed into substring uvwxy that satisfy the condition length (vwx) k, length (v) + length (x) >, and i i uv wx y L, ( i ). Since the length (vwx) k, the vwx can have the following possibilities: 27

Case : It contains a-s and b-s ( a i b j or b i a j, < i + j k. Since length (vwx) k, it cannot have a-s at both side of b-s,). Then uv 2 wx 2 y contains a b a b a. It will not be in L any more. Case 2: It is contained in one of a k, k b 2 or second a k. Since the repetition in uv 2 wx 2 y will increase one of the string s lengths of k k a, b 2 or second k a, the ratio of :2: will not be satisfied. Then uv 2 wx 2 y L. There is no decomposition L that satisfies the condition of pumping lemma. Therefore, L is not context free. Problem 4. REDOVISNINGSUPPGIFT 28

Problem 5. 7 page 22, Linz We want to prove that the language L = {a n b n : n and n is not a multiple of 5} is a context-free language. Consider the following two languages: L ={w : w is made from a s and b s and the length of w is a multiple of ten} L 2 = {a n b n : n } Let L denote the complement of L. We have that L = L L 2. It is easy to see that L is a regular language, since we can easily build a finite automaton with states that accepts any string in this language. Since L is regular it must be that L is regular too, since regular languages are closed under complement (Theorem 4.). The language L 2 is context-free ( the grammar is: S->aSb λ). Therefore, the language L = L L 2 is also context-free, since context-free languages are closed under regular intersection (Theorem 8.5). Problem 6. { k i i j j k i, j, k > } CF. Here is a grammar that will generate the language. S A A A B B CC C D D D λ Problem 7. {w#x w is a substring of x, where w, x are in {,}*}. 29

Not CF. Assume for the purpose of contradiction that it is. Then let the pumping length be p. Consider the string s= p p # p p, which is in the language. If we decompose s into s=uvwxy as in the statement of the pumping lemma, there are three cases to consider. If vwx is contained in the first half of the string s, then pumping up even once (that is, taking the string uv 2 wx 2 y) will give us a string where the first half is longer than then second half, which means it can't be a substring of the second half. If vwx is contained completely in the second half, then when we pump down (that is, take the string uwy). Then the second half will be shorter than the first half (since vwx >=) and so the pumped down string will not be in the language. If vwx overlaps with the symbol #, then it must be the case that the # is contained in w, or else pumping up would give too many # symbols. So either the v will be a string of 's or the x will be a string of 's or both (We need the fact that vwx <= p here). If v is a nonempty string of 's then pumping up will give the left half more 's then the right side, so the left side will not be a subset of the right side. If x is a nonempty string of zeros then pumping down will give the right side fewer zeros than the left so the left side will not be a subset of the right side. In any case we come to the conclusion that no matter how we choose the decomposition, the conditions of the pumping lemma will be violated. So the language cannot be regular. 3

Problem 8. { i i j i i, j > } Not CF. Suppose it is. Then let the pumping length be p. Consider the string s= p p p, which is in the language. If we decompose s into s=uvwxy as in the statement of the pumping lemma, then an argument like the previous one shows that if vwx is anything but the lone zero between the ones, then pumping up will give you something that is not in the language. On the other hand if vwx is the lone zero, then pumping down will give you. p p p, which is not in the language. This contradicts the pumping lemma, so therefore the language is not context free. Problem 9. The complement of {( n n ) m m, n > }. CF. Here is a grammar that generates the language, with some comments on the side to explain what each non-terminal represents. S ABA A λ AC C C; n n B B D F; m n m n D D; one or more s F F; one or more s Why this works: Since R=(**)(**)* is regular, the complement ~R includes all strings which are not even of the correct form. We can Union ** complement... nondeterministically guess which pair mismatches. Problem. REDOVISNINGSUPPGIFT 3

Problem. REDOVISNINGSUPPGIFT. Problem 2. 2.8 c. C = { w#x w is a substring of x, where w, x {a, b}*}. Not CF. Let p be the pumping length given by the pumping lemma. We show that s = a p b p #a p b p cannot be pumped. Let s = uvxyz. Neither v nor y can contain #, otherwise s` = uv xy z does not contain # and therefore s` C. If both v and y are nonempty and occur on the LHS of the #, the string s` = uv 2 xy 2 z C because it is longer on the LHS than on the RHS. Similarly, if both strings occur on the RHS, the string s` = uv xy z C because, again, the LHS is longer than the RHS. If only one of v and y is nonempty ( vy >, therefore v and y can not be empty at the same time), treat them as if they both occurred on the same side of the # as above. The only remaining case will be where both v and y are nonempty and v is on the LHS, y on the RHS. However, vxy <= p, therefore, v can only contain b s and y can only contain a s. Hence s` = uv 2 xy 2 z C because it contains more b s on the LHS than on the RHS. Since s cannot be pumped in any way without violating the pumping lemma, C is not CF. Referenser. Linz, An Introduction to Formal Languages and Automata, Jones & Bartlett 2 2. Sipser, Introduction to the Theory of Computation, PWS 997 3. Sudkamp, Languages and Machines, Addison Wesley 998 4. Lewis-Papadimitriou, Elements of the Theory of Computation, Prentice Hall 998 32