Context-free Languages

Similar documents
Automata and Computability. Solutions to Exercises

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Pushdown automata. Informatics 2A: Lecture 9. Alex Simpson. 3 October, School of Informatics University of Edinburgh als@inf.ed.ac.

Honors Class (Foundations of) Informatics. Tom Verhoeff. Department of Mathematics & Computer Science Software Engineering & Technology

The Halting Problem is Undecidable

Pushdown Automata. place the input head on the leftmost input symbol. while symbol read = b and pile contains discs advance head remove disc from pile

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 4 Nancy Lynch

C H A P T E R Regular Expressions regular expression

3515ICT Theory of Computation Turing Machines

Turing Machines: An Introduction

Increasing Interaction and Support in the Formal Languages and Automata Theory Course

Introduction to Automata Theory. Reading: Chapter 1

Automata and Formal Languages

Model 2.4 Faculty member + student

6.080/6.089 GITCS Feb 12, Lecture 3

SRM UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF SOFTWARE ENGINEERING COURSE PLAN

ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK. 15 april Master Edition

Regular Expressions and Automata using Haskell

Fundamentele Informatica II

Automata on Infinite Words and Trees

CS154. Turing Machines. Turing Machine. Turing Machines versus DFAs FINITE STATE CONTROL AI N P U T INFINITE TAPE. read write move.

Finite Automata. Reading: Chapter 2

Philadelphia University Faculty of Information Technology Department of Computer Science First Semester, 2007/2008.

(IALC, Chapters 8 and 9) Introduction to Turing s life, Turing machines, universal machines, unsolvable problems.

Regular Languages and Finite State Machines

Mathematical Induction

Reading 13 : Finite State Automata and Regular Expressions

Properties of Stabilizing Computations

6.3 Conditional Probability and Independence

Implementation of Recursively Enumerable Languages using Universal Turing Machine in JFLAP

Overview of E0222: Automata and Computability

Regular Expressions with Nested Levels of Back Referencing Form a Hierarchy

Turing Machines, Part I

Regular Languages and Finite Automata

Chapter 7 Uncomputability

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

3. Mathematical Induction

Finite Automata and Regular Languages

CS 301 Course Information

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 2

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

8 Divisibility and prime numbers

CS5236 Advanced Automata Theory

Mathematical Induction

Web Data Extraction: 1 o Semestre 2007/2008

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Finite Automata. Reading: Chapter 2

How To Understand The Theory Of Computer Science

2) Write in detail the issues in the design of code generator.

So let us begin our quest to find the holy grail of real analysis.

CS 3719 (Theory of Computation and Algorithms) Lecture 4

Introduction to Finite Automata

Genetic programming with regular expressions

MATH10040 Chapter 2: Prime and relatively prime numbers

CAs and Turing Machines. The Basis for Universal Computation

Sequential Data Structures

Increasing Interaction and Support in the Formal Languages and Automata Theory Course

Continued Fractions and the Euclidean Algorithm

INCIDENCE-BETWEENNESS GEOMETRY

ω-automata Automata that accept (or reject) words of infinite length. Languages of infinite words appear:

CSC4510 AUTOMATA 2.1 Finite Automata: Examples and D efinitions Definitions

Pushdown Automata. International PhD School in Formal Languages and Applications Rovira i Virgili University Tarragona, Spain


CMPSCI 250: Introduction to Computation. Lecture #19: Regular Expressions and Their Languages David Mix Barrington 11 April 2013

Full and Complete Binary Trees

Fast nondeterministic recognition of context-free languages using two queues

I. GROUPS: BASIC DEFINITIONS AND EXAMPLES

Push-down Automata and Context-free Grammars

Math Workshop October 2010 Fractions and Repeating Decimals

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Diagonalization. Ahto Buldas. Lecture 3 of Complexity Theory October 8, Slides based on S.Aurora, B.Barak. Complexity Theory: A Modern Approach.

Lecture 2: Universality

Finite Automata and Formal Languages

The Optimum One-Pass Strategy for Juliet

Computational Models Lecture 8, Spring 2009

Automata Theory. Şubat 2006 Tuğrul Yılmaz Ankara Üniversitesi

Basics of Counting. The product rule. Product rule example. 22C:19, Chapter 6 Hantao Zhang. Sample question. Total is 18 * 325 = 5850

The Chinese Remainder Theorem


CS103B Handout 17 Winter 2007 February 26, 2007 Languages and Regular Expressions

How To Compare A Markov Algorithm To A Turing Machine

Deterministic Finite Automata

On Winning Conditions of High Borel Complexity in Pushdown Games

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Two-dimensional Languages

Scanner. tokens scanner parser IR. source code. errors

Cartesian Products and Relations

Formal Grammars and Languages

Compiler Construction

CONTINUED FRACTIONS AND PELL S EQUATION. Contents 1. Continued Fractions 1 2. Solution to Pell s Equation 9 References 12

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Preliminary Mathematics

THE SEARCH FOR NATURAL DEFINABILITY IN THE TURING DEGREES

24 Uses of Turing Machines

Just the Factors, Ma am

Lecture 2: Regular Languages [Fa 14]

THE BANACH CONTRACTION PRINCIPLE. Contents

Lecture 1: Course overview, circuits, and formulas

Systems of Linear Equations

Transcription:

Chapter 3 Context-free Languages In the previous chapter, we discussed the class of regular languages and its corresponding finite automata. We know that many languages are regular, but some other languages are not. In this chapter, we discuss another important class of languages, the context-free languages, and the corresponding automata, the pushdown automata. Context-free languages are very important, as almost(?) all programming languages belong to this class. At first, we discuss a way to describe contextfree language, context-free grammars. 1

Grammar The following is an example of a (context-free) grammar, G 1, A A B 0A1 B Generally speaking, a grammar consists of a set of substitution rules, each of which comprises a variable on the left and a string of variables and other symbols, called terminals, on the right. Variables are often represented by capital letters, while terminals represented by lowercase letters, numbers and other symbols. One variable is designated as the start symbol. 2

From a grammar... A grammar describes a language by generating each string of that language in the following way: 1. Write down the start variable. 2. Find a variable that is written down and a rule that starts with that variable. Replace the variable with the right-hand side of that rule. 3. Repeat step 2 until no variables are left. 3

An example G 1 generates the string 000 111 in the following derivation: A 0A1 00A11 000A111 000B111 000 111. The derivation of a string can also be represented by using a parse tree. Homework: Exercises 2.2 and 2.9. 4

...to its language All strings generated via derivations constitute the language of the grammar, G, and denoted as L(G). For example, L(G 1 ) = {0 n 1 n }. Below is another context-free grammar, G 2. S NP V P NP CN CN PP V P CV CV PP PP PREP CN CN ARTICLE NOUN NOUN CV V ERB V ERB NP ART ICLE a the N OU N boy girl f lower V ERB touches likes sees P ERP with Question: What is L(G 2 )? Answer: It takes a while to find out, but... 5

...the following are strings of L(G 2 ). a boy sees the boy sees a flower a girl with a flower likes the boy Below is a derivation that derives the first string. S NP V P CN V P ARTICLE NOUN V P a NOUN V P a boy V P a boy CV a boy V ERB a boy sees 6

Formal definition Definition: A context-free grammar is a 4- tuple (V,Σ,R, S), where 1. V is a finite set called the variables, 2. Σ is a finite set, called the terminals, 3. R is a finite set of rules, each of which contains a variable and a string of variables and terminals, and 4. S is the start variable. If u, v and w are strings of variables and terminals, and A w is a rule of the grammar, we say that uav yields uwv, written as uav uwv. We also write u v if either u = v or there is a sequence u 1, u 2,..., u k, k 0 and u u 1 u 2 u k v. The language of the grammar is {w S w}. Any language that can be generated by a contextfree grammar is called a context-free language. 7

Homework assignment 1. Revisit the JFLAP simulator software, and check our its Grammar component. 2. Do Exercise 2.4(b, c, e, f) and 2.6(b) with JFLAP. 3. Use the program to check out your results. 4. When you hand in this batch of homework, you have to attach JFlap program files that can be run in the JFlap simulator. 8

Examples Let G 1.5 = ({S}, {a, b},r, S), where R is the follows: S 0S1 ɛ. Then, L(G 1.5 ) = {0 n 1 n n 0}. Let G 3 = ({S}, {a,b}, R, S), where R is the follows: S asb SS ɛ. L(G 3 ) contains strings such as abab, aaabbb, and aababb. If you interpret a as the left parenthesis and b the right one, then L(G 3 ) is the language of all strings of properly nested parentheses. Consider G 4 = ({E, T, F }, {a,+,,(,)}, R, {E}), where R contains the following rules: E E + T T T T F F F ( E ) a 9

Grammar G 4 describes a segment of a programming language concerned with arithmetic expressions. G 4 generates such strings as a + a a and (a+a) a. Below show the respective parse trees. Notice that in the tree for a + a a, the subexpression a a is grouped together as one operand, while in the tree for (a+a) a, a+a is grouped together, presumably, because of the parentheses. Thus, G 4 does capture the required precedence and its override. Homework: Exercise 2.1. 10

Ambiguity Some grammar generates a string in more than one ways. Such a string thus will have several different parse trees, which lead to different meanings. In other words, the meaning of a sentence is technically expressed through its parse tree. This phenomenon is undesirable for certain applications, particularly, in languages, either natural or artificial. If a grammar generates the same string in several different ways, we say that the string is derived ambiguously in that grammar. If a grammar generates any string ambiguously, we say the grammar itself is ambiguous. 11

Examples Assume that grammar G 5 contains the following rule: E E + E E E E ( E ) a then, it generates a + a a ambiguously. Similarly, G 2 is also an ambiguous grammar, just as any natural language usually does. For example, it generates the following string ambiguously, The girl touches the boy with the flower by generating two different parsing trees. In the first, with the flowers is interpreted as part of a noun phrase; while in the second, it is a prepositional phrase, part of a verb phrase. It thus gives it two different meanings via two sentence structures. 12

An exception Sometimes, a string can have two different derivations but the same parse tree. For example, we can apply G 4 to derive a + a a in two different ways, but both sharing the same parse tree. This is not considered as ambiguous. To concentrate on the structural issue, we define the derivation of a string in a grammar is a leftmost derivation if at every step the leftmost remaining variable is the one replaced. 13

Really ambiguous Definition: A string w is derived ambiguously in context-free grammar G if it has two different leftmost derivations. A grammar is ambiguous if it generates some string ambiguously. G 2 is such a grammar. If a language can only be generated by an ambiguous grammar, it is called inherently ambiguous. English is such a language. 14

Chomsky normal form Chomsky normal form is one of the simplest and most useful form of a context-free grammar. Definition: a context-free grammar is in Chomsky normal form if every rule is of the form: A A BC a S ɛ, where a is any terminal and A, B and C are any variables, except that neither B nor C can be the start variable. Theorem 2.6: Any context-free language is generated by a context-free grammar in Chomsky normal form. 15

Proof: The proof is done via construction, i.e., given any context-free grammar, we gradually replace all the illegal rules with those that are both legal and equivalent. At the end, we come up with a CN grammar that is equivalent to the original one. 1. Add in a new start variable S 0 and the rule S 0 S, where S is the old start symbol. Thus, S 0 won t occur on the right of any rule. 2. Remove any rule in the form of A ɛ, and then for each occurrence of A on the righthand side of a rule, we add a new rule with the occurrence removed, reflecting all the effects of A ɛ. For example, if we have a rule R uavaw, we would add in three: R uvaw, R uavw, and R uvw.(?) 16

3. We remove any unit rule A B. Then whenever a rule B u appears, we add the rule A u, unless this is a rule previously removed. Now, every rule has at least two symbols on the right. 4. For each rule A u 1 u 2 u k, k 3, we replace it with the following rules: A u 1 A 1, A 1 u 2 A 2,..., A k 2 u k 1 u k. Every rule has two symbols on its right,... Finally, for u i is a terminal symbol, we replace it with a new variable U i and add in a rule U i u i. 17

An example Let a CFG G 6 be the following grammar: S A B ASA ab B S b ɛ 1. Below is the result of applying the first step to make a new start symbol, S 0. S 0 S A B S ASA ab B S b ɛ 2.1. Removal of B ɛ rule leads to the following: S 0 S A B S ASA ab a B S ɛ b 18

2.2. Removal of A ɛ rule leads to the following: S 0 S A B S ASA ab a SA AS S B S b 3.1. Removal of unit rules S S leads to the following: S 0 S A B S ASA ab a SA AS B S b 3.2. Removal of S 0 S leads to the following: S 0 S A B ASA ab a SA AS ASA ab a SA AS B S b 19

3.3. Removing A B and A S lead to the following, respectively: S 0 S A B ASA ab a SA AS ASA ab a SA AS S b b, and, S 0 S A B ASA ab a SA AS ASA ab a SA AS b ASA ab a SA AS b 4. After introducing additional rules and variables, we eventually obtain the following: S 0 AA 1 UB a SA AS S AA 1 UB a SA AS A b AA 1 UB a SA AS A 1 SA U a B b Homework: Exercises 2.14 and 2.26. 20

Pushdown automata Now, we present the automata corresponding to the context-free languages: the pushdown automata(n)pda. NPDA s are quite similar to the NFA s, except that they also have a boundless stack as an extra memory component, which allows the automata to accept some non-regular languages. As we will see later, NPDAs are equivalent to the context-free grammars, which gives us two options for proving that a language is context free: we can either write a context-free grammar to generate such a language, or to produce a pushdown automata to accept it. 21

How does an NPDA work? While reading symbols, one at a time, from an input tape, an NPDA can also write symbols on the stack and read them back later. Writing a symbol pushes down all the other symbols currently on the stack. Reading a symbol pops it. Just like the ordinary stack, at any moment, an element can only be pushed at the top and only the top element can be popped off. As we saw earlier, DFA is equivalent to NFA. But NPDA is not equivalent to PDA. Later on, we will see, NTM is also equivalent to TM, while no body knows yet whether LBA is equivalent to NLBA, the automata that accepts the class of context sensitive languages. 22

An example Let s see how an NPDA accepts {0 n 1 n n 0}, which, as we have shown, is not regular. Given an input string consisting of 0 and 1, the NPDA pushes all the 0 s it reads into its stack, until its reads a 1, when it starts to pop off a 0 for this 1. The process continues. It accepts this input string if and only if all the input symbols are read exactly when the stack becomes empty for the first time. In other words, it will reject the input if 1) when it reads all the symbols, there are still 0 s left; or 2) there is no 0 (s) left in the stack although there are still 1 unread. 23

Formal definition The general situation in an NPDA is that, in a certain state, the PDA reads in an input symbol, observes the top of the stack, then get into a new state and possibly replace the original top element of the stack with a new one. Because we allow NPDA, there could be several legal next moves. Finally, the alphabet of the stack can be different from that of the inputs. Definition: An NPDA is defined with (Q,Σ,Γ, δ, q 0, F), where Q,Σ,Γ, and F are all finite, and 1. Q is the set of states, 2. Σ is the input alphabet, 3. Γ is the stack alphabet, 4. δ : Q Σ ɛ Γ ɛ P(Q Γ ɛ ) is the transition function, 5. q 0 is the start state, and 6. F Q is the set of accept states. 24

How does an NPDA compute? An NPDA M = (Q,Σ,Γ, δ, q 0, F) computes as follows. It accepts an input w = w 1 w 2 w n, for all i, w i Σ, if there exists a sequence (r 0, r 1,..., r n ), for all i, r i Q; and a sequence (s 0, s 1,..., s n ), for all i, s i Γ such that 1. r 0 = q 0 and s 0 = ɛ. 2. For i = 0,..., n 1, we have that (r i+1, b) δ(r i, w i+1, a), where s i = at and s i+1 = bt for some a, b Σ ɛ and t Γ. 3. r n F. In the above, all the elements of Γ, such as t, represent the contents of the stack, where s i represents a symbol in the stack, and s i t indicates that s i is above t in the stack. 25

Examples of NPDA Below shows an NPDA that accepts the language {0 n 1 n n 0}. M 1 = ({q 1, q 2, q 3, q 4 }, {0,1}, {0,$}, δ, q 1, {q 1, q 4 }), where the transition function δ is given by the following table. Note that a transition rule (q, a, b) (q, c), means that, if the PDA, in state q, reads an input a, while the top of the stack is b, then it moves to q, and replaces b with c. 26

All the cases... With (q, a, b) (q, c), one of the following can also happen: 1. If b ɛ and c ɛ, then we simply pop off b and push in c; 2. If a = ɛ, then PDA makes this move without reading in any input; 3. if b = ɛ, but c ɛ, then PDA makes this move without popping any element, but pushes in c; 4. if b ɛ but c = ɛ, then PDA pops off b, but doesn t need to push in any new symbol into the stack. 5. if a = b = c = ɛ, i.e., (q, ɛ, ɛ) (q, ɛ), it is a free ride. 27

State Diagram Similar to the FA case, it is a lot easier to use a graphical notation to represent an NPDA, mainly its transition. We again adopt the notation of the state diagram. Below is the state diagram of M 1, where, for any transition rule, (q, a,b) (q, c), we label an arrow from q to q with a, b c. 28

A little useful trick In particular, with the transition (q 1, ɛ, ɛ) (q 2,$), we move from state q 1 to q 2 by putting in $ into the stack. Notice that the formal definition of NPDA does not specify any mechanism to test if the stack is empty. We provide M 1 with an additional stack symbol, $, and put it in as an initial symbol. Thus, when this symbol is read again, M 1 knows the stack is empty. This technique is actually pretty general, and can be used in other cases as well. Notice that JFlap always implicitly puts in Z instead of $. 29

NPDA comes in... How does an NPDA accepts the following language? {a i b j c k i, j, k 0 and i = j or i = k}. Informally, the PDA begins by reading and pushing all the a s into the stack, then match them with either the b s or the c s. The question is which one? This is when NPDA comes into the scene, using the free ride rule of ɛ, ɛ ɛ. An NPDA can guess whether to match with b s or c s, as shown by the following figure. 30

Another example The following shows a NPDA that accepts all the palindromes consisting of 0 and 1, i.e., the language {ww R w {0,1} }. It begins to put the $ into the stack, then keeps reading and pushing. At each point, it guesses that the mid point of the string has been read, thus begins to pop the stack to match the rest of the input. If after all the pops, the stack is empty, it accepts the input, otherwise, rejects it. The fact is that an input is a palindrome iff there is a copy that succeeds, which is all we need. 31

Homework assignment 1. Revisit the JFLAP simulator software, and check our its Pushdown Automata component. 2. Enter the above two examples and play with them with various inputs. 3. Use the program to check out your results. 4. Send in the machines as constructed. 5. Exercises 2.4(b, e), and 2.9. 32

CFG NPDA Theorem: If a language is context-free, then some NPDA accepts it. If a language is context-free, there exists a CFG, G, that generates its language. We will construct a NPDA that accepts an input iff G generates it. Receiving an input string, the NPDA begins by writing a start variable on its stack. This NPDA will then make an educated guess as which substitution rule in G to use for the next derivation step. In general, we just replace a variable at the top of the stack with the RHS of some rule and continue... The bottom line is that if the grammar does generate such a string, the machine will accept it. 33

A trick One obvious place to store all the intermediate strings in the NPDA is its stack. However, as the PDA has to simulate further derivations, it has to look for variables, which are not always at the top of the stack. This leads to a problem. The solution is to keep on popping off the terminal symbols at the top of the stack with the corresponding symbols in the input string. If they don t match, we simply drop this branch. Thus, the top element is always a variable which can be further replaced. 34

An informal presentation 1. Place the marker symbol $ and the start variable on the stack. 2. Repeat the following steps: a. If the top of the stack is a variable symbol A, nondeterministically select one of the rules for A and substitute A with the right-hand side of the rule. b. If the top of the stack is a terminal symbol a, read the next symbol from the input and compare it with a. If they do match, repeat. Otherwise, reject this branch. If all branches are rejected, reject the input. c. If the top of the stack is $ when all the symbols are read, enter the accept state to accept this input. 35

Proof: We construct an NPDA, P = (Q,Σ,Γ, δ, q start, {q accept }) for a given CFG. Besides q start and q accept, we also need q loop, and other states needed to elaborate our simplification. We begin to define δ, by implementing step 1 using δ(q start, ɛ, ɛ) = {(q loop, S$)}, where S is the start variable in the CFG. Below are the rules for the main loop: 1. For every rule A w, add in δ(q loop, ɛ, A) = {(q loop, w)}. 2. For every terminal symbol, a, add in δ(q loop, a, a) = {(q loop, ɛ)}. 3. Finally, add in δ(q loop, ɛ,$) = {(q accept, ɛ)}. 36

Yet another trick Let q, r be states of an NPDA, let a Σ ɛ, s Γ ɛ, and let w = w 1 w l Γ, we use δ(q, a, s) = (r,w) to mean that, in state q, after reading a from the input and seeing s at the top of the stack, the machine will go to state r, after popping off s and writing w into the stack. This simplification can be elaborated by introducing new states, q 1,... q l 1 and adding the following transition rules. δ(q, a, s) = δ(q, a, s) {(q 1, w l )} δ(q 1, ɛ, ɛ) = {(q 2, w l 1 )}, δ(q 2, ɛ, ɛ) = {(q 3, w l 1 )}, δ(q l 1, ɛ, ɛ) = {(r,w 1 )} 37

The state diagram Below is the state diagram of the general NPDA we just developed. Question: Will this NPDA accept something which is NOT in the language? Answer: No, it goes to the last rule to accept a string only when the input string is empty. 38

An example Given the following CFG: S T at b b Ta ɛ. Below is the diagram of an equivalent NPDA. Now, we have to take care of the other half. 39

Theorem: If a NPDA accepts a language, it must be a CFL. Given a NPDA, P, we want to generate a CFG, G, that generates all the strings P accepts. In other words, G should generate a string iff it brings P from its start state to its accept state. For each pair of states p and q, we will have a variable A pq, which generates all the strings that bring P from p with an empty stack to q with an empty stack. The key point is that such strings also take P from p to q, without touching anything in the stack. Particularly, A qstart,q accept, the start variable, will generate all the strings that P accepts. We now have all the variables, the alphabet is the same. We have to work out the rules. 40

Let s simplify it a bit... We modify the NPDA P so that 1) it has a single accept state, 2) it empties its stack before accepting, and 3) each transition either pushes a symbol into a stack or pops one off the stack, but does not do both. 41

How to do it? 1. How to have only one accept state? Let P be a NPDA with F > 1. We convert P to another NPDA P such that L(P ) = L(P) and P has only one accept state as follows: We add in a new state q f as the only accept state of P, and, for every original accept state, q in P, we insert a transition from q to q f with a label ɛ, ɛ ɛ. 2. How to empty the stack? Let P be a NPDA with q s as its start state, and q f as its only accept state, and let $ be the special end-of-stack symbol. We add in a new start state, q S, and a new accept state, q F, and the following transition rules: a) We add in ɛ, ɛ, $ in between q S to q s ; and b) ɛ,$ ɛ in between q f to q F ; c) For every γ Γ, we add in ɛ, γ ɛ from q f back to q f. 42

3. How to let all the rules either push in a symbol, or pop off a symbol, but not both? a) Assume there are rules that do both. Let a,b c be a transition rule between p and q. We add in a new state, r, in between p and q and replace the original rule with the following two rules: a, b ɛ between p and r; and ɛ, ɛ c between r and q. b) Assume there are rules that do nothing. Let a,ɛ ɛ be a transition rule between p and q. Let γ Γ, we add in a new state, r, in between p and q, and then replace the original one with the following two rules: a, ɛ γ between p and r; and ɛ, γ ɛ between r and q. After such adjustment, P now satisfies all the desired properties. Now what? Remember what we want is to come up with these rules so that whatever accepted by the machine can be generated. 43

A key observation For any string x, P s first move on x must be a push, as there is nothing to pop at the very first moment; similarly the last move on x must be a pop, as the stack must be emptied after the last moment. Two things can happen during the computation on x : Either the symbol popped at the end is the same symbol P pushed on the stack at the very beginning, or not. If it is the former case, then the stack is empty only at the beginning and the end. Otherwise, the very first symbol must be popped off at some point and thus the stack becomes empty at this point. 44

We simulate the first case with the rule A pq aa rs b, where a is the first symbol P reads after it reaches state p, and b is the last symbol it reads before entering q; and r is the state following p and s is the state preceding q. We also simulate the second case with the rule A pq A pr A rq, where r is the state where the stack became empty for the first time. Notice that r q for this case. Otherwise, we are going back to the first case. 45

The grammar Let P = (Q,Σ,Γ, δ, q 0, {q accept }), and construct G = (Σ, V, A q0,q accept, R), where V = {A pq p, q Q, }. We continue to define R, the collection of rules for G. 1. For each p, q, r, s Q, t Γ, and a, b Σ ɛ, if (r,t) δ(p, a, ɛ) and (q, ɛ) δ(s,b, t), then add in the rule A pq aa rs b. 2. For each p, q, r Q, add the rule A pq A pr A rq. 3. Finally, for every p Q, add in A pp ɛ. 46

They are equivalent We will show that given any NPDA, P, the just constructed grammar, Γ, generates exactly the same language as P accepts. We will actually prove something a bit more general: For any two states p, q Q, A pq generates x Σ, iff x brings P from p with empty stack to q with empty stack. Once this proved, then in particular we have that A q0,q accept generates x iff x brings P from q 0 to q accept, i.e., L(P) = L(Γ). 47

Claim 1: If A pq generates x, then x can bring P from p with empty stack to q with empty stack. Proof by induction on the number of steps in the derivation of x. If it takes only one step, then the rule G uses can t have a variable on its right-hand side. The only such rule is in the form of A pp ɛ. Clearly, ɛ does take P from p to p. Assume that A pq x in k +1 steps. Then the first step will be either A pq aa rs b, or A pq A pr A rs. In the first case, consider the portion y, of x, that A rs generates. Thus, A rs y in k steps, thus, by the inductive assumption, y brings P from r with an empty stack to s with an empty stack. 48

By construction, (r, t) δ(p, a,ɛ) and (q, ɛ) δ(s,b, t). Thus, beginning at p, P can reads in a, goes to r and push t into the stack, and then the string y brings it to s, without changing t, finally, it can read b, popping off t and goes into q. Therefore, x brings P from p to q with empty stacks. In the second case, consider the portions y and z that A pr and A rq generates, respectively. Because P derive them in no more than k steps, by the inductive assumption, y brings P from p to r and z brings P from r to q, with empty stacks. Hence, x brings P from p to q, as well. This concludes the proof. 49

Claim 2: If x can bring P from p with empty stack to q with empty stack, A pq generates x. Proof by induction on the number of steps in the computation of P that goes from p to q with empty stacks on input x. If it just takes 0 steps, then P must start and end in the same state, p. Also, the only thing it can read in 0 steps is ɛ. By construction, A pp ɛ is a rule of G. Suppose P has a computation where x brings P from p to q with empty steps in k+1 steps, then either the stack is empty only at the beginning and end of this computation, or it becomes empty elsewhere, too. 50

In the first case, the symbol, t, that is pushed into the stack is the same as that is popped at the last move. Let a be the input P reads in the first move, and b be the symbol read in the last move, r be the state after the first move, and s be the state before the last move. Then (r, t) δ(p, a,ɛ) and (q, ɛ) δ(s,b, t). By construction, A pq aa rs b G. Let x = ayb. As y brings P from r to s without touching t in k 1 steps, by inductive assumption, Hence, A rs y A pq x. 51

In the second case, let r be the state where the stack became empty for the first time, then the portions of computation from p to r and r to q each contain at most k steps. Let y and s are the strings that bring P from p to r and from r to q respectively, by the inductive assumption, A pr y and Arq z. Therefore, A pq x. This completes the proof of the claim and the proof of the equivalence result. 52

Every regular language is CFL By definition, a regular language is accepted by an FA, which is a NPDA, without a stack, or its stack is never used. Moreover, we just proved that every language that is accepted by a NPDA is a context-free language. Thus, any regular language is trivially a contextfree language. For another argument from a language perspective, i.e., a closure property of CFL in terms of regular operators, check out the following homeworks, 2.16 and 2.17. Homework: Exercises 2.15, 2.16, and 2.17. 53

Non-context-free languages We present another pumping lemma which can be used to show that certain languages are not context-free. Again the idea is that if A is context free, it must satisfy certain properties. Thus, if a language fails to satisfy any of these pumping properties, it cannot be context-free. Pumping Lemma for CFL: If A is a contextfree language, then there is a number l, the critical length, where, if s is any string in A of length at least l, then s may be divided into five pieces s = uvxyz satisfying the following three properties: 1. For each j 0, uv i xy i z A, 2. vy > 0 3. vxy l. 54

The basic idea If A is context free, then there is a CFG, G, that generates it. We want to show that any sufficiently large string s in A can be pumped, either up or down, and remains in A. Let s be a very long string in A. Because s A, there is a parse tree for it, based on G, with a long path. The critical observation is that, when the path is long enough, namely, when the number of symbols along that path is strictly more than the number of variables, some variable, R, must repeat itself, according to the pigeonhole principle. We will get to the details of this critical length later on. 55

How to cut the word? When a variable R repeats itself, the following figure shows that we can replace the parse tree under the first occurrence, counted from bottom up, of R with that under the second one and still derive a string in A. This process can be repeated. We can also do the opposite, and replace the second with the first to get the bottom right derivation tree. 56

Proof of the lemma Let G be a CFG for A, and let b be the maximum number of symbols in the right-hand side of a rule such that b > 1. Thus, in any parse tree of G, no node can have more than b children. If we call the start variable is at level 0, then the length of the string at level 1 is no more than b, that at level 2 is no more than b 2,... In particular, let the height of the tree be at most h, then the length of the string generated by this parse tree is at most b h. Conversely, if the length of a string is at least b h, then the height of its derivation tree must be at least h. 57

The final kick Let V be the number of variables in G, let s l = b V +2, and let τ be the tree that generates s with the smallest number of nodes. Then, h(τ) V + 2. As τ has a height at least V + 2, some path in τ has a length at least V + 2. As the last symbol of this path must be a terminal symbol, this path contains at least V + 1 variables. By the pigeonhole principle, some variable, R, must repeat in at least two different locations of τ. We select such an R that it repeats among the first V +1 variables, counted from bottom up. 58

We now divide s into uvxyz as follows: Each occurrence of R generates a part of s. The upper occurrence of R generates vxy, which is strictly longer than x, the part generated by the lower occurrence of R. If this is not true, i.e., both v and y are empty, then we can have another tree, with the sub-tree rooted at the upper occurrence of R being replaced with that rooted at the lower occurrence of R, that still generates s, with less variables. This contradicts our assumption about τ. Thus, the second conclusion holds, i.e., vy > 0. 59

The other two pieces Now, we may replace, repeatedly, the parse tree generated by the second occurrence of R with that generated by the first one and get parse trees to generate uv i xy i z, i 1, and other way around to get a tree that generates uxz. Therefore, all the strings in the form of uv i xy i z, i 0 belong to A. Finally, as both occurrences of R are located within V + 1 variables in the path, thus, the height of the subtree rooted at the upper occurrence of R is at most V + 2, which means that the length of the string it generates, i.e., vxy is at most l(= b V +2 ). 60

Some examples Example 1: B = {a n b n c n n 0} is not a CFL. Assume that B is context free. By definition, there is a context free grammar Γ = (V, T, S, R) such that L(Γ) = B. Let l be the critical length as given in the Lemma, i.e., l = b V +2, where b is the length of the longest RHS of any rule in R. We choose s = a l b l c l. Since s B and s = 3l l, by the pumping lemma, s = uvxyz, such that 1. For each j 0, uv i xy i z B, 2. vy > 0 3. vxy l. If, e.g., v contains a,b and c, v must contain all the b s, thus v > l, contradicting the fact that vxy l. Hence both v and y contain at most two different kinds of symbols. 61

If, e.g., v contains two kinds of symbols. Without loss of generality, v contains both a and b. Then, uv 2 xy 2 z contains a mixed segment of a p b q a p b q, thus can t be in B. This contradicts the fact that uv 2 xy 2 z B. Thus, both v and y contain only one kind of symbols. However, in this latter case, uxz B, either, since vy > 0, v and x can t be both empty. Thus, in uxz, the number of the respective symbols won t match. This contracts the fact that uxz B. Therefore, by the Pumping lemma for CFL, B cannot be a context-free language. Homework: Exercise 2.13(a,b). 62

Example 2: D = {ww w {0,1} } is not a CFL. The critical point of applying pumping lemma is to find an appropriate s, which is not always easy. We might use s = 0 l 10 l 1. Clearly s satisfy the conditions, thus, s can be split into uvxyz. But, it does not lead to the needed contradiction. For example, if x happens to be 1 and v = y, then uv i xy i z D, i 0. The key observation is that any CFL has to be accepted by an NPDA, which can only use a stack. Thus, if you can find a string in the language which seems quite difficult to accept using a stack, this string might be a good candidate. For example, the string we used before s = 0 l 10 l 1 is not that difficult to be accepted with a stack, but t = 0 l 1 l 0 l 1 l is quite different. Why? 63

Let t = uvxyz, where 0 < vy vxy l. If vxy occurs in the first half of t, 4l = t < uv 2 xy 2 z = t + vy 5l. Looking right from the end of uv 2 xy 2 z, its middle position is beyond the second group of 0 s. Thus, the first symbol in the second half of uv 2 xy 2 z is a 1, while the first symbol of the first half is a 0. Similarly, it can t happen in the second half, when the end of the first half is a 0, while that of the second is a 1. Thus, the vxy part can only occur in the middle. Then, the string uxz, must be in the form of 0 l 1 i 0 j 1 l. As v and y cannot be both ɛ, i and j cannot be both l. Hence, uxz D. Since every regular language must be context free, we know immediately that D can t be regular. Notice that we have shown in Homework 1.29(b) that {www w {0,1} } is not regular, while {w w {0,1} } is. 64

Regular grammar Definition: A regular grammar is a 4-tuple (V,Σ,R, S), where 1. V is a finite set called the variables, 2. Σ is a finite set, called the terminals, 3. R is a finite set of rules, each of which contains a variable on the left and a string of a variable and terminals on the right. If a rule does contains a variable, the variable must occur as the last one. 4. S is the start variable. For any language, L, if there exists a regular grammar, Γ, L(Γ) = L, we say L is regular. Obvious, any regular language is also context free. We can show that the two definitions of a language being regular agree with each other. 65

An example The language L = {w w (abab baba) } is certainly regular, according to the old definition(?). On the other hand, we also have the following grammar Γ such that L(Γ) = L. Γ = (V,Σ, R,S), where V = {S, A,B}, Σ = {a,b}, and R contains the following rules: For example, S ba,s ab, A abas,b babs, S ɛ. S ba babas babaab babaababs babaabab. Homework: Generate babaabababab using the above grammar. 66

Context-sensitive grammar Definition: A context-sensitive grammar is a 4-tuple (V, Σ, R, S), where 1. V is a finite set called the variables, 2. Σ is a finite set, called the terminals, 3. R is a finite set of rules, each of which contains a variable and a string of variables and terminals, and For every rule in R, g h, g h. 4. S is the start variable. For any language, L, such that there exists a context-sensitive grammar, Γ, L(Γ) = L, we say L is context sensitive. Obvious, any CFL is a context sensitive. 67

An example The language {a n b n c n n 0} is context sensitive, since given the following grammar, Γ, we can show that L(Γ) = {a n b n c n n 0}. Γ contains the following variables: S, B, and C, the terminal symbols a, b and c, with S as its start variable, and the following rules: For example, S asbc, S abc, CB BC, bb bb, ab ab, bc bc, cc cc. S asbc aabcbc aabcbc aabbcc aabbcc aabbcc aabbcc. Homework: Generate aaaabbbbcccc using the above grammar. 68

What automata? We have so far seen that regular languages correspond to (D)FA; context-free languages correspond to NPDA; a natural question is then what type of automata correspond to context sensitive languages, i.e., those generated by a context sensitive grammar? A short answer is that context-sensitive languages correspond to linear bounded automaton, a special kind of Turing Machine in the sense that while its tape is initially considered infinite, only a finite contiguous portion whose length is a linear function of the length of the initial input can be accessed by the read/write head. We will start with TM first with the next chapter. 69