This action might not be possible to undo. Are you sure you want to continue?

INFO

**Introduction to Theoretical Computer Science
**

Today computers are used everywhere: banks, hospitals, schools, airline companies, gas stations, grocery stores, in our cars, in home appliances, PCs, etc., etc. Some are used to crunch numbers, some are used to process images, some are used to process other nonnumeric data and some are used to control operations of various devices. They can reason, they can prove many mathematical theorems, they can beat chess experts in their game, they can solve some very complex problems, they can understand our languages, they can answer our questions and of course they can crunch numbers much much faster than us. Let us for a moment call what computers do computation for convenience, though some of the things computers do such as controling appliances, answering our questions etc. don't fall into our traditional sense of computation. Then these computers seem to be able to compute an awfully lot of things if not everything. But are they capable of computing anything ? Are there things computers can not do ? If there are things computers can not do, what are they ? And why ? If there aren't things computers can not do, then how can we tell ? What do we exactly mean by computation ? Unfortunately there are many things computers can not do. Computers can not solve certain types of problems. For example no computer can tell in general whether or not a given computer program stops after a finite amount of time on a given input. They can not solve some other types of problems fast enough even though they can solve them in some finite amount of time. For example take the traveling salesman problem: a salesman is given a road map with distances between cities and wants to find a shortest round trip route that visits all the cities on the map exactly once. At the moment the so called traveling salesman problem requires an extremely large amount of time to solve. No one has been able to find a reasonably fast algorithm to solve it and the consensus is that it is not likely that anyone can find such an algorithm. I have just given you an example of the problems that computers could not solve. How do we know that that is the case ? Are there other problems like that ? How can we tell whther or not a given problem can be solved and solved fast enough ? The main objective of this course is to answer those questions, that is to study limitations of computers and computation. We are going to investigate limitations of computers and computations by studying the essence of compuers and computations rather than all the variations of computer and computation. This essence is a device called Turing machine. It was first conceived of by Alan Turing in early 20-th century. It is a very simple device but remarkably, every task modern computers perform can also be accomplished by Turing machines. Though it has not been proven, it is generally believed (Church's thesis) that any "computation" humans do can be done by Turing machines and that "computation" is the computation performed by Turing machines. Thus by studying Turing machines we can learn capabilities hence limitatgions of computers. Before proceeding to the study of Turing machines and their computations in this course, we study a simpler type of computing device called finite automata. Finite automata are very

WWW.REVASTUDENTS.INFO similar to Turing machines but a few restrictions are imposed on them. Consequently they are less capable than Turing machines but then their operations are simpler. So they provide a good introduction to our study of Turing machines. In addition finite automata can model a large number of systems used in practice. Thus they are a powerful tool to design and study those systems with. We call a set of strings (of symbols) a language. Finite automata process strings. More specifically they answer the question whether or not a given string belongs to a language. We say finite automata recognize languages. It turns out that solving a problem can be viewed as recognizing a language. Thus when a finite automaton is processing strings, it can actually be solving a problem. The languages that are recognized by finite automata are called regular languages. Since so many systems in practice can be described by regular languages, we are also going to study regular languages in detail as well as finite automata. We are going to learn their properties, ways to describe them and how to use them to model many of the real life systems. Turing machines also recognize languages. The languages Turing machines recognize are called Type 0 (or phrase structure) languages (regular languages are Type 3) and they are more complex than regular languages. These two type of languages belong to a hierarchy of four languages called Homsky hierarchy. The other two are context-free languages and context-sensitive languages. Those four languages are together called formal languages. After briefly studying context-free languges, which are also heavily used in practice, we go to Turing machines. There we learn how computers can be simulated by Turing machines and what it means that a Turing machine recognizes (decides) a language, which is the key to the unsolvability of problem by computers. Then with Turing machines we investigate limitations of computers and computations. In particular we are going to see a few problems that can not be solved by Turing machines hence by computers and how we can tell that they are unsolvable. Our last topic is time complexities of various problems. Among the solvable problems there are problems that can be solved within a reasonable amount of time and there are problems that are known to require a finite but very large amount of time to solve. We are going to see some of those which take a large amount of time. Unfortunately there is nothing we can do to speed them up. The time needed to solve such a problem increases at least exponentially with the size of the problem as long as we use Turing machines (hence computers). Increasing the processor speed does not help much for such problems. If the computation time is 2n where n is the size of the problem, then even if the processor speed increased 1,000 times it can handle only ten or so more larger problem sizes. For example for the traveling salesman problem if 100 cities were too many to solve fast enough, then with the increase in the processor speed of 1,000 times 110 cities would already be too many. The time complexity issues are investigated using Turing machines so that the results apply to all computers. Let us start with review of mathematics.

2

WWW.REVASTUDENTS.INFO

**Basic Mathematical Objects
**

The following are the contents of this introductory chapter.

y y y y

Logic Sets Relations Functions

Logic

Proposition and Logical Connectives "Proposition" can be defined as a declarative statement having a specific truth-value, true or false. Example: The following statements are propositions as they have precise truth values. Their truth values are false and true, respectively.

y y

2 is a odd number. 4 is a perfect square.

"Connective": Two or more propositions can be combined together to make compound propositions with the help of logical connectives. Example: Above two propositions can be used to make a compound proposition using any of the logical connectives.

y y

2 is an odd number AND 4 is a perfect square. 2 is an odd number OR 4 is a perfect square.

Their truth vales are false and true respectively. For the first compound proposition to be true both the propositions have to be true as the connective is AND and as OR is the connective for the second one if either of the propositions is true the truth value of the compound proposition is true. The following are the logical connectives used commonly: a. Conjunction The logical conjunction is understood in the same way as commonly used ôandö. The compound proposition truth-value is true iff all the constituent propositions hold true. It is represented as " ^ ". Truth table for two individual propositions p and q with conjunction is given below 3

p q is read as "if p. Conditional This is used to define as "a proposition holds true if another proposition is true" i. q T F T F pVq T T T F q T F T F p^q T F F F p -> q is also expressed in a number of different (but equivalent) ways in English. "p is sufficient for q" . Disjunction This is logical "or" read as either true value of the individual propositions. "if not q then not p" .REVASTUDENTS. "q is necessary for 4 .INFO p T T F F b.WWW. "p only if q" . Negation This is the logical "negation" and it is expressed by Truth table is given below p T F d. Truth table is given below p T T F F c.e. For example. then q" Truth table is given below p T T F F q T F T F p T F T T q p F T as p for "not p".

which is false in every case. which is true in every case.g.WWW. and if q then p".g. E. e. E. Tautology A compound proposition.: p ^ p 5 .INFO p". Biconditional A proposition (p conjunction as p q) ^ (q p) can be abbreviated using biconditional q and is read as "if p then q. f.REVASTUDENTS.: p V p g. Contradiction This is the opposite of tautology. "q is a necessity/consequence of p" and "q whenever p" are all differnt ways of saying "if p then q".

B.3} B = {3. Example: If U is the set of natural numbers and A = { 1.5} then A .5} Alternate way is to give the properties that characterize the elements of the set.4} call it Z by writing Z = {x | x N | x e 5} where N represents the set of natural numbers.4.4. Using this notation we can specify the set {0. It is denoted by A' or .2.B = {1. then the difference of A from B is the set that consists of the elements of A that are not in B.3. if A is a subset of. A is a subset of B is represented as A B.2. if every element of A is an element of B.3} and B = {3.3 } . Example: If A = {1. Union If A and B are two sets.3.INFO Sets What is a set? Set is a group of elements. Set Operations The operations that can be performed on sets are: 1.5} then A B = {1.3.2. Thus A' = { x | x U ^ x A } . Example: If A = {1. where means " is not an element of ".1.2. then the complement of A is the set consisting of all elements of the universal set that are not in A. Complement If A is a set. It is denoted by A . It is read as "the set of natural numbers that are less than or equal to 5". It is denoted by A B.5} 2. Difference If A and B are two sets. Example: A = {1. Note: If A is a subset of B and B is a subset of A then A=B. having a property that characterizes those elements. but not equal to B represented as A B. then the union of A and B is the set that contains all the elements that are in A and B including the ones in both A and B.4. Example: B = {x | x is a positive integer less than or equal to 5} Some sets can also be defined recursively. All the elements belonging to the set are explicitly given. How to specify a Set? One way is to enumerate the elements completely. then A' = { x | x U ^ x > 3 } .REVASTUDENTS. A is a subset of B.4. Set terminology Belongs To x B means that x is an element of set B. Also.2. Subset Let A and B be two sets.. Universal Set The set U of all the elements we might ever consider in the discourse is called the universal set.2.2} 6 .WWW.

Example: If A = {1.A . For A and B of the above example B .4. that Venn Diagrams must NOT be used for rigorous discussions. 3.B B . then the intersection of A and B is the set that consists of the elements in both A and B .8. 4. It is denoted by A B.8} B = {3.3.4.5 } and B = { 6.3.A = {4.WWW.8} then A B = {3.5.INFO Note that in general A . It is a very good tool to get a general idea. B= ø. because they can represent only very limited 7 ø=A ø=ø . C represent arbitrary sets and ø is the empty set and U is the Universal Set. Intersection If A and B are two sets.8}.2.2. The Commutative laws: A B=B A A B=B A The Associative laws: A (B C) = (A B) C A (B C) = (A B) C The Distributive laws: A (B C) = (A B) (A C) A (B C) = (A B) (A C) The Idempotent laws: A A=A A A=A The Absorptive laws: A (A B) = A A (A B) = A The De Morgan laws: (A B)' = A' B' (A B)' = A' B' Other laws involving Complements: ( A' )' = A A A' = ø A A' = U Other laws involving the empty set A A Other laws involving the Universal Set: A U=U A U=A Venn Diagrams A common technique in working with Set Operations is to illustrate them by drawing Venn Diagrams.5} . A where ø is the Empty set. Example: A = { 1.REVASTUDENTS. however. Note. Following is a list of some standard Set Identities A.e. B. Disjoint sets A and B are said to be disjoint if they contain no elements in common i.9 } are disjoint.

8.WWW.3.4 } can be represented as shown below using Venn Diagrams: Set A U represents the Universal set in which A is one of the Set. The idea of Venn Diagram is to draw a region representing the universe and within that to draw the regions representing the component sets we are starting with so that the resulting diagram describes their interrelationships.REVASTUDENTS. For example sets A = { 1.2. Set B The following Venn Diagram is used to illustrate A B A B The following Venn Diagram is used to illustrate A U B A B 8 .INFO situations and miss many other possibilities.4 } and B = { 6.2.

3.4. 7 } B = { 2. For example: U = { 1.6.4.2.5.4 } A B = { 1.3. To denote either of these B C.6.7.B is the yellow shaded region and B .REVASTUDENTS. 9 .A is the blue shaded region in the Venn Diagram shown below Generalized Set Operations Union.(A B) = { 5.8 } A = { 1.INFO A B is the set consisting of all the different elements in A and B. (A B)' is the yellow region in the Venn diagram given below.4.8 } A .3. For example expressions we often use A holds. intersection and Cartesian product of sets are associative.6.WWW.2.2.8 } (A B)' = U .

Inductive Clause: Ai = ( Ai) An+1 Ai and generalized Cartesian product Similarly the generalized intersection Ai can be defined. De Morgan's law on set union and intersection can also be generalized as follows: Theorem (Generalized De Morgan) = . An Basis Clause: For n = 1 .WWW. A2 .REVASTUDENTS. Based on these definitions.INFO This can be generalized for the union of any finite number of sets as A1 .... which we write as Ai This generalized union of sets can be rigorously defined as follows: Definition ( Ai) : Ai = A1. and = Proof: These can be proven by induction on n and are left as an exercise. 10 .

the object is not a member of the set. then they can be combined in certain specified ways to create other objects. These rules can also be used to test elements for the membership in the set. The inductive clause always asserts that if objects are elements of the set. The Set of Natural Numbers Basis Clause: Inductive Clause: For any element x in . A recursive definition of a set always consists of three distinct clauses: 1. x + 1 is in . The extremal clause asserts that unless an object can be shown to be a member of the set by applying the basis and inductive clauses a finite number of times. The basis for this set N is { 0 } . and x is the child of x + 1. the rules to be used to generate elements of the set from elements already known to be in the set are given. This part of the definition specifies the "seeds" of the set from which the elements of the set are generated using the methods given in the inductive clause. It essentially gives a procedure to generate the members of the set one by one starting with some subset of its elements. and for which there are no convenient or obvious predicates to specify their elements can often be defined using a recursive definition (also called inductive definition). Next. and the new object is their child . Extremal Clause: Nothing is in unless it is obtained from the Basis and Inductive Clauses.INFO Recursive Definition Subjects to be Learned y y y y y recursive/inductive definition basis clause basis inductive clause extremal clause Contents Sets which have too many elements to list them up. 11 . These rules provide a method to construct the set element by element starting with the seeds. 3. In this type of definition.REVASTUDENTS. The basis clause (or simply basis) of the definition establishes that certain objects are in the set. The set of elements specified here is called basis of the set being defined.WWW. first a collection of elements to be included initially in the set is specified. Let us call the objects used to create a new object the parents of the new object. The inductive clause (or simply induction) of the definition establishes the ways in which elements of the set can be combined to produce new elements of the set. 2. Examples of Recursive Definition of Set Example 1. The x + 1 in the Inductive Clause is the parent of x. These elements can be viewed as the seeds of the set being defined.

3! can be found as follows: Since 0 ! = 1. Then the value of the function at an element. Then see how other elements can be obtained from them. 0 + 1 (= 1) is in N. Note that if we don't have (3). Note that here Extremal Clause is not necessary. Example 2: The function f(n) = n! for natural numbers n can be defined recursively as follows: Basis Clause: f(0) = 0! = 1 Inductive Clause: For all natural number n. try simplest elements in the set such as smallest numbers (0. So there is no chance of other elements to come into the function being defined. because the set of natural numbers can be defined recursively and that has the extremal clause in it. simplest expressions. which is not what we want as the set of natural numbers.. A few examples are given below. 0 is the parent of 1. 2.5.5. Proceeding in this manner all the natural numbers are put into N. and 1 is the child of 0. Then by (2) again.INFO Following this definition. or 1). of the domain is defined using its value at the parent(s) of the element x. 12 .5. 1 is the parent of 2. since 0 is in N. the set of natural numbers N can be obtained as follows: First by (1). and generalize that generation process for the "Inductive Clause". The set of propositions (propositional forms) can also be defined recursively. can be included in N. Condition: The domain of the function you wish to define recursively must be a set defined recursively. To see how it is defined click here. Hence 3 ! = 3 * 2 ! = 3 * 2 * 1 = 6 . They are all on functions from integer to integer except the last one.. or shortest strings. 1 ! = 1 * 0 ! = 1 * 1 = 1 . say x. Then by (2). Hence 2 ! = 2 * 1 ! = 2 * 1 = 2 . 1 + 1 (= 2) is in N. 0.WWW. 1. .REVASTUDENTS. 0 is put into N. f(n+1) = (n+1) f(n). Recursive Definition of Function Some functions can also be defined recursively. Using this definition. Tips for recursively defining a set: For the "Basis Clause". How to define function recursively: First the values of the function for the basis elements of the domain are specified. and 2 is the child of 1.

The first principle of mathematical induction states that if the basis step and the inductive step are proven. .REVASTUDENTS.WWW. For since 0 has the property by the basis step. Then it is proven that if an arbitrary natural number. which is 2. can be used. then the next element. Thus the set of natural numbers can be described completely by specifying the basis element (0). that is n + 1. has that property (inductive step). we proceed as follows: Basis Step: Prove that P( ) is true. it is often a good idea to restate P(k+1) in terms of P(k) so that P(k). This process is somewhat analogous to the knocking over a row of dominos with knocking over the first domino corresponding to the basis step. has the same property by the inductive step. that is 0. natural numbers can be proven to have certain properties as follows: First it is proven that the basis element. which is 1. When these two are proven. + n = n( n + 1 )/2 can be proven by mathematical induction by the same token. More generally mathematical statements involving a natural number n such as 1 + 2 + . As a first step for proof by induction. Proceeding likewise. Then since 1 has the property. then P(n) is true for all natural number . aking advantage of this. To prove that a statement P(n) is true for all natural number number. denote it by n. the element next to it. and its elements can be generated one by one starting with 0 by adding 1. has the same property again by the inductive step.. which is assumed to be true. 13 . has the property in question (basis step). Induction: Prove that for any integer then P(k+1) is true. where is a natural . any natural number can be shown to have the property. and the process of generating an element from a known element in the set. if P(k) is true (called induction hypothesis). the set of natural numbers can be defined recursively..INFO Mathematical Induction Subjects to be Learned y y y y y first principle of mathematical induction basis step induction hypothesis induction second principle of mathematical induction Contents First Priciple of Mathematical Induction As we have seen in recursion . then it follows that all the natural numbers have that property. has the property in question. the element next to it.

Proof: Basis Step: If n = 0. which is equal to the RHS for n+1. and RHS = 0 * (0 + 1) = 0 . and somehow use the induction hypothesis.. Induction: Assume that for an arbitrary natural number n. + n) + (n + 1) . then LHS = 0.. Thus LHS = RHS for n+1.. Here let us try LHS for n + 1 = 0 + 1 + .Induction Hypothesis To prove this for n+1. + n = n( n + 1 )/2 . + 2n = n( n + 1 ) . first try to express LHS for n+1 in terms of LHS for n.. + 2n) + 2(n + 1) . and RHS = 0 * (0 + 1) = 0 . the last expression can be rewritten as n( n + 1 )/2 + (n + 1) . the last expression can be rewritten as n( n + 1 ) + 2(n + 1) . first try to express LHS for n+1 in terms of LHS for n.Induction Hypothesis To prove this for n+1. then LHS = 0. Using the induction hypothesis. + 2n + 2(n + 1) = (0 + 2 + . and somehow use the induction hypothesis.Sum of Evens Problem: For any natural number n . Thus LHS = RHS for n+1. ------. + 2n = n( n + 1 ). Hence LHS = RHS. Proof: Basis Step: If n = 0. Here let us try LHS for n + 1 = 0 + 2 + ... Factoring (n + 1) out. Factoring (n + 1) out. + n = n( n + 1 )/2 . we get (n + 1)(n + 2) ... Induction: Assume that for an arbitrary natural number n. 2 + 4 + .WWW. End of Proof. we get (n + 1)(n + 2) / 2 . Mathematical Induction Example 1 --.... 14 . which is equal to the RHS for n+1.. ------.INFO Example: Prove that for any natural number n.. Hence LHS = RHS.. + n + (n + 1) = (0 + 1 + . 0 + 1 + . Using the induction hypothesis. 0 + 1 + .REVASTUDENTS.. 0 + 2 + ..

6}. Let us write a R b Symmetric: R is symmetric if for every a and b in A. and S be a relation from set B into set C. c) RS if and only if there exists b B such that (a. Let A be a set of people and let P = {(a. Then P is a relation on A which we might call a parent-child relation. c) A x C. 2. Relation on a Set A relation from a set A into itself is called a relation on A.WWW. Relations DefinitionRelation Let A and B be sets. A x A. if aRb. then bRa. then aRc. if aRb and bRc. 1. PropertiesOf Relations Assume R is a relation on set A. symmetric and transitive.INFO End of Proof. 3. b) Rand (b. is the composition of P with itself and it is a relation which we know as grandparentgrandchild relation. Composition Let R be a relation from a set A into set B. b and c in A. The composition of R and S. 4. b) R . b) | a A ^ b A ^ a is a child of b } . Reflexive: R is reflexive if for every a A. A binary relation from A into B is any subset of the Cartesian product A x B. where P is the parent-child relation given above. For example PP. a R a. Equivalence: R is an equivalence relation on A if R is reflexive. 3. 15 . Transitive: R is transitive if for every a. R and S of Example 2 above are relations on A = {2. in other words.REVASTUDENTS. 5. is the set of pairs of the form(a. written as RS. where (a. c) S. R to denote (a.

and b is called the image of a under f . Definition (function): A function. c> are in the relation. a social security number uniquely identifies the person.WWW. The set A in the above definition is called the domain of the function and B its codomain. tax rate. a function is quite like a relation. letter grade. The image of the domain under f is called the range of f . and so on. formally. The set of images of the elements of a set S under a function f is called the image of the set S under f. from a set A to a set B is a relation from A to B that satisfies 1.REVASTUDENTS. respectively) is assigned. we define a function as a special type of binary relation. and is denoted by f(S) . tuple of test and exam scores. homeworks and projects) some member of another set (person. homeworks and projects.INFO Functions Subjects to be Reviewed y y y y y y y y y y y y function domain. the income tax rate varies depending on the income. income. Thus. f is a function if it covers the domain (maps every element of the domain) and it is single valued. b> and <a. In fact. As you might have noticed. if <a. the final letter grade for a course is often determined by test and exam scores. In all these cases to each member of a set (social security number. denote it by f. then b = c . b> is denoted as f(a) = b . 16 . and 2. codomain image image of set range sum of functions product of functions one-to-one function (injection) onto function (surjection) one-to-one onto function (bijection) inverse function composite function Contents A function is something that associates each element of a set with an element of another set (which may or may not be the same as the first set). The relation given by f between a and b represented by the ordered pair <a. The concept of function appears quite often even in nontechnical contexts. For example. f(S) = { f(a) | a I S }. for each element a in A. there is an element b in B such that <a. where S is a subset of the domain A of f . that is. b> is in the relation.

INFO Example: Let f be the function from the set of natural numbers N to N that maps each natural number x to x2 . for example. x = y . if and only if whenever f(x) = f(y) . Then the domain and codomain of this f are N. 1. Example: The function f(x) = x2 from the set of natural numbers N to N is a one-to-one function.. Definition (onto): A function f from a set A to a set B is said to be onto(surjective) . because for example f(1) = f(-1) = 1 . Then the sum and the product of f and g are defined as follows: For all x. f is onto if and only if f( A ) = B . the image of. In each figure below. Note that f(x) = x2 is not one-to-one if it is from the set of integers(negative as well as non-negative) to N . and arrows show < x. under this function is 9.} . the points on the left are in the domain and the ones on the right are in the codomain. Definition (sum and product): Let f and g be functions from a set A to the set of real numbers R. These concepts are illustrated in the figure below. However. { 0. . Thus it is a bijection. 16. Example: The function f(x) = 2x from the set of natural numbers N to the set of nonnegative even numbers E is an onto function. Example: Let f(x) = 3x + 1 and g(x) = x2 .e. ( f + g )(x) = f(x) + g(x) . 17 . if and only if for every element y of B .. Then ( f + g )(x) = x2 + 3x + 1 .REVASTUDENTS. 9. where f(x)*g(x) is the product of two real numbers f(x) and g(x). and ( f*g )(x) = 3x3 + x2 Definition (one-to-one): A function f is said to be one-to-one (injective) . that is. because. and its range is the set of squares. nothing in N can be mapped to 3 by this function. 4. Definition (bijection): A function is called a bijection . i. f(x) = 2x from the set of natural numbers N to N is not onto. Example: The function f(x) = 2x from the set of natural numbers N to the set of nonnegative even numbers E is one-to-one and onto.. f(x) > relation. ( f*g )(x) = f(x)*g(x) .WWW. there is an element x in A such that f(x) = y . Every bijection has a function called the inverse function. and for all x. say 3. if it is onto and one-to-one.

Then f( g(x) ) = ( x + 1 )2 .INFO Definition (inverse): Let f be a bijection from a set A to a set B. Note that such an x is unique for each y because f is a bijection. Example: Let f(x) = x2 . and g(x) = x + 1 . Then the composition of functions f and g . where f(x) = y . Therefore one can also talk about composition of functions. A function is a relation. is the function from A to C that satisfies fg(x) = f( g(x) ) for all x in A . if for every element y of B. 18 . For example. It is also a bijection. denoted by fg . Then the function g is called the inverse function of f. Definition (composite function): Let g be a function from a set A to a set B . and it is denoted by f -1 . the rightmost function in the above figure is a bijection and its inverse is obtained by reversing the direction of each arrow.REVASTUDENTS.WWW. Example: The inverse function of f(x) = 2x from the set of natural numbers N to the set of non-negative even numbers E is f -1(x) = 1/2 x from E to N . and let f be a function from B to a set C . g(y) = x .

Also for some important classes of problems. 19 . are quite useful for modeling systems used in practice such as co9mputer network communication protocols. in general there are more than one NFAs and DFAs that reconize one language. They are devices that nondeterministic finite automata with are conceptually simpler and easier to use recognize regular languages. if the number of states of DFA is minimized. In asddition two of the formal languages. together with regular expressions.). It can be rigorously shown that some problems can not be solved by computers in any finite amount of time and that some others are practically unsolvable because of the time it takes to solve them. Then we investigate various kinds of finite automata: deterministic finite automata (DFA).INFO Introduction to Language Our first and one of the main topic for this course is language. natural languages etc. lexical analyzers and parser for compilers for programming languages. NFA and NFAwhen modeling a system because there are no restrictions on transitions for them unlike for DFA. regular and context-free languages. These formal languages and automata capture the essense of various computing devices and computation in a very simple way.e. A language is. Then after seeing yet another way of representing regular laguages. These formal languages are characterized by grammars which are essentially a set of rewrite rules for generating strings belonging to a language as we see later. However. Also there are various kinds of computing devices called automata which process these types of languages Thus formal languages can also be characterized by the computing devices which process them. Type 3 is a subset of type 2 which is a subset of type 1 and type 0 is the most general including the other three as a subset. which are a method of representing regular languages. checking whether or not a string is in a language.WWW. Programming langauges we use are a language in that sense. The four classes are regular (or type 3) languages. context-free (or type 2) languages. the simplest of the four formal languages. solving them can be seen as recognizing languages i. Using automata and formal languages we can study limitations of computer and computation.REVASTUDENTS. On the other hand DFAs are suited for writing a simulator program because there is no nondeterminism such as going to two or more states from a state upon reading one input symbol. Others such as languages of logics. context-sensitive (or type 1) languages and phrase structure (or type 0) languages. nondeterministic finite automata (NFA) and transitions (NFA. Then we study regular languages. languages of mathematics. We are going to see an algorithm for converting NFA. In the following chapters we first learn about languages.to NFA which recognizes the same language and another for NFA to DFA conversion. regular grammars. then the resulting DFA is unique up to the state names for a given regular language. What we are going to study on languages in this course are four classes of languages called (Chomsky) formal languages and their properties. in this course. are all languages in that sense. we are going to learn modeling of systems finite automata. a set of strings of symbols. As we are going to learn next.

L and it is also a language. Operations on languages Since languages are sets. uv is the string obtained by appending the sequence of symbols of v to that of u. b. 111} is a language (over alphabet {0. {a. Let u and v be strings. So it is not . Note that u and v may be an empty string. ab. For a string w its length is represented by |w|. So a string is a substring of itself.REVASTUDENTS. 1}. Then the concatenation of L1 with L2 is denoted as L1L2 and it is defined as L1L2 = { uv | u I L1 and v 20 . 2.INFO Definitions on Language Subjects to be Learned y y y y alphabet string (word) language operations on languages: concatenation of strings. it has no symbols. A language is a set of strings over an alphabet. Kleene star Contents Here we are going to learn the concept of language in very abstract and general sense. For any alphabet denoted by . The number of symbols in a string is called the length of the string. We are going to use first few symbols of English alphabet such as a and b to denote symbols of an alphabet and those toward the end such as u and v for strings. Though has no symbols.b}) and {0. That is. Basic concepts First. The set { } is a language which has one string. The empty string (also called null string) is the string with length 0. b} is another alphabet with two symbols and English alphabet is also an alphabet. A string x is called a substring of another string y if there are strings u and v such that y = uxv. It can be defined more formally by recursive definition. the set of all strings over (including the empty string) is empty. this set has an object in it. For example {0. 3. For example if u = aab and v = bbab. then uv = aabbbab.WWW. Then uv string is denoted by denotes the string obtained by concatenating u with v. all the set operations can be applied to languages. Thus | | = 0. union. The empty (capital lambda). Thus a language over alphabet is a subset of . baa} is a language (over alphabert {a. Thus the union. b} and 0. operations on languages and some of their properties. intersection. A string x is a prefix of another string y if there is a string v such that y = xv. v is called a suffix of y. A string (also called a word) is a finite sequence of symbols of an alphabet. Thus {a. complement of a language L over an alphabet is Another operation onlanguages is concatenation. Note that vu = bbabaab { uv. that is. The . an alphabet is a finite set of symbols. 10 and 001 are examples of string over alphabet {0. intersetion and difference of two languages over an alphabet are languages over . a and aabab are examples of string over alphabet {a. Some special languages The empty set J is a language which has no strings. Let L1 and L2 be languages.1}). 1. 1} is an alphabet with two symbols. namely .

Thus L+ is the set of strings obtained by concatenating one or more strings of L. Recursive definition of L+: Basis Clause: L L+ Inductive Clause: For any x I L+ and any w I L. the extremal clause is not necessary. then L+ = { aba. Recursive definition of Lk: Basis Clause: L0 = { } Inductive Clause: L(k+1) = Lk L.INFO IL2 }. then L* = { .. bbaba. Hence Lk is the set of strings that can be obtained by concatenating k strings of L. ababb. bb }. . abaaba. bb }. Here a0 = and u0 = . babb. b} {aaa.REVASTUDENTS. bb. baaba}. That is L1L2 is the set of strings obtained by concatenating strings of L1 with those of L2.. uk denotes the concatenation of k u's. For example if L = { aba. abaaba. For a string u I Similarly for a language L. a's. For example Lk can be defined recursively as follows.e. bbaba. ababb. baaa. * The following two types of languages are generalizations of them quite often in this course. } The * in * is also the same Kleene star defined above. xw I L*. Lk means the concatenation of k L's. bbbb.. ababb. } Let us also define number k } . Powers : For a symbol a I and a natural number k. For example {ab. Theorem 1: Ln L* (i. and we are going to see Recursive definition of L*: Basis Clause: I L* Inductive Clause: For any x I L* and any w I L. 4. . aaba} = {abaaa. L* is the set of strings obtained by concatenating zero or more strings of L as we are going to see in Theorem 1. bbbb. L0 L L2 ... aba.WWW. This * is called Kleene star. These powers can be formally defined recursively. ) as ={x|x Lk for some natural Theorem 2: 21 . xw I L+. abb. Extremal Clause: Nothing is in L+ unless it is obtained from the above two clauses. ak and uk can be defined similarly. abaaba.. Since Lk is defined for natural numbers k. For example if L = { aba. bb. ak represents the concatenation of k and a natural number k. Extremal Clause: Nothing is in L* unless it is obtained from the above two clauses.

for each wi there are strings wi1.. w2. Conversely ( L* )* L* can be proven as follows: Let x be an arbitrary nonempty string of ( L* )*. Let us list one of them as a theorem and prove it... Since L* (L*)* and ( L* )* L* . Theorem 5: L* = (L*)*. by applying Theorem 2 to the language L* we can see that L* (L*)*. . wk are strings of L*. w1w2. 22 . w2.... .. wimi in L such that wi = wi1wi2... Proof: Because by Theorem 2.wk .. Hence ( L* )* L* ... i..INFO Theorem 3: Theorem 4: L+ = L L* = L*L Note: According to Theorems 2 and 3.w1m1 w21.e. Hence x is in L* .REVASTUDENTS.wk for some k. L* and L* have a number of interesting properties. Since w1.wm1. wi2. Then there are nonempty strings w1.wmmk . If x is an empty string. any nonempty string in L* or L+ can be expresssed as the concatenation of strings of L. ....w2m2.. wk in L* such that x = w1 w2. where wi's are strings of L..wimi Hence x = w11 . . then it is obviously in L* ..... L* = ( L* )* .WWW.

from a given state there is exactly one transition (there can be no transitions from a state also) and we are sure (or can determine) to which state the machine enters. H. H is shown the transition table 2. finite set of input alphabets.4. q0. § is non-empty.REVASTUDENTS. finite set of states. §.WWW. w) A } The non-acceptance of the string w by an FA or DFA can be defined in formal notation as: L(M) = { w | w §* and H*(q0. So.b Fig. q2. A Q is set of accepting or final states. Since it has finite number of states the machine is called Deterministic finite machine or Deterministic Finite Automaton or Finite State Machine (FSM). the machine is called Deterministic machine.7 Transition diagram to accept string ab(a+b)* So. H is transition function.2. which is a mapping from Q x § to Q. A) where Q is non-empty. q0. q3} § = {a.b q b q a. H pq0 q1 q2 q3 n7p a b q1 q3 q3 q2 q2 q2 q3 q3 23 a a q b q2 n States p . q1. Note: For each input symbol a. b} q0 is the start state A = {q2}. w) A } Obtain a DFA to accept strings of a¶s and b¶s starting with the string ab a. H. § . q0 Q is the start state.INFO Definition: A DFA is 5-tuple or quintuple M = (Q. the DFA which accepts strings of a¶s and b¶s starting with the string ab is given by M = (Q. A) where Q = {q0. The language accepted by DFA is L(M) = { w | w §* and H*(q0.

2.WWW. a q a b q a q b a q b b Fig.b Obtain a DFA to accept strings of a¶s and b¶s except those containing the substring aab.REVASTUDENTS.2. b q0 a b q0 b q0 a b q1 a b q2 a a q1 b q3 a q4 a.22 DFA to accept even no. of a¶s and b¶s 24 . b q0 a b q1 a q2 a b q3 a.b Obtain DFAs to accept strings of a¶s and b¶s having exactly one a. b b q1 a q2 a.22. b a.INFO Draw a DFA to accept string of 0¶s and 1¶s ending with the string 011.b Obtain a DFA to accept strings of a¶s and b¶s having even number of a¶s and b¶s The machine to accept even number of a¶s and b¶s is shown in fig. 1 q0 0 0 q1 1 0 q2 0 1 q3 Obtain a DFA to accept strings of a¶s and b¶s having a sub string aa 1 b q0 a b q1 a q2 a.

Pascal. A) be a DFA.REVASTUDENTS. q0. Fortran or any other compiler is designed using the finite automata. §. to strip of the comments etc.INFO q0 q1 a b b b b a q2 q3 a a q0 a b q2 a a q0 a b q2 a Regular language Definition: Let M = (Q. The DFAs are extensively used in the building the various phases of compiler such as y y y y Lexical analysis (To identify the tokens.a WWW. * Applications of Finite Automata * String matching/processing Compiler Construction The various compilers such as C/C++. identifiers. H.) Syntax analysis (To check the syntax of each statement or control statement used in the program) Code optimization (To remove the un wanted code) Code generation (To generate the machine code) b a q3 b b q1 b a q3 b b q1 Other applications 25 . The language L is regular if there exists a machine M such that L = L(M).

This function shows the change of state from one state to a set of states based on the input symbol. Non deterministic finite automata(NFA) Definition: An NFA is a 5-tuple or quintuple M = (Q. automatic sensors.. § is set of input alphabets (from which a string can be formed).REVASTUDENTS.. A Q is set of final states. finite set of input alphabets. In game theory and games wherein we use some control characters to fight against a monster. 3. q0 Q is the start state.e. automatic traffic signals. §. elevators. §. 2. Finite automaton is very useful in hardware design such as circuit verification. q0 is the start state and A is the final or accepting state. It is not possible to list all the applications as there are infinite number of applications. A) be a DFA where Q is set of finite states. q0. H is transition function which is a mapping from Q x {§ U I} to subsets of 2Q. economics. sometimes it is very essential to solve an un-decidable problem. Acceptance of language Definition: Let M = (Q. linguistics etc. to name a few. The string (also called language) w accepted by an NFA can be defined in formal notation as: L(M) = { w | w §*and H*(q0. q0. using theory of computation. multi-language dictionaries. Any editor that we use uses finite automaton for implementation. computer graphics. we can find the approximate solutions. A) where Q is non empty. finite set of states.WWW. § is non empty. remote sensing or controller etc. to indent the documents. 4. H. Even though there is no general solution exists for the specified problem. finite automaton plays a very important role. H is transition function from Q x {§UI} to 2Q. in calculators to evaluate complex expressions based on the priority of an operator etc. This section lists some applications: 1. Finite automaton is very useful in recognizing difficult problems i. Large natural vocabularies can be described using finite automaton which includes the applications such as spelling checkers and advisers. in design of the hardware board (mother board or any other hardware unit). H. w) = Q with atleast one Component of Q in A} Obtain an NFA to accept the following language L = {w | w ababn or aban where n u 0} The machine to accept either ababn or aban where n u 0 is shown below: b q1 I q0 a q2 b q3 a q4 26 a .INFO The concept of finite automata is used in wide applications. radio controlled toys.

qm.qk]. if at least one of the state in qa. §N. AN) be an NFA and accepts the language L(MN). Add the transition from [qi.qn] on the input symbol a iff the state [ql. a) U ««HN(qk.qc] should be the final state of NFA.REVASTUDENTS. There should be an equivalent DFA MD = (QD. 1 q 1 2 1 Step1: q0 is the start of DFA (see step1 in the conversion procedure).1 q 0. qb. Step4: If epsilon () is accepted by NFA. QD = {[q0]} (2. q0. So. Add the state [ql.7) Step2: Find the new states from each state in QD and obtain the corresponding transitions. qb.qn] say. qb.INFO 2.qn] is added to QD in the previous step. a) = HN(qi. HD. the transitions for each input symbol in § can be obtained as shown below: 1. Convert the following NFA into an equivalent DFA.WWW.«. qm.qn] to QD.e. Step3: The state [qa.1 Conversion from NFA to DFA Let MN = (QN. The procedure to convert an NFA to its equivalent DFA is shown below: Step1: The start state of NFA MN is the start state of DFA MD.«. So. HN. qc AN i. qm... qj. The way to obtain different transitions is shown in step2.«.qc] QD is the final state.«. Step2: For each state [qi. q0.«. 2. qj.«. «. 0 q0 0. at least one of the component in [qa. HD([qi.«.qk] in QD. Consider the state [q0]: 27 .qk] to [ql. if it is not already in QD. AD) such that L(MD) = L(MN). §D. then start state q0 of DFA is made the final state. qj.«.«. a) U HN(qj. qm. add q0(which is the start state of NFA) to QD and find the transitions from this state. a) = [ql. 3.

13) 0 [q0.10).7). q1].10) The corresponding transitions on a = 0 and a = 1 are shown below. [q0. q1]. 1) Since the states obtained in (2. [q1]. add these two states to QD so that QD = {[q0].WWW. q1].9) are not in QD(2.9) When a = 1 HD([q0]. 1) = [q1] (2. q1]. q1. q1]: When a = 0 HD([q0. q1]. q2]. q2] } and add the transitions on a = 0 and a = 1 as shown below: § (2. 1) 1) = {q1} U {q2} = [q1. [q1. 0) = HN(q0.11) and (2. 0) 0) = {q0. q1] 1 [q1] 28 . 0) U HN(q1. 0) = HN([q0].8) and (2. add these two states to QD so that QD = {[q0].11) When a = 1 HD([q0. [q0. q1] [q1] Consider the state [q0. = HN([q0. q2] (2. q1} U {q2} = [q0.12) are the not defined in QD(see 2. 1) U HN(q1.8) = HN([q0]. 1) = HN(q0.INFO When a = 0 HD([q0]. [q1] } (2. 0) = [q0. q1. [q0.REVASTUDENTS. = HN([q0. q2] (2. q1]. § H [q0] Q [q0.12) Since the states obtained in (2. q1] (2.

0) = [q2] (2. q2] Consider the state [q1]: When a = 0 HD([q1].13).q2]: When a = 0 HD([q0.q1. add the state q2 to QD so that QD = {[q0]. 0) {q0. q2] [q1. q1. 0) HN(q0.q1.INFO H Q [q0] [q0. q2] 0 [q0. q1] [q0. q2] 1 [q1] [q1.14) and (2.q1} U {q2} U {J} [q0. q2].16) and add the transitions on a = 0 and a = 1 as shown below: § H [q0] [q0.q2] 29 .q1. q1]. 0) = HN([q1]. [q0. [q1. [q1]. 1) = [q2] (2. 0) U HN(q2.15) are same and the state q2 is not in QD(see 2.q1. [q0.REVASTUDENTS.WWW. 0) U HN(q1. q2] [q2] Q Consider the state [q0. 1) = HN([q1]. q1.14) When a = 1 HD([q1].q2]. q1. 0) = = = = HN([q0. q1. q1. q1] [q1] [q0.q2]. q2] [q1. q2] [q2] 1 [q1] [q1. [q2]} (2. q2] [q2] 0 [q0. q2].15) Since the states obtained in (2. q1] [q1] [q0. q1] [q0.

q1] [q0. see 2. q1.REVASTUDENTS. 0) = = = = HN([q1. But. But. 1) HN(q1. 0) {q2} U {J} [q2] (2. q1.q2].16).18) Since the states obtained in (2. 0) U HN(q2.q2]. 0) HN(q1.q2].q2] q2] [q1.17) and (2.18) are not new states (are already in QD.INFO (2. do not add these two states to QD.WWW. q2] Q Consider the state [q1. 1) U HN(q2. q2] [q2] H 1 [q1] [q1.q2]: When a = 0 HD([q1.q1.q2]. 1) {q1} U {q2} U {q2} [q1. 1) = = = = HN([q0. q2] (2.q1. q1] [q0. q2] [q1] [q2] [q0. 1) U HN(q1. the transitions on a = 0 and a = 1 should be added to the transitional table as shown below: § 0 [q0] [q0.19) HN([q1. 1) HN(q0. 1) = = = = Since the states obtained in (2. [q0.17) When a = 1 HD([q0.q1. 1) U HN(q2.20) are not new states (are already in QD see 2. do not add these two states to QD. 1) {q2} U {q2} [q2] (2.19) and (2.16). q2] [q2] [q1. the transitions on a = 0 and a = 1 should be added to the transitional table as shown below: § 30 .20) When a = 1 HD([q1.q2].q2].

16). 1 [q2] 31 .22) are not new states (are already in QD.35. H [q0] 0 [q0.21) = HN([q2].q2] q2] [q1. q2] [q2] [q1. q2] [q2] [q0.q1] [q0. 1 [q0. q2] [q1] [q2] [q0. q1] [q0.q2] [q2] [q2[q1] ] J [q1. q2] [q2] [q0. q 2] 0. q 2] 1 [q1.q1. The final transitional table is shown in table 2.q1. q1. [q0. q2] [q2] Q Consider the state [q2]: When a = 0 HD([q2]. q1] [q0.q2] 0 [q0] 1 [q1.REVASTUDENTS.q1. q 1. 0) = {J} (2. q2] [q2] [q2] H 1 [q1] [q1. q1] [q0. 0) = HN([q2]. But. see 2. q1. do not add these two states to QD.WWW.21) and (2.q2] [q0. q2] [q2] [q2] 1 [q1] [q1. q 1] [q1] 0 1 0. and final DFA is shown in figure 2.INFO 0 [q0] [q0. 1) = [q2] (2.22) When a = 1 HD([q2]. the transitions on a = 0 and a = 1 should be added to the transitional table. q1. 1) Since the states obtained in (2.14.

2.REVASTUDENTS.INFO Fig.35 The DFA 32 .WWW.

**WWW.REVASTUDENTS.INFO
**

Convert the following NFA to its equivalent DFA.

I I 0 a 1 b 2 I 3 I 6 b I 7 I 4 a 5 8 9

Let QD = {0} Consider the state [A]: When input is a: H(A, a) = HN(0, a) = {1} (B) = HN(0, b) = {J}

(A)

When input is b: H( A, b)

Consider the state [B]: When input is a: H(B, a) = HN(1, a) = {J}

When input is b: H( B, b)

= HN(1, b) = {2} = {2,3,4,6,9}

(C)

This is because, in state 2, due to I-transitions (or without giving any input) there can be transition to states 3,4,6,9 also. So, all these states are reachable from state 2. Therefore, H(B, b) = {2,3,4,6,9} = C Consider the state [C]: When input is a: H(C, a) = HN({2,3,4,6,9}, a) = {5} = {5, 8, 9, 3, 4, 6} 33

WWW.REVASTUDENTS.INFO = {3, 4, 5, 6, 8, 9} order) (D) (ascending

This is because, in state 5 due to I-transitions, the states reachable are {8, 9, 3, 4, 6}. Therefore, H(C, a) = {3, 4, 5, 6, 8, 9} = D When input is b: H( C, b)

HN({2, 3, 4, 6, 9}, b) {7} {7, 8, 9, 3, 4, 6} {3, 4, 6, 7, 8, 9}(ascending order) (E) This is because, from state 7 the states that are reachable without any input (i.e., Itransition) are {8, 9, 3, 4, 6}. Therefore, H(C, b) = {3, 4, 6, 7, 8, 9} = E

= = = =

Consider the state [D]: When input is a: H(D, a) = = = = HN({3,4,5,6,8,9}, a) {5} {5, 8, 9, 3, 4, 6} {3, 4, 5, 6, 8, 9} order) (D)

(ascending

When input is b: H(D, b) = = = = HN({3,4,5,6,8,9}, b) {7} {7, 8, 9, 3, 4, 6} {3, 4, 6, 7, 8, 9} order) (E)

(ascending

Consider the state [E]: When input is a: H(E, a) = = = = HN({3,4,6,7,8,9}, a) {5} {5, 8, 9, 3, 4, 6} {3, 4, 5, 6, 8, 9}(ascending order) (D)

When input is b: H(E, b) = HN({3,4,6,7,8,9}, b) 34

WWW.REVASTUDENTS.INFO = {7} = {7, 8, 9, 3, 4, 6} = {3, 4, 6, 7, 8, 9}(ascending order) (E) Since there are no new states, we can stop at this point and the transition table for the DFA is shown in table 2.15. § H A B a B D D D b C E E E

Q

C

D E Table 2.15 Transitional table The states C,D and E are final states, since 9 (final state of NFA) is present in C, D and E. The final transition diagram of DFA is shown in figure 2.36 a A a B b C b E b Fig. 2.36 The DFA a a b D

Regular Languages

Regular expression Definition: A regular expression is recursively defined as follows. 1. 2. 3. 4. J is a regular expression denoting an empty language. I-(epsilon) is a regular expression indicates the language containing an empty string. a is a regular expression which indicates the language containing only {a} If R is a regular expression denoting the language LR and S is a regular expression denoting the language LS, then 35

b. (a+b)* (a + Set of strings of a¶s and b¶s ending with either a bb) or bb (aa)*(bb)*b Set of strings consisting of even number of a¶s followed by odd number of b¶s (0+1)*000 Set of strings of 0¶s and 1¶s ending with three consecutive zeros(or ending with 000) (11)* Set consisting of even number of 1¶s Table 3.WWW.LS. ba and bb.1 shows some examples of regular expressions and the language corresponding to these regular expressions. (a+b)*aa(a+b) Set of strings of a¶s and b¶s having a sub string * aa. String of a¶s and b¶s of even length can be obtained by the combination of the strings aa.INFO a. the regular expression can be of the form (aa + ab + ba + bb)* The * closure includes the empty string. R. The language may even consist of an empty string denoted by I. aa*bb*cc* Set of string consisting of at least one µa¶ followed by string consisting of at least one µb¶ followed by string consisting of at least one µc¶. 5. ab. Regular expressions (a+b)* Meaning Set of strings of a¶s and b¶s of any length including the NULL string. c. The table 3. R+S is a regular expression corresponding to the language LRULS. So.1 Meaning of regular expressions Obtain a regular expression to accept a language consisting of strings of a¶s and b¶s of even length. (a+b)*abb Set of strings of a¶s and b¶s ending with the string abb ab(a+b)* Set of strings of a¶s and b¶s starting with the string ab. 36 . a*b*c* Set of string consisting of any number of a¶s(may be empty string also) followed by any number of b¶s(may include empty string) followed by any number of c¶s(may include empty string).REVASTUDENTS.S is a regular expression corresponding to the language LR. R* is a regular expression corresponding to the language LR*.. The expressions obtained by applying any of the rules from 1-4 are regular expressions. a+b+c+ Set of string consisting of at least one µa¶ followed by string consisting of at least one µb¶ followed by string consisting of at least one µc¶.

I and a The schematic representation of a regular expression R to accept the language L(R) is shown in figure 3. 37 . A) which accepts L(R). So.¶. So. §. Let us take each case separately and construct equivalent machine. the regular expression can be of the form (aa + ab + ba + bb)* (a+b) String of a¶s and b¶s of odd length can also be obtained by the combination of the strings aa. 3.a.WWW. the corresponding machines to recognize these expressions are shown in figure 3. ba and bb preceded by either a or b.REVASTUDENTS. ab. H1.1 NFAs to accept J. J. µ-µ and µ. q2. L(R) q M f Fig 3. Let M1 = (Q1. So. Proof: By definition. §1. H. Obtain NFA from the regular expression Theorem: Let R be a regular expression. Then there exists a finite automaton M = (Q.1.2 Schematic representation of FA accepting L(R) In the definition of a regular expression it is clear that if R and S are regular expression. q1. where q is the start state and f is the final state of machine M. then R+S and R. String of a¶s and b¶s of odd length can be obtained by the combination of the strings aa.INFO Note: This regular expression can also be represented using set notation as L(R) = {(aa + ab + ba + bb)n | n u 0} Obtain a regular expression to accept a language consisting of strings of a¶s and b¶s of odd length.1. a variety of regular expressions can be obtained for a language and all are equivalent.S and R* are regular expressions which clearly uses three operators µ+¶. Let M2 = (Q2. f2) be a machine which accepts the language L(R2) corresponding to the regular expression R2. §2.c respectively.b and 3. ab. the regular expression can also be represented as (a+b) (aa + ab + ba + bb)* Note: Even though these two expression are seems to be different. ba and bb followed by either a or b.1. q0. I and a are regular expressions. q0 qf q0 I (b) qf q0 a (c) qf J (a) Fig 3.2. H2. the language corresponding to those two expression is same. So. f1) be a machine which accepts the language L(R1) corresponding to the regular expression R1.

INFO 38 .REVASTUDENTS.WWW.

L(R1) L(R2) I q1 M1 q2 M2 f f Fig.a.R2).3 that the machine can either accept L(R1) or L(R2).5.REVASTUDENTS. Here. 3. Thus. R2. We can construct an NFA which accepts either L(R1) or L(R2) which can be represented as L(R1 + R2) as shown in figure 3. Case 2: R = R1 . I q0 I q1 f M1 L(R1) I (a) I q0 I q1 f M1 I 39 I qf I qf .4. the machine moves to f2 which is the final state. We can construct an NFA which accepts either L(R1)*) as shown in figure 3. R2) as shown in figure 3. In state q2.3. without any input there will be a transition from state f1 to state q2. q0 is the start state of the combined machine and qf is the final state of combined machine M.5.INFO Case 1: R = R1 + R2. q1 which is the start state of machine M1 becomes the start state of the combined machine M and f2 which is the final state of machine M2. becomes the final state of machine M and accepts the language L(R1. Case 3: R = (R1)*. L(R1) I q0 I q1 f q2 f M1 M2 L(R2) I qf I Fig. R2) It is clear from figure 3. upon accepting L(R2).WWW.4 that the machine after accepting L(R1) moves from state q1 to f1. Since there is a I-transition. 3.4To accept the language L(R1 . We can construct an NFA which accepts L(R1) followed by L(R2) which can be represented as L(R1 .b.3 To accept the language L(R1 + R2) It is clear from figure 3. It can also be represented as shown in figure 3.

5 To accept the language L(R1)* It is clear from figure 3. I I 2 I 3 I 6 b I 7 I 4 a 5 I 8 I 9 Step 5: The machine to accept ab is shown below. 3. q0 is the start state qf is the final state.REVASTUDENTS. Step 1: The machine to accept µa¶ is shown below.WWW. 0 a 1 b 2 Step 6: The machine to accept ab(a+b)* is shown below. Obtain an NFA which accepts strings of a¶s and b¶s starting with the string ab. Here. 4 a 5 Step 2: The machine to accept µb¶ is shown below. The regular expression corresponding to this language is ab(a+b)*.INFO (b) Fig. 6 b 7 Step 3: The machine to accept (a + b) is shown below. I a I 0 a 1 b 2 I 3 I 4 5 I 8 I 9 40 6 b 7 I .5 that the machine can either accept I or any number of L(R1)s thus accepting the language L(R1)*. I 3 I 6 b 7 I 4 a 5 I 8 Step 4: The machine to accept (a+b)* is shown below.

A) be an FA recognizing the language L. H. r2.1 q1 The figure can be reduced as shown below: 01 q0 10 41 . If q0 and q1 are the final states then the regular expression can be of the form (3. 3. Any graph can be reduced to the graph shown in figure 3.1) r q1 r Note: 1.3) r = r1* + r1*r2 r4* Obtain a regular expression for the FA shown below: 0 q0 1 0 q2 1 1 q3 0 0.REVASTUDENTS.2) r = r1*r2 r4* 3.INFO Fig.9.9 Generalized transition graph where r1. the regular expression can be of the form (3. 2. Consider the generalized graph r1 q0 r Fig. The general procedure to obtain a regular expression from FA is shown below.1 and obtain the final regular expression. The regular expression for this can take the form: r = r1*r2 (r4 + r3r1*r2)* (3. q0. If r3 is not there in figure 3. Then there exists an equivalent regular expression R for the regular language L such that L = L(R). Then substitute the regular expressions appropriately in the equation 3. r3 and r4 are the regular expressions and correspond to the labels for the edges.WWW.9. 3.6 To accept the language L(ab(a+b)*) Obtain the regular expression from FA Theorem: Let M = (Q. §.

INFO It is clear from this figure that the machine accepts strings of 01¶s and 10¶s of any length and the regular expression can be of the form (01 + 10)* What is the language accepted by the following FA 0 q0 1 q1 1 0 0.WWW. So. In UNIX operating system. We can match an identifier or a decimal number or we can search for a string in the text. 0 q0 1 q1 1 The state q0 is the final state and at this point it can accept any number of 0¶s which can be represented using notation as 0* q1 is also the final state. it can be removed and the following FA is obtained. to reach q1 one can input any number of 0¶s followed by 1 and followed by any number of 1¶s and can be represented as 0*11* So.REVASTUDENTS. q2 1 Since. we can use the editor ed to search for a specific pattern in the text. For example. Applications of Regular Expressions Pattern Matching refers to a set of objects with some common properties. state q2 is the dead state. An application of regular expression in UNIX editor ed.E = 0* + 0*11* = 0* ( + 11*) = 0* ( + 1+) = 0* (1*) = 0*1* It is clear from the regular expression that language consists of any number of 0¶s (possibly I) followed by any number of 1¶s(possibly I). So. the regular expression is R. if the command specified is 42 . the final regular expression is obtained by adding 0* and 0*11*.

INFO /acb*c/ then the editor searches for a string which starts with ac followed by zero or more b¶s and followed by the symbol c. 43 . As the input can vary dynamically.WWW.REVASTUDENTS. it is challenging to write programs for string patters of these kinds. Note that the editor ed accepts the regular expression and searches for that particular pattern in the text.

which has finite number of states. Design of switching circuits. which is not possible with a finite state machine. such that: 1. state 3 remembers that two 0¶s have come and from there state 5 remembers that two 1¶s are accepted. In other words if we have to remember n. Ex.WWW. Closure properties of regular languages Used to build recognizers for languages that are constructed from other languages by certain operations. Then there exists a constant µn¶ (which depends on L) such that for every string w in L such that |w| n. Let L= {0212} 0 1 2 0 3 1 1 6 0. Pumping Lemma for regular languages ( Explanation) Let L = {0n1n | n 1} There is no regular expression to define L.1 0 1 4 0 1 5 0. Pumping Lemma (PL) for Regular Languages Theorem: Let L be a regular language. which varies from 1 to gwe have to have infinite states. 00*11* is not the regular expression defining L.REVASTUDENTS. Automata for intersection of two regular languages Decision properties of regular languages ± ± Used to find whether two automata define the same language Used to minimize the states of DFA eg. we can break w into three strings. This implies DFA has no memory to remember arbitrary µn¶.INFO Properties of regular languages Pumping Lemma Used to prove certain languages like L = {0n1n | n 1} are not regular. |y| > 0 44 . w=xyz.1 State 6 is a trap state.

REVASTUDENTS. General Method of proof: (i) (ii) Select w such that |w| u n Select y such that |y| u 1 (iii) Select x such that |xy| e n (iv) Assign remaining string to z (v) Select k suitably to show that. Therefore xykz = a1 -----.WWW.an-1 is accepted a1 -----.a2 . For all k 0. y=an and z = I. 45 .a2 .a3 -----an-1 . If you want to show that language is regular.an+1 is accepted k=10 a1 -----. |w| = n n. the string xykz is also in L. PROOF: Let L be regular defined by an FA having µn¶ states. Uses of Pumping Lemma: . Let w = xyz where x= a1.an is accepted a1 -----. It should never be used to show that some language is regular. Let the start state be P1. DFA or NFA. |xy| n 3. Example 1.This is to be used to show that. certain languages are not regular. Let w= a1.INFO 2.an+9 is accepted and so on. resulting string is not in L.an-1 (an)k I k=0 k=1 k=2 a1 -----. write separate expression.a3----an and is in L.

w=xykz.INFO To prove that L={w|w I anbn. we get an-l bn L. where l > 0 (Because |y| > 0). Hence the Language is not regular.2.2. Let n is the constant (PL Definition).1. aba. i.------g. That is. Let n is the constant (PL Definition). such that |w|=2n. Then.b}*} is not regular. it is not a palindrome.«} Proof: = anban. (Because |xy| n).------g.2. xy contain only a¶s. should belong to L. (Because |xy| n). abbbba. for all k=0.2. Consider a word w in L.1. such Let L be regular. Consider a word w in L. That is an-l (al)k ban L.------g. where n 1} is not regular Proof: Let L be regular. y and z can be as follows from the definition of PL . Let |y|=l. Let |y|=l.REVASTUDENTS. Contradiction. Example 2. where k=0. y and z can be as follows from the definition of PL w=xykz.1. Since 2n+1 > n and L is regular it must satisfy PL. for all k=0. the break up of x. should belong to L.e. That is an-l (al)k bn L. Let w that |w|=2n+1. where k=0.. Contradiction. Let w = anbn.------g Put k=0. L={aabaa. the break up of x.WWW. Put k=0. To prove that L={w|w is a palindrome on {a. where l > 0 (Because |y| > 0). Since 2n > n and L is regular it must satisfy PL. because.1. we get an-l b an L. xy contain only a¶s. hence the language is not regular 46 .

by PL xykz L | xykz | = | xz | + | yk | = (p-m) + m (p-m) = (p-m) (1+m) ----.025 . 13 .REVASTUDENTS. Example 3. To prove that L={ all strings of 1¶s whose length is prime} is not regular. i.09 ... To prove that L={ 0i2 | i is integer and i >0} is not regular.this can not be prime if p-m 2 or 1+m 2 1. m u1 } (iv) L={an | n is a perfect square } (v) L={an | n is a perfect cube } b) Apply pumping lemma to following languages and understand why we cannot complete proof 47 . L={02.016 . 04 .WWW.----} (1+m) 2 because m 1 Limiting case p=n+2 (p-m) 2 since m n Let k = p-m Proof: Let L be regular.17 . i. for all k = 0. Example 4. 2. m u0 and n<m } (ii) L={anbm | n.INFO .--Select k = 2 | xy2z | = | xyz | + | y | = n2 + Min 1 and Max n Therefore n2 < | xy2z | n2 + n n2 < | xy2z | < n2 + n + 1+n n2 < | xy2z | < (n + 1)2 adding 1 + n ( Note that less than or equal to is replaced by less than sign) Say n = 5 this implies that string can have length > 25 and < 36 which is not of the form 0i2.e.15 .----} Proof: Let L be regular. L={12.1. Exercises for students: a) Show that following languages are not regular (i) L={anbm | n. Let w = 0n2 where |w| = n2 n by PL xykz L. Let w = 1p where p is prime and | p| = n +2 Let y = m.111 .e. m u0 and n>m } (iii)L={anbmcmdn | n.

a4. 48 .-----} L2={ab.a4.a3.WWW.a5b5.L is also a regular language. L1={a. L1={a.REVASTUDENTS. The union of two regular languages is regular. then L = 7* . a4b4. 7. The inverse homomorphism of a regular language is regular Closure under Union Theorem: If L and M are regular languages. L1={ab.a6. 5.a6.a4.a5. m u0 } Closure Properties of Regular Languages 1. 4. 6. 9.----} RE=a(a)* Ex2.-----} L2={a2. 2. The concatenation of regular languages is regular. 8. a3b3.INFO (i) L={anaba | n u0 } (ii) L={anbm | n. The difference of two regular languages is regular. 3. a4b4.a2b2. a5b5----} RE=ab(ab)* Closure Under Complementation Theorem : If L is a regular language over alphabet S. The closure (star) of a regular language is regular.-----} RE=(aa)* Ex2.a3.a2. The reversal of a regular language is regular. A homomorphism (substitution of strings for symbols) of a regular language is regular. Ex1. a2 b2.-----} L1L2 = {a.a3. The intersection of two regular languages is regular. The complement of a regular language is regular. Ex1.a2.a3 b3.-----} 7* -L1={e. a3b3.-----} L1L2 = {ab. then so is L M.a5.

REVASTUDENTS. That is L(A) = (0+1)*01. A that accepts all and only the strings of 0¶s and 1¶s that end in 01.WWW. The complement of L(A) is therefore all string of 0¶s and 1¶s that do not end in 01 49 .INFO Consider a DFA.

q0. Ex1. L1={a.INFO Theorem: . Then pr represents only the initial condition. then w is in L(B) if and only if H^ ( q0. That is.a5b5.-----} L1L2 = J RE= J Ex3. q0. Consider a DFA that accepts all those strings that have a 0. in which we have seen neither 0 nor 1. Q-F).-----} L1L2 = {a2. while state ps represents the 50 . Consider a DFA that accepts all those strings that have a 1.a4. Then L = L(B). A=(Q. and vice versa. which occurs if and only if w is not in L(A).a2.If L is a regular language over alphabet 7.REVASTUDENTS. Then state qr means that we have seen only once 0¶s. L = 7* . 7.Let L =L(A) for some DFA. H.a6. This automaton accepts the intersection of the first two languages: Those languages that have both a 0 and a 1. a6b6.a6. Closure Under Intersection Theorem : If L and M are regular languages.a4. where B is the DFA (Q.-----} L2={a2. F). 7. The product of above two automata is given below.a4.----} RE=aa(aa)* Ex2 L1={ab.a6.a5.WWW. then. a4b4. H. B is exactly like A. then so is L M.a3.a3b3. w) is in Q-F. but the accepting states of A have become non-accepting states of B.a7b7-----} L2={a2 b2.L is also a regular language Proof: .

INFO condition that we have seen only 1¶s. The accepting state qs represents the condition where we have seen both 0¶s and 1¶s.REVASTUDENTS. Ex 4 (on intersection) Write a DFA to accept the intersection of L1=(a+b)*a and L2=(a+b)*b that is for L1 L2. DFA for L1 L2 = J (as no string has reached to final state (2.4)) Ex5 (on intersection) Find the DFA to accept the intersection of L1=(a+b)*ab (a+b)* and L2=(a+b)*ba (a+b)* that is for L1 L2 DFA for L1 L2 51 .WWW.

7 .REVASTUDENTS.01.1}. Where QR = Q. H.a7. Let L = {001. iff H (q.10} To prove that regular languages are closed under reversal.10. then so is L ± M. we can use . Proof. If there are more than one final states. F). M = (Q.FR) defines the language LR. so is LR Ex.a)-> q. be a language over 7={0. 7R = 7.FR=q0. H R. 7R . L={001. If L is regular we can show that LR is also regular.a4. Let FA.a3.111.-----} L2={a2. 111}.WWW. Ex.INFO Closure Under Difference Theorem : If L and M are regular languages. As L is regular it can be defined by an FA.a5.q0R.111}. MR = (QR.a6. and HR (p. LR is a language consisting of the reversals of the strings of L.01} LR={100.-----} L1-L2 = {a. q0. q0R=F. having only one final state.a3.a) -> p 52 .transitions from the final states going to a common final state.111. That is LR = {100.a5. 10. L1={a.01.a7----} RE=a(a)* Reversal Theorem : If L is a regular language.

3. The proof implies the following method 1. 2.INFO Since MR is derivable from M.REVASTUDENTS. Swap initial and final states. That is L = {ab. bab.aaab. 53 .WWW. Create a new start state p0 with transition on to all the accepting states of original DFA Example Let r=(a+b)* ab define a language L. Reverse all the transitions. LR is also regular. aab. -----}. The FA is as given below The FA for LR can be derived from FA for L by swapping initial and final states and changing the direction of each edge. It is shown in the following figure.

and h is a homomorphism on 7. 1}* Resulting : h1(L) = (01 + 11)* 01 (01 + 11)* h2(L) = (101 + 010)* 101 (101 + 010)* h3(L) = (01 + 101)* 01 (01 + 101)* Inverse Homomorphism Theorem : If h is a homomorphism from alphabet S to alphabet T. Then h-1(L) is the language of regular expression (ba)*. then h (L) is also regular.Let L be the language of regular expression (00+1)*.INFO Homomorphism A string homomorphism is a function on strings that works by substituting a particular string for each symbol. Ex. Let h be the homomorphism defined by h(a)=01 and h(b)=10.REVASTUDENTS. h applied to the string 00110 is ababccab L1= (a+b)* a (a+b)* h : {a.WWW. Ex. Theorem : If L is a regular language over alphabet 7. and L is a regular language over T. b} {0. 54 . then h-1 (L) is also a regular language. The function h defined by h(0)=ab h(1)=c is a homomorphism.

R=R1* Then L(R) is not empty. Computation of DFA takes O(n3) time where number of states of DFA can be 2n. It always includes at least I 4. DFA to NFA Conversion Conversion takes O(n) time for an n state DFA. 3. 55 3 . Testing Emptiness of Regular Languages Suppose R is regular expression. Then L(R) is empty if and only if both L(R1) and L(R2) are empty. If we convert an NFA to DFA and then convert the DFA to a regular expression it takes the time O(n34n 2n) Regular Expression to Automaton Conversion Regular expression to I-NFA takes linear time ± O(n) on a regular expression of length n. then 1. 2. R= R1R2. Computing I-Closure of n states takes O(n3) time. Therefore the bound on the running time is O(n3s) where s is the number of states the DFA actually has. Then L(R) is empty if and only if both L(R1) and L(R2) are empty. Is a particular string w in the described language? 3. R= R1R2.WWW. Automaton to Regular Expression Conversion For DFA where n is the number of states. R=(R1)* Then L(R) is not empty. Converting Among Representations Converting NFA¶s to DFA¶s Time taken for either an NFA or -NFA to DFA can be exponential in the number of states of the NFA. R = R1 + R2. Is the language described empty? 2. Do two descriptions of a language actually describe the same language? This question is often called ³equivalence´ of languages. R = R1 + R2. The running time of NFA to DFA conversion including I transition is O(n3 2n). Conversion from I-NFA to NFA takes O(n3) time. R=(R1) Then L(R) is empty if and only if L(R1) is empty since they are the same language. R=(R1) Then L(R) is empty if and only if L(R1) is empty since they are the same language. 2. then 1. It always includes at least I 4.REVASTUDENTS. conversion takes O(n34n) by substitution method and by state elimination method conversion takes O(n3) time. Then L(R) is empty if and only if either L(R1) or L(R2) is empty. Testing Emptiness of Regular Languages Suppose R is regular expression.INFO Decision Properties of Regular Languages 1. Then L(R) is empty if and only if either L(R1) or L(R2) is empty. 3.

q) with p q.q) are distinguishable. The marked pair (p. then processing of each input symbol . If the representation of L is a Regular Expression of size s. H (s.w) is accepting and the other is not accepting. if w is of length n.b) J. This implies H(1. That is (1.closure has to be computed. 56 . stop.NFA. and (p. If DFA ends in accepting state the answer is µYes¶ .a) = q. On first pass. Simulation of the above takes O(ns2) time on an input w of length n Minimization of Automata ( Method 1) Let p and q are two states in DFA.q) is already marked. (J.J).3) where J and 3 are non final states.q) for which p q.3) that (1. such that one of H^ (p. NFA has s states. else it is µno¶.3) are equivalent and can replaced by a single state A. mark each pair of which exactly one element is in F.3) are marked in the first pass according to the algorithm 1.s) if there is an a§ for which H (r. running time of this algorithm is O(ns2) If the representation is I . H(1. That is (1. Algorithm 1: List all unordered pair of states (p. we can convert to an I NFA with almost 2s states. Fig 2. a4. a2.2). On each subsequent pass.a) 2.a) J and H (3. is w in L. I . The FA is shown in Fig 1. gives the list of all unordered pairs of states (p.2) and (2. This test takes O(n) time If the representation is NFA.INFO Testing Membership in a Regular Language Given a string w and a Regular Language L. If L is represented by a DFA. in O(s) time.WWW. The boxes (1. mark any pair (r. Two states p and q are said to be distinguishable. w. «. Let L = {. beginning in start state. After a pass in which no new pairs are marked. Examples: 1. has 2 stages.REVASTUDENTS. simulate the DFA processing the string of input symbol w. if there is at least one string. (J. each of which requires O(s2) time.a) = p.} be a regular language over § = {a. a6.b}.w) and H^ (q. Our goal is to understand when p and q (p q) can be replaced by a single state. Make a sequence of passes through these pairs. a . In pass 2 no boxes are marked because. where J is a non-final state.b) J and H (3.

and 7 can be replaced by the single state 357. (Method1): Let r= (0+1)*10. 6 57 .2) is one of these. Example 2.WWW.A. This implies state 1 and 3 are equivalent and can not be divided further. 2. then L(r) = {10.2) and (1. and 4 can be replaced by a single state 124 and states 3. This gives us two states 2. and the pair (6.4) was marked on pass 1. Minimal Automata corresponding to FA in Fig 1 Minimization of Automata (Method 2) Consider set {1. (1. 5. From this we can make out that 1. since (5.00010. For example (5.2) (6. ---}.3}.010.4). The pairs marked 2 are those marked on the second pass. They are marked on pass 1.3) (J.INFO Fig 3.q) with p q The pairs marked 1 are those of which exactly one element is in F.REVASTUDENTS. The FA is given below Following fig shows all unordered pairs (p.J). The resultant minimal FA is shown in Fig.3) (2. The resultant FA is shown is Fig 3.110.

similarly it can be easily shown for the pairs (4.3) 2. 58 .INFO The transitions of fig 4 are mapped to fig 6 as shown below Example 2.7) and (2.5) (1.6) this implies that 2 and 3 belongs to different group hence they are split in level (2. (Method1): (4.REVASTUDENTS.WWW.5) and so on.

Example: The grammar Gpal for palindromes is represented by Gpal = ({P}. T the terminals. Here is one such derivation: E E*E I*E a*E a * (E) a * (E + E) a * (I + E) a * (a + E) a * (a + I) a * (a + I0) a * (a + I00) a * (a + b00) Leftmost Derivation . P 0 3.WWW.REVASTUDENTS. where V is the set of variables.S).1}. starting with the string E. P) where A represents the set of five productions 1.P. P 2. G is represented by four components that is G=(V.T.Tree 59 . P the set of productions and S the start symbol. P 1P1 Derivation using Grammar Example 1: Leftmost Derivation The inference that a * (a+b00) is in the language of variable E can be reflected in a derivation of that string.INFO Context Free Grammar Context Free grammar or CGF. A. P 0P0 5. P 1 4.{0.

Rightmost Derivation .Tree There is a rightmost derivation that uses the same replacements for each variable. although it makes the replacements in different order. Thus. This rightmost derivation is: E E * E E * (E) E * (E + E) E * (E + I) E * (E + I0) E * (E + I00) E * (E + b00) E * (I + b00) E * (a + b00) I * (a + b00) a * (a + b00) This derivation allows us to conclude E a * (a + b00) 60 . we can describe the same derivation by: E E * E E *(E) E * (E + E) E * (E + I) E * (E +I0) E * (E + I00) E * (E + b00) E * (I + b00) E * (a +b00) I * (a + b00) a * (a + b00) We can also summarize the leftmost derivation by saying E a * (a + b00).WWW.INFO Example 2: Rightmost Derivations The derivation of Example 1 was actually a leftmost derivation.REVASTUDENTS. or express several steps of the derivation by expressions such as E*E a * (E).

S) (a. then is a left ± sentential form. That is if G = (V.T.S|S Leftmost derivation S (L) (L.S) is a CFG. the middle E is replaced. As an example of a left ± sentential form. E * (I + E) is a sentential form. since there is a derivation E E * E E * (E) E * (E + E) E * (I + E) However this derivation is neither leftmost nor rightmost.a) (a. T. the derivation E E * E E * (E) E * (E + E) Shows that E * (E + E) is a right ± sentential form. with the leftmost derivation. since at the last step. Note that the language L(G) is those sentential forms that are in T*. then any string in (V T)* such that S E is a sentential form. S) is a CFG.S) (L.P. consider a * E.a) The Language of a Grammar If G(V. 61 . then is a right ± sentential form. If S E.INFO Consider the Grammar for string(a+b)*c E E+T|T T T*F|F F (E)|a|b|c Leftmost Derivation E T T*F F*F (E)*F (E+T)*F (T+T)*F (F+T)*F (a+T)*F (a+F)*F (a+b)*F (a+b)*c Rightmost derivation E T T*F T*c F*c (E)*c (E+T)*c (E+F)*c (E+b)*c (T+b)*c (F+b)*c (a+b)*c Example 2: Consider the Grammar for string (a.S) (S. is the set of terminal strings that have derivations from the start symbol. that is they consist solely of terminals. the language of G.a) Rightmost Derivation S (L) (L.S) (a. and if S E . E E*E I*E a*E Additionally.REVASTUDENTS.a) (S. For example. denoted by L(G). L(G) = {w in T | S w} Sentential Forms Derivations from the start symbol produce strings that have a special role called ³sentential forms´.WWW. P.a) S->(L)|a L->L.

T={a.REVASTUDENTS. Ex:Consider the grammar G=(V.I}.+. E (E).c.*.WWW.)}.(. Alternatively. ambiguity implies the existence of two or more left most or rightmost derivations. and productions.T. E E*E.P) with V={E.E. E E+E.b. E I. Now unambiguous grammar for the above Example: 62 .INFO Ambiguity A context ± free grammar G is said to be ambiguous if there exists some w L(G) which has at least two distinct derivation trees. I a|b|c Consider two derivation trees for a + b * c.

printf ("result is %d\n".} | Exp µ*¶ Exp {$$ = $1 * $3.INFO E T. T T*F.REVASTUDENTS. F I. I a|b|c Inherent Ambiguity A CFL L is said to be inherently ambiguous if all its grammars are ambiguous Example: Condider the Grammar for string aabbccdd S AB | C A aAb | ab B cBd | cd C aCd | aDd D->bDc | bc Parse tree for string aabbccdd Applications of Context ± Free Grammars Parsers The YACC Parser Generator Markup Languages XML and Document typr definitions The YACC Parser Generator E E+E | E*E | (E)|id %{ #include <stdio.} | Exp µ+¶ Exp {$$ = $1 + $3. $1). F (E). T F. } 63 .WWW.h> %} %token ID id %% Exp : id { $$ = $1 . E E+T.

%% int main (void) { return yyparse ( ).} | term '/' factor {$$ = $1 / $3.} . } %{ #include "y.} | '(' exp ')' {$$ = $2. return id.ID = atoi(yytext).REVASTUDENTS.} term : factor {$$ = $1. factor : number {$$ = $1.} %% {$$ = $2.} . } void yyerror (char *s) { fprintf (stderr. exp : term {$$ = $1.$3. $1). } Example 2: %{ #include <stdio. s).INFO | µ(µ Exp µ)¶ .} | exp '-' term {$$ = $1 .} [ \t \n] . } %{ 64 . "%s\n". } void yyerror (char *s) { fprintf (stderr. %% int main (void) { return yyparse ( ).' {printf ("result is %d\n". {ECHO. "%s\n". s). [+ * ( )] {return yytext[0].} .} .tab.h> %} %start line %token <a_number> number %type <a_number> exp term factor %% line : exp '. yyerror ("unexpected character").WWW.h" %} %% [0-9]+ {yylval.} | term '*' factor {$$ = $1 * $3.} | exp '+' term {$$ = $1 + $3.

WWW.REVASTUDENTS.INFO #include "y.tab.h" %} %% [0-9]+ {yylval.a_number = atoi(yytext); return number;} [ \t\n] ; [-+*/();] {return yytext[0];} . {ECHO; yyerror ("unexpected character");} %%

Markup Languages

Functions

Creating links between documents Describing the format of the document

Example

The Things I hate 1. Moldy bread 2. People who drive too slow In the fast lane HTML Source <P> The things I <EM>hate</EM>: <OL> <LI> Moldy bread <LI>People who drive too slow In the fast lane </OL> HTML Grammar Char Text Doc Element a|A|« e | Char Text e | Element Doc Text | <EM> Doc </EM>| <p> Doc | <OL> List </OL>| « List-Item <LI> Doc List e | List-Item List Start symbol

5. 6.

**XML and Document type definitions.
**

65

WWW.REVASTUDENTS.INFO

1. A E1,E2. A BC B E1 C E2 2. A E1 | E2. A E1 A E2 3. A (E1)* A BA A I B E1 4. A (E1)+ A BA A B B E1 5. A (E1)? A I A E1

EXERCISE QUESTIONS

1) Design context-free grammar for the following cases a) L={ 0n1n | nl } b) L={aibjck| ij or jk} 2) The following grammar generates the language of RE 0*1(0+1)* S A|B A 0A|I B 0B|1B|I Give leftmost and rightmost derivations of the following strings a) 00101 b) 1001 c) 00011 3) Consider the grammar S aS|aSbS|I Show that deviation for the string aab is ambiguous

4) Suppose h is the homomorphism from the alphabet {0,1,2} to the alphabet { a,b} defined by h(0) = a; h(1) = ab & h(2) = ba a) What is h(0120) ? b) What is h(21120) ? c) If L is the language L(01*2), what is h(L) ? d) If L is the language L(0+12), what is h(L) ? e) If L is the language L(a(ba)*) , what is h-1(L) ?

66

WWW.REVASTUDENTS.INFO

67

z. or some thing could be pushed into the stack and could be popped out of the stack. the context-free languages (CFL) have corresponding machines called pushdown automata (PDA). z + is the stack start symbol. t) p (Q. So. Figure 1 shows a diagrammatic representation of PDA. + is the stack alphabet. F). The Finite State Control (FSC) reads inputs. current state and the top symbol on the stack. Based on the input symbol. i. PDA is more powerful than FA.INFO Introduction to Push Down Automata Just as finite-state automata correspond to regular languages. 7 is a the input alphabet. Regular expressions are generators for regular languages and Finite Automata¶s are recognizers for them. a. FSC does some state transitions and does some operations to the stack content. and F Q is a set of final states. Stack functions as the required memory.REVASTUDENTS. a PDA is an NFA with a stack. An FA cannot recognize the language anbn. q0. Similarly for Context-free Languages. nu 0. u) 68 . H is a transition function.e. gives a new state and stack symbol. +. PDA is NFA with an added memory. for equating with number of b¶s found. Stack could be kept unchanged. Transition function: for any given state. H. because FA does not have any memory to remember the number of a¶s it has already seen. Input Tape Finite State Control stack Figure 1: PushDown Automaton Formal Definition: A nondeterministic pushdown automaton or npda is a 7-tuple M = (Q. where Q is a finite set of states. it has the form: (P. input symbol and stack symbol. one symbol at a time. has the form H: Q X (7 {}) X + p finite subsets of Q X +* q0 Q is the initial state. Context Free Grammars (CFG) are generators and Pushdown Automata (PDA) are recognizers. 7.WWW.

and stack top is a. {a. input tape symbol is b.WWW. I)} (q0. b. If current state is q0. Z0. stack top is a. a) = {(q1. and input tape symbol is a. Z0) = {(q2. and stack top is Z0.x. q0. a. If current state is q1. b. a. then stack is popped If u= wx. stack top is a. I. I)} (q1. q2}. a) = {(q0. I)} (q1. b}. So we can define PDA as M = ({q0. If current state is q0. and replace the symbol µt¶ on top of the stack with the symbol µu¶ ´. 2. input tape symbol is I and stack top is Z0. then t is replaced with x and w is pushed into the stack.INFO Basically. Z0}. {q2}). 69 . If current state is q0. Start at state q0 and keep Z0 in the stack. Example 1: Construct PDA for the language L= {anbn | a. Means ³read the symbol µa¶ from the input. If current state is q1. stay in q1 and pop the top symbol of the stack 6. then stay in q0 and push µa¶ to the stack. move to q2 the final state. 3. I)} (q.Y) = J for all other possibilities is Graphical Notation of PDA: To understand the behavior or PDA clearer. 4. q1. Z0) = {(q0. the transition diagram of PDA can be used. If u = . input tape symbol is µa¶.REVASTUDENTS. a 7. If current state is q0. t + and P and Q are states. The following transitions are possible: 1. move from state P to state Q. a) = {(q1. then move to q2 the final state. Transition diagram of PDA is generalization of transition diagram of FA. and symbol on input tape is at I. input tape symbol is b. aa)} (q0. then stack is unchanged. move to state q1 and pop the top symbol of the stack. If u = t. . Z0) = {(q2. if. and stack top Z0. I. {a. 5. where defined by following rules: (q0. aZ0)} (q0. b n u 0}. stay in q0 and push µa¶ to the stack. b.

w is the unread part of the input string. where W indicates the rest of the input string following the a. moves automaton to state q and replaces the stack top with E.a/aa 0 Start b. written as a string. Then the following move by consuming an input symbol is possible: (q1. u is the stack contents. u). yZ). w * 3. Doubly circled states are final states 4. with the leftmost symbol at the top of the stack. I. This notation says that in moving from state q1 to state q2.WWW. Arc corresponds to transitions of PDA. 0/a a. w. 1. x) = {(q2. aW. ¡ ¡ q0 b. . a.INFO 1. X) = {(p. y). where 1.}. a. W. So u * Moves of A PDA: Let the symbol "|-" indicates a move of the nPDA. X/E) from state q to state p means that an input tape head positioned at symbol a and stack top with X. 0 /I Figure 2: Transition diagram Instantaneous Description: Instantaneous Description or configuration of a PDA describes its execution status at any time.a/ I q1 I. Node corresponds to states of PDA 2. xZ) |. q is the current state of the automaton.a/ I a.(q2. The transition diagram for the above example PDA is given in Figure 2.REVASTUDENTS. an 70 ¡ 0 /I q2 ¡ .. and Z indicates the rest of the stack contents underneath the x. E)«} is an arc labeled (a. There are two types of moves possible for a PDA. Arrow labeled Start indicates start state 3. Instantaneous Description is a represented by a triplet (q. 2. If (q.. Move by consuming input symbol Suppose that H(q1.

(q1. a) = {(q0. a) = {(q1. aa)} |. aZ0)} |. an input symbol µa¶ is not consumed from the input string aW.}.(q0. a) = {(q1. So the automaton stops and the string is not accepted. aaZ0) as per transition rule (q0.(q0.(q0. a.move Suppose that H(q1. I)} |. Z0) |. b. I)} |. b. I.WWW.(q0. Then the following move without consuming an input symbol is possible: (q1. I. Z0) as per transition rule (q0. abbb. aZ0)} |.. Z0) |. aZ0) as per transition rule (q0. b. a) = {(q0. aZ0) as per transition rule (q0.There is no defined move. The above example PDA with a few example input strings. bb. aW.. aaZ0) as per transition rule (q0.(q0. aZ0) as per transition rule (q0. I. stack is empty and PDA has reached a final state.(q2.(q2. aabbb. a.(q0. aa)} |. Z0) |. a.(q0. a) = {(q1. leaving yZ on the stack. b) Moves for the input string aaabb: (q0.(q1. I)} |. This notation says that in moving from state q1 to state q2.I). a. I. b. Z0) = {(q2.(q1.I) as per transition rule (q1. aaZ0) as per transition rule (q0. c) Moves for the input string aabbb: (q0. bb. b. aa)} |.INFO input symbol µa¶ is consumed from the input string aW. a) = {(q1. b. y). I. . a) = {(q1. a. Z0) = {(q0. abb. So the automaton stops and the string is not accepted. I)} |. aZ0) as per transition rule (q0. and the symbol µx¶ at the top (left) of the stack xZ is replaced with symbol µy¶. a. I)} |.(q1. aabb. The input tape is empty. bb. the moves are given below: a) Moves for the input string aabb: (q0. I. b. aZ0) as per transition rule (q0.There is no defined move.(q1. aaZ0) as per transition rule (q0. 2. bbb.REVASTUDENTS.(q1. a) = {(q1. I)} PDA reached a configuration of (q2. aZ0) as per transition rule (q0. I. yZ). Z0) as per transition rule (q0. and the symbol µx¶ at the top (left) of the stack xZ is 71 . aW. Z0) = {(q0. Z0) = {(q0. aaabb. a) = {(q0. aaaZ0) as per transition rule (q0. aa)} |. xZ) |. a) = {(q0. aZ0)} |. abb. So the string is accepted. I)} |. a. b. b. aabb. x) = {(q2.

REVASTUDENTS.Z) |. Z) |.Z}.(c. I)} H (c.(d. Z0) |-* (q2.1. 0)} H (c. This move is usually used to represent non-determinism. I.0Z) |. 0) ={(c. Figure 3: Transition diagram M = ({a.(c. so PDA stops 72 . {0. In this move. 00)} H (b.(c. 0Z) |. 0. 110. 0Z) |. 00Z) |(c. where H is given by: H (a. 010110. Z) ={(d. Moves for the input string 0010110 is given by: (a.1}. I)} H (b.10110.(b. 0.1. I. 10. Example 2: Design a PDA to accept the set of all strings of 0¶s and 1¶s such that no prefix has more 1¶s than 0¶s. Solution: The transition diagram could be given as figure 3. I. 0Z)} H (b. aabb.I). {d}). Moves for 011 (a. 0110. 1. 0. {0. PDA stops.(b. 00Z) |. 0)} H (b. a. 0) ={(d. Z)} For all other moves.used to represent zero or more moves of PDA. 0) ={(b. 1.INFO replaced with symbol µy¶. 0. 0Z)} H (c. leaving yZ on the stack. So the input string is accepted by the PDA. The relation |-* is the reflexive-transitive closure of |.011. For the above example. Z) ={(b.11. 0Z) |.Z) |-no move. 0) ={(c.(b. I. Z) ={(d. 0010110.(b. 0) ={(b.WWW. Z) ={(b. Z. 00)} H (c. the tape head position will not move forward. 0) ={(d. I. Z) |. H. 0Z) |. (q0. d}. 0Z). I. Z)} H (c. 0.(b. I. c. b.

(consider any type of parentheses) 73 . Show the moves for abbaba 3. n u 0} 4. b . For the language L = {anb2n | a. Accepting the language of balanced parentheses. For the language L = {wcwR | w {a. b} with equal number of a¶s and b¶s. Accepting the set of all strings over {a. c } 2.WWW.INFO Exercises: Construct PDA: 1.REVASTUDENTS. b}*.

0) = {(q0. PDA enters a final state. Accept by Final state: Let P = (Q. I. Z0. 0Z0)} (q0. I)} (q1. M = ({q0. the difficulty is how to decide the middle of the input string? The 3rd 1 can be part of w or can be part of wR.INFO Push Down Automata Languages of PDA 1. 0. Z0)} (q0. 0. 0) = {(q1. I)} (q1. {0. w. 101101. Accept by empty stack After consuming the input. 1) = {(q0. Acceptable input strings are like 00. 1. 0. I. 0) = {(q1. the language for even length palindrome. I. In the string 0110. The content of the stack is irrelevant. . Languages of PDA There are 2 ways of accepting an input string PDA a. E)}. . 10)} (q0.REVASTUDENTS. For example the language {L = anbm | n u m}. Z0) = {(q2. Accept by Final state After consuming the input. 1Z0)} (q0. q2). where is defined by: (q0. 1. Z0. q0. where q F and E * Example: L = {wwR | w is in (0 + 1)*}. q1. The current state could be final or non-final state. I. Usually the languages that a PDA accept by final state and PDA by empty stack are different. 01)} (q0. The PDA could be constructed as below. 1) = {(q1. {0. and 110011. . 00)} (q0. Z0) = {(q0. Both methods are equivalent. Z0) = {(q0. the appropriate PDA could be by final state. 1}. stack of the PDA will be empty. I. After consuming the input. 1. 1) = {(q1. 1111. Z0)} 74 . 1. 0)} (q0. 1. q0. 11)} (q0. Z0) = {(q1. .WWW. q2}. the language accepted by P by the final state is {w | (q0. 0) = {(q0. F) be a PDA. It is possible to covert a PDA accept by final state to another PDA accept by empty stack and also the vice versa. 1) = {(q0. b. 0110. the stack may not be empty. 1)} (q1. 0. Z0) |-* (q.Z0}. Then L(P).

REVASTUDENTS. . Z0/0Z0 1. Z0 / Z0 q2 Figure 1: Transition Diagram for L = {wwR} The moves of the PDA for the input string 101101 are given figure 2. Z0). 0. wRZ0) |. Z0/ Z0 q0 q1 I. Z0) |-* (q0.0/ I 1. I. q0. wRZ0) |-* (q1. . wR. Z0) |-* (q.(q2. I. Z0/1Z0 0.1/ 01 1. Z0) |.0/0 I.1/1 I.WWW.0/ 10 0.INFO (q0. where q Q Example: 75 .(q1. We define the language accepted by empty stack by N(P) = {w | (q0.0/00 1.1/ I I. wwR.1/11 0. I)}. . Figure 2: moves of PDA for string 101101 Accept by empty stack: Let PDA P = (Q. I. w. wR. Z0) The transition diagram for the PDA is given in figure 1.

Z0/Z0 1.Z0/Z0 1. 0/I 1. same for both PN and PF. Z0/Z0 I. X0/I (add this transition from all states of PN to new state Pf) Pf Figure 4: PF simulates PN The method of conversion is given in figure 4. Z0) = {(q2. then there is a PDA PF PN q0 I. F. y) = N(q. {Pf}). 0/I 1.Z0/I r j Figure 3: transition diagram of 0i1 | 0 e i e j 2. X0) = {(Pf. I)} to get accept by empty stack. a. Let PF = (Q{p0. Z0/Z0 q I.REVASTUDENTS. Z0/Z0 s p I. F(q. . This example also shows that L(P) = N(P) Example: j Construct PDA to accept by empty stack for the language L={0i1 | 0 e i e j} The transition diagram for the PDA is given in Figure 3. I. y) a or a = I and y . We use a new symbol X0. From Empty Stack to Final State: Theorem: If L = N(PN) for some PDA PN= (Q. {X0}. I)} to accept the string by moving to final state. Also add a new start state p0 and final state pf for PF. I. p0. a. pf}. X0. Z0). . 0. q0.WWW.INFO Construct PDA to accept by empty stack for the language L = {wwR | w is in (0 + 1)*} Instead of the transition (q1. N. which must be not symbol of to denote the stack start symbol for PF. such that L = L(PF) Proof: p0 I. Z0)} give (q1. I. where F is defined by F(p0. Z0 X0)} to push X0 to the bottom of the stack F(q. Z0) = {(q2. The set of accepting states are irrelevant. 76 . X0) = {(q0. Conversion between the two forms: a. X0/Z0X0 . Z0/0Z0 1. 0/00 0. I.

X0) |. 0. I.Z}. 1}.(p. 1. {0. I)}. .REVASTUDENTS. The moves of PN to accept a string w can be written like: (p0. I. I ) Example: Construct PDA to accept by final state the language of all strings of 0¶s and 1¶s such that number of 1¶s is less than number of 0¶s. same for both or y = X0 . X0) |-PF (p0. where is given by: (q0. y or y = X0. w. Let PN = (Q{p0. X0). . p0. I. I ) b. I. F. {X0}. initially change the stack start content from Z0 to Z0X0. where N is defined by: N(p0. I)}. w. Z0X0) |-*PF (q. N. y) = F(q . p}. 0Z)} (q0. .(Pf . w. q1}. X0) |. I. +/I I. to pop the remaining stack contents. 1Z)} 77 ¢ p I. I. To avoid PF accidentally empting its stack. a. same for both N(q .INFO The moves of PF to accept a string w can be written like: (p0. a. Z. Z0X0) |-*PN (q. Solution: PDA by final state is given byM = ({q0. Z) = {(q0. y) = {(p . Z0X0)} to change the stack content initially N(q . X0/Z0X0 PF q0 I. y). w. q F. X0) = {(q0. {q1}). +/I . X0) |-PN (q0.WWW. then there is a PDA PN such that L = N(PN) Proof: p0 I. Also convert the PDA to accept by empty stack. F). +/I Figure 5: PN simulates PF The method of conversion is given in figure 5.. 1. {0. q0. Z0. Also add a new start state p0 and final state p for PN. . Z) = {(q0. a or a = I and y . From Final State to Empty Stack: Theorem: If L = L(PF) for some PDA PF= (Q. y) = {(p. y N(p . I. q0.

Z) = {(q2. {aibjck | i.0) = {(q1. j u 1} 78 . I. 1.INFO (q0.0) = {(q0.REVASTUDENTS. I)} Exercises: Design nPDA to accept the language: 1. I)} (q1. I)} (q0.0) = {(q2. 1) = {(q0. I.0) = {(q2. PDA by empty stack is given by M = ({q0. I. I. Z). I)} (q0. {aibi+jcj | i u 0. I. q2}. q1. PDA stops. Z)} (q0. q0. 00)} (q0.1) = {(q0. j u 0} 3. 0. k u 0 and i = j or i = k} 2. 0) = {(q0. 11)} (q0. {aibjci+j | i. I)} (q2.WWW.Z) = {(q2. j. 1}. where ¶ is the union of and the transitions given below: (q1. I)} (q2. 0. {0. 1. Z}.Z) = {(q1. {0. I. 0)} For all other moves. 1. ¶.

we construct a PDA P that simulates the leftmost derivations of G. S) where is defined by: 1. Q. a. each one corresponding to terminals of G. M will have AE on its stack after reading u. For every intermediate sentential form uAE in the leftmost derivation of w (initially w = uv for some v). . The representation is shown in figure 1. CFG PDA by empty stack PDA by Final state Figure 1: Equivalence of PDA and CFG From CFG to PDA: Given a CFG G. (q. q. Let G = (V. At the end (case u = w) the stack will be empty. There is only one state in the new PDA. For each terminal a. First construct CFG. I. one for each production.REVASTUDENTS. The PDA which accepts L(G) by empty stack is given by: P = ({q}.WWW. For each variable A include a transition. I)} CFG to PDA conversion is another way of constructing PDA. Most transitions are on I.INFO Push Down Automata Equivalence of PDA and CFG I. a) = {(q. include a transition (q. T. and then convert CFG to PDA. Equivalence of PDA and CFG The aim is to prove that the following three classes of languages are same: 1. Example: Convert the grammar with following production to PDA accepted by empty stack: 79 . Language accepted by PDA by empty stack It is possible to convert between any 3 classes. b) | A p b is a production of Q} 2. A) = {(q. New transitions are added. The stack symbols of the new PDA contain all the terminal and non-terminals of the CFG. Language accepted by PDA by final state 3. T. all the rest of the information is encoded in the stack. S) be a CFG. V T. Context Free Language defined by CFG 2.

. 1}. 1. (q. XZ)}. . Z) = {(q. (q. 0S1). . 1A0).INFO S p 0S1 | A A p 1A0 | S | I Solution: P = ({q}. {X. S) 80 .X) = {(r. I)} Solution: Equivalent productions are: S p [qZq] [qZq] p i[qXq][qZq] [qXq] p e [qZq] p I If [qZq] is renamed to A and [qXq] is renamed to B. I)} (q. where V = {S. (This does not accept if ±if ±else-else statements). {i. I)} and (q. S). Let (q. G has productions S p [q0Z0 p] 2. I. I)} From PDA to CFG: Let P = (Q. [pXq]}. G has productions [qXrk] p a[rY1r1] [r1Y2r2] « [rk-1Ykrk] If k = 0 then [qXr] pa Example: Construct PDA to accept if-else of a C program and convert it to CFG. 0) = {(q. I)} (q. Let the PDA P = ({q}. q. productions of R consists of 1. S}. Y1Y2«Yk)} where a or a = I. . S). S).a. 0. A. k can be 0 or any number and r1r2 «rk are list of states. Bp e}. q. ApiBA | I. where is given by: (q. (q.WWW. A) = {(q. R. {SpA. where is given by: (q. e}. e}. A. 1. {i. Z) = {(q. I. where p. q Q and X . then the CFG can be defined by: G = ({S. B}. A)} (q. Z). Z0) be a PDA. (q. . For all states p. X) = {(q. {0. I. i.REVASTUDENTS.Z}. An equivalent CFG is G = (V. e. q0. 1) = {(q. {0. . S) = {(q.

1. 0. . X) = {(p. X)} (p. I)} (p. {X. X) = {(q. X)= {(q. XZ)} [qZq] p 1[qXq][qZq] [qZq] p 1[qXp][pZq] [qZp] p 1[qXq][qZp] [qZp] p 1[qXp][pZp] For (q. the equivalent CFG can be defined by: 81 . X)} [qXq] p 0[pXq] [qXp] p 0[pXp] For (p. XZ)} (q. [pZq] to C. 0.q}. I. X) = {(q. 1. [pZp] to D. Z)= {(q. 1. {0. X) = {(p. X) = {(q.WWW. Z) = {(q. X) = {(p. 1. Z)} Solution: Add productions for start variable S p [qZq] | [qZp] For (q. 1. 0.INFO Example: Convert PDA to CFG. X) = {(p. Transition function is defined by: (q. [pXp] to G and [pXq] to H. q. Z) = {(q. I)} (q. Z)} [pZq] p 0[qZq] [pZp] p 0[qZp] Renaming the variables [qZq] to A. XX)} (q. [qXq] to E [qXp] to F. I)} [pXp] p 1 For (p. I. 0.Z}. PDA is given by P = ({p. Z) = {(q.REVASTUDENTS. I)} [qXq] p I For (q. [qZp] to B. XX)} [qXq] p 1[qXq][qXq] [qXq] p 1[qXp][pXq] [qXp] p 1[qXq][qXp] [qXp] p 1[qXp][pXp] For (q.1}. Z)). 1.

(q.X) must be empty DPDA is less powerful than nPDA.If (q. X) = {(p. A p aAA.0/00 1. S p SS | (S) | I 3. Deterministic PDA¶s (DPDA) are very useful for use in programming languages. The transition diagram for the DPDA is given in figure 2. Z) = {(q. I)} (p. 0. F) is deterministic if and only if. A. S). Convert to PDA. A p bBB | aS | a. I. Definition: A PDA P= (Q. I. Z0/0Z0 1. X) = {(p. . XX)} (p. The class of language DPDA accept is in between than of Regular language and CFL. X)} (q. but not by DPDA. E. 0. G. X) = {(q. H}.0/0 c.1/ 01 82 c. S p aAS | bAB | aB.WWW. 0. X) = {(q. Z0/1Z0 0. Convert to CFG. B. a or a= I and X 2.0/ I 1. I)} (p. I)} II. {0.1/1 c. 1.1}. Example: Construct DPDA which accepts the language L = {wcwR | w {a. XZ)} (q. 1. b}*. X) = {(p.a. XX)} (q. Deterministic PDA: NPDA provides non-determinism to PDA. CFG with productions: 1. R. Z) = {(p. B p bA | a b.REVASTUDENTS.a. I. . 1. c }. D. . NPDA can be constructed for accepting language of palindromes. A p aS | bS | a 2. Z / Z . The Context Free Languages could be recognized by nPDA. C. q0.1/11 0.X) has at most one member for qQ. F. The productions of R also are to be renamed accordingly. PDA with transition function: (q.X) is not empty for some a . Z0. 1. Exercises: a. For example Parsers used in YACC are DPDA¶s.INFO G = ({S.1/ I 0. then (q.

But all unambiguous grammars are not accepted by DPDA.WWW. (Consider any type of parentheses) 2. Non-Regular language L=WcWR could be accepted by DPDA with empty stack. If L = N(P) for DPDA P. because if you take any x. N(P) L DPDA and Ambiguous grammar: DPDA is very important to design of programming languages because languages DPDA accept are unambiguous grammars.REVASTUDENTS. The two modes of acceptance are not same for DPDA. then P(q. but not with empty stack. or vice versa. q Q such that A(q. But the language. Accepting {0n1m| n u m} DPDA and Regular Languages: The class of languages DPDA accepts is in between regular languages and CFLs. L=L(P) for some DPDA P. The stack is inactive always. 83 . A language L = N(P) for some DPDA P if and only if L has prefix property. because strings of this language do not satisfy the prefix property.a. Accepting strings with number of a¶s is more than number of b¶s 3. then x should not be a prefix of y. So N(P) are properly included in CFL L. This is language is accepted by only nPDA. L={0*} could be accepted by DPDA with final state. To accept with empty stack: Every regular language is not N(P) for some DPDA P.a)=p. x and y satisfy the prefix property. PDA surely includes a stack. Accepting the language of balanced parentheses. For example S p 0S0|1S1| I is an unambiguous grammar corresponds to the language of palindromes. ie.Z)={(p. but the DPDA used to simulate a regular language does not use the stack. then surely L has unambiguous CFG. To accept with final state: If L is a regular language. y L.INFO Exercises: Construct DPDA for the following: 1. If A is the FA for accepting the language L.Z)} for all p. The DPDA languages include all regular languages. y L(WcWR). Definition of prefix property of L states that if x.

then L has unambiguous CFG. Then convert N(P) to CFG G¶.WWW. 84 .REVASTUDENTS. From G¶ we have to construct G to accept L by getting rid of $ . To convert L(P) to N(P) to have prefix property by adding an end marker $ to strings of L.INFO If L = L(P) for DPDA P.So add a new production $pI as a variable of G.

If X is not useful. Eliminating non generating symbols 2. w+*. then G1=(V1. T1. P. S) be a grammar without useless symbols by 1. T. P. 2. ie if S * EXF Theorem: Let G = (V. P. then it is useless. P1. S. S) be a CFG and assume that L(G) { J. T. 1. X is generating variable. Useless symbols Those variables or terminals that do not appear in any derivation of a terminal string starting from Start variable. A CFG can be simplified by eliminating 1. T. Here A is a useless symbol SpA.INFO Simplification of CFG The goal is to take an arbitrary Context Free Grammar G = (V.Productions A p I. S) if there is S * EXF * w. Eliminating symbols that are non reachable 85 . A and B are variables 1.WWW. ie if X * w. Unit production A p B. Omitting useless symbols from a grammar does not change the language generated Example: S p aSb | I | A. S) and perform transformations on the grammar that preserve the language generated by the grammar but reach a specific format for the productions. B p bB. Here B is a useless symbol Symbol X is useful if both the conditions given below are satisfied in that order itself. where w+* 2. ApaA | I. A p aA.REVASTUDENTS. where A is a variable 3. X is reachable. I . Eliminate useless symbols: Definition: Symbol X is useful for a grammar G = (V.

A}. S p AB | a. A p a. S) Example: Eliminate the non-generating symbols from S p aS | A | C.WWW. then A is generating Non-generating symbols = V. If the order of eliminations is 1 followed by 2 gives S p a. {S p a. A pa}.generating symbols.REVASTUDENTS. B}. A pa}. Eliminate non-generating symbols: Generating symbols follow to one of the categories below: 1. we get G1= ({S. C p aCb Solution: C is a non-generating symbol. After eliminating C gets. A. Example: G= ({S. B p aa. the dependency graph is given below. A pa. Here A is still useless symbol. {a}. If A p E and E is already generating. C D 86 . B p aa Eliminate symbols that are non reachable: Dependency Graph: For the production C p xDy.INFO Elimination has to be performed only in the order of 1 followed by 2. S) Solution: B is a non-generating symbol. so completely useless symbols are not eliminated. Example: Consider a grammar with productions: S p AB|a. After eliminating B. A pa. Every symbol of T is generating 2. Otherwise the grammar produced will not be completely useless symbols eliminated. A pa. S p aS | A. {a}. If the order of eliminations is 2 followed by 1 gives S p a.

then X is non reachable. S) Solution: S A Draw the dependency graph as given above. C p b} 87 . B p aa S A B Draw the dependency graph as given above. B is non-reachable from S. A pa.REVASTUDENTS. {a}. S) Example: Eliminate non-reachable symbols from S p aS | A. {a}. Example: Eliminate non-reachable symbols from G= ({S. A pa}. All Variables are reachable. we get the grammar with productions S p aS | A. C. V2 = {A. S} P2 = {S p CA. {S p a}. If there is no edge reaching a variable X from Start symbol S. C p b} Step 2: Eliminate symbols that are non reachable S A C Draw the dependency graph as given above. S} P1 = {S p CA. B pBC | AB. A p a Example: Eliminate useless symbols from the grammar with productions S p AB | CA. A is non-reachable from S. C p AB | b Step 1: Eliminate non-generating symbols V1 = {A. G1= ({S}. After eliminating B.INFO Draw dependency graph for all productions. C. A pa. So the final variables and productions are same V1 and P1. A pa. A p a. A}.WWW. After eliminating A. {S p a.

E p aC} 2. B p bcc. Example for a grammar G with I .Productions: Most theorems and methods about grammars G assume L(G) does not contain I. A p bBB.WWW. Remove I-productions and duplicates Step 1: Find set of nullable variables Nullable variables: Variables that can be replaced by null (I). B p bB | I. Eliminate I . P= {S p aS | AB.P= {S p aAa. B p ab. P= {S p aBa | BC. In the grammar with productions S p ABA. A p aA | I. B p bB | I The procedure to find out an equivalent G with out I-productions 1. S is nullable because both A and B are nullable. then we have to find out an equivalent G without I-productions. So if I is not there in L(G). A is nullable because of the production A p I. 3.productions is S p ABA. C p aB} 4. C pabb. A p aC | BCC. E pd} 3. A p aA | I. A pSb | bCC. D p E. A p bA. P= {S p aAa. C pa. If A * I then A is a nullable variable. Find nullable variables 2. B is nullable because of the production B p I. Add productions with nullable variables removed.INFO Exercises: Eliminate useless symbols from the grammar 1.REVASTUDENTS. 88 . B p AA} III.

B p bB | I. D pb}. in Ni-1} until Ni = Ni-1 Step 2: Add productions with nullable variables removed For each production of the form A p w. where w¶ is obtained from w by removing one or more occurrences of nullable variables. Bp I. A p aA | I. Example: In the grammar with productions S p ABA. {S p aS | AB. A.INFO Algorithm to find nullable variables is given below: V: set of variables N0 n{A | A in V. production A p I} Repeat Ni n Ni-1U {A| A in V. A p .REVASTUDENTS. D}. S) 89 . create all possible productions of the form A p w¶. A. A p I.Productions G = ({S. B and S are nullable variables. So after removing nullable variables we get the productions S p ABA | BA | AA | AB | A | B | I A p aA | I | a B p bB | I | b Step 3: Remove I-productions and duplicates The desired grammar consists of the original productions together with the productions constructed in step 2. {a}. minus any productions of the form A p I.WWW. B. Example: For the above example we get the final grammar with productions S p ABA | BA | AA | AB | A | B A p aA | a B p bB | b Example: Find out the grammar without I .

WWW. 2. B. W pZ.productions and useless symbols from the grammar S pa |aA|B|C. Y p b | X S p Xa. A paB. {a}. X p aX | bX | I S p XY. D pddd Step 1: Eliminate I . {S p aS | a | AB | A | B. X p Y| I. Eliminate useless symbols from G1 and obtain G2 Example: Eliminate I . Y p bW. A.productions from the grammar 1.REVASTUDENTS.INFO Solution: Nullable variables = {S. A. 4. D p ddd Step 2: Eliminating useless symbols Step 2a: Eliminate non-generating symbols Generating ={D. B p Ba | Bb| I S p ASB | I. A p aA | bB | I. S} 90 . Eliminate I .productions and useless symbols.productions and obtain G1 2. D pb}. C paCD. S) Exercise: Eliminate I . X pZb. Z pAB. S p a |Xb | aYa. B} New Set of productions: S p aS | a S p AB | A | B Dpb G1= ({S. A p aAS | a.productions Nullable ={A} P1={S pa |aA | B | C. B. B paA. B p SbS | A| bb But if you have to get a grammar without I . 3. C p aCD. A paB| I. D}. follow the sequence given below: 1. B paA|a.

B p aA|a. eliminating C gets P3= S pa |aA|B. B paA|a} 91 .WWW. A p aB. D p ddd} Step 2b: Eliminate non -reachable symbols S A B D C is non-reachable.INFO P2={S pa | aA| B. A p aB.REVASTUDENTS.

Sp ABA | BA | AA | AB | A | B | aA | a | bB | b A p aA | a B p bB | b Remove unit productions from above productions to get Sp ABA | BA | AA | AB | aA | a | bB | b A p aA | a B p bB | b Example: Eliminate unit productions from S p Aa | B. where B p E is a non-unit production in P. Unit productions could complicate certain proofs and they also introduce extra steps into derivations that technically need not be there. For each unit production A p B. S pB. So A and B are derivable. add to P1 all productions A p E.WWW. B pA | bb Solution: 92 .REVASTUDENTS. Delete all the unit productions Example: Eliminate unit productions from S p ABA | BA | AA | AB | A | B. Now add productions from derivable. A p a | bc | B. Definition: Eliminate unit productions: Unit production is of form A p B. Add all non unit productions of P to P1 2. A p aA | a.INFO I. 3. where A and B are variables. The algorithm for eliminating unit productions from the set of production P is given below: 1. B p bB|b Solution: The unit productions are S p A.

Eliminate I . B p aA.WWW. follow the sequence given below: 1. Eliminate useless symbols from G2and obtain G3 Example: Eliminate useless symbols. I -productions and unit productions from the grammar with productions: S p a | aA | B| C. A p aB | I. useless symbols and unit productions. C p cCD. D pddd} Step 3: Eliminate useless symbols After eliminate non-generating symbol C we get P3 = {S p a | aA. Add productions from derivable and eliminate unit productions to get. S p Aa | bb | a | bc A p a | bc | bb B p bb | a | bc Simplified Grammar: If you have to get a grammar without I . C p cCD.INFO Unit productions are S p B. D p ddd Step 1: Eliminate I -productions Nullable = {A} P1 = {S p a | aA | B | C. Eliminate unit productions from G1 and obtain G2 3. A p aB. P2 = {S p a | aA | cCD. D p ddd} After eliminate symbols that are non reachable S A B D 93 . A p aB.productions. A p B and B p A. B p aA | a. B p aA | a. C p cCD. D p ddd} Step 2: Eliminate unit productions Unit productions are S p B and Sp C.REVASTUDENTS. So Derivable variables are B and C.productions from G and obtain G1 2. A p aB. B p aA | a. So A and B are derivable.

A. A p aB. Eliminate useless symbols. Replace Xi in A production of P by Cai c) Consider A p X1X2«Xn. has a grammar in CNF with productions of the form: 1. where A V and a T Algorithm to produce a grammar in CNF: 1. C1 p X2C2. Chomsky Normal Form (CNF) Every nonempty CFL without I. A p aB. then add a new variable Cai to V1 and a new production Cai p ai to P1. Cn-2 pXn-1Xn to P1 and C1. C2. where A. A p a. C V 2. B p aA | a} So the equivalent grammar G1 = ({S. {S p a | aA.REVASTUDENTS. B. Cn-2 to V1 Example: Convert to CNF: S p aAD. {a}. B p aA | a}. S) II. If Xi is a terminal say ai. where n u3 and all Xiµs are variables. B pb. B}. Step2a: Elimination of terminals on RHS Change S p aAD to S p CaAD. D pd Solution: Step1: Simplify the grammar Grammar is already simplified. Elimination of terminals on RHS of a production a) Add all productions of the form A p BC or A p a to P1 b) Consider a production A p X1X2«Xn with some terminals of RHS. Cbp b 94 . « . I -productions and unit productions from the grammar 2. Ca p a A paB to A p CaB A p bAB to A p CbAB. A paB | bAB.WWW. A p BC. Introduce new productions A p X1C1.INFO P4 = {S p a | aA. « .

C1 pAD. B p AC S p 0A0 |1B1 | BB. C2}. Capa. B p SC3 | SCb | CbS | b | CbCb | CaC2 | CaA | a. C p S| I 95 . C2p AB Grammar converted to CNF is given below: G1=({S. C2pAB}. B pSbS | A | bb} Solution: Step1: Simplify the grammar Step 1a: Eliminate I -productions: Consists of S p I Eliminating I -productions from P to get: P1={S pASB|AB. C2 p AS. A. 2. D. 4. A p aab. C3 p CbS} Exercises: Convert the following grammar with productions to CNF: 1. Bp SbS | Sb | bS | b | A | bb} Step 1b: Eliminate unit productions: Bp A P2= {S p ASB|AB. C1 p SB. 3.WWW. S p aSa | bSb | a | b | aa | bb S p bA | aB. Cb. A p aAS | aA | a. b}. A p CaB| CbC2. Ca. Cb p b. B p S|A. Cap a. A p aAS | a. A paAS|aA|a. C1. A p bAA | aS | a. B pSbS | Sb | bS | b | bb | aAS | aA | a} Step 1c: Eliminate useless symbols: no useless symbols Step2: Convert to CNF P3={S p AC1 | AB. A p C. Cbpb. {a. B. B p aBB | bS | b Sp Aba. A p CaC2 | CaA | a. C1 p AD A p CbAB to A p CbC2. S) Example: Convert the grammar with following productions to CNF: P={S p ASB | I. S p CaC1.INFO Step2b: Reduce RHS with 2 variables Change S p CaAD to S p CaC1.REVASTUDENTS.

Conversion to GNF is a complex process. S p aAa | bBb| I.REVASTUDENTS. The remainder of the computation uses the input symbol and the stack top to determine the appropriate transition.WWW. A p 1 Usage of GNF: Construction of PDA from a GNF grammar can be made more meaningful with GNF. pushes the variables A1A2«An on the stack and enters q1. C p CDE |I. Converting CFG in GNF to PDA gets a PDA without I-rules. An S rule of the form S p aA1A2«An generates a transition that processes the terminal a. B p C | b. A p C|a. 96 . Example: Transform into Greibach normal form the grammar with productions S p 0S1 | 01 Solution: S p 0SA | 0A. where a T and E is a string of zero or more variables.INFO 5. Assume that the PDA has two states: start state q0 and accepting state q1. Greibach Normal Form (GNF): Every nonempty language without I is L(G) for some grammar G with productions are of the form A p aE. D p A | B | ab III.

as often as we like. consider a CFL L={anbn | n u 1}. 2 symbols at 1. The extended parse tree for the string a4b4 is given in figure 2.INFO The Pumping Lemma for CFL The pumping lemma for regular languages states that every sufficiently long string in a regular language contains a short sub-string that can be pumped.WWW. The parse tree for the string a4b4 is given in figure 1. That is. Equivalent CNF grammar is having productions S p AC | AB. For example. 1 symbols at level 0. 4 symbols at 2 «2i symbols at level i. A p a. Number of symbols at each level is at most twice of previous level.REVASTUDENTS. Then there exists a constant ku 0 such that if z is any string in L such that |z| u k. C p SB. then we can write z = uvwxy such that 97 . Figure 1: Parse tree for a4b4 Figure 2: Extended Parse tree for a4b4 Extend the tree by duplicating the terminals generated at each level on all lower levels. Pumping Lemma Theorem: Let L be a CFL. The pumping lemma for CFL¶s states that there are always two short sub-strings close together that can be repeated. both the same number of times. inserting as many copies of the sub-string as we like always yields a string in the regular language. Both leftmost derivation and rightmost derivation have same parse tree because the grammar is unambiguous. B p b. tree must be having at least depth of n and level of at least n+1. To have 2n symbols at bottom level.

These parse trees are shown in figure 4. vx { I (since v and x are the pieces to be ³pumped´. 3.trees Let T be the subtree rooted at upper occurrence of S and t be subtree rooted at lower occurrence of S. consider the first pair of same variable along the path. Reading from bottom to top. v=a. Say X has 2 occurrences. and the parse tree is shown in figure 3. at least one of the strings we pump must not be empty). For all i u 0. Example parse tree: For the above example S has repeated occurrences. x=b. Figure 3: Parse tree for a4b4 with repeated occurrences of S circled. w=ab. The longest path in the parse tree is at least n+1. some variables occur more than once along the path.WWW. cut out t and replace it with copy of T. So here u=aa.REVASTUDENTS. Figure 4: sub. |vwx| e k (that is. By pigeonhole principle. uviwxiy is in L. Proof: The parse tree for a grammar G in CNF will be a binary tree. To get uv2wx2y L. w = ab is the string generated by lower occurrence of S and vwx = aabb is the string generated by upper occurrence of S. so this path must contain at least n+1 occurrences of the variables. Let k = 2n+1.INFO 1. the middle portion is not too long). Any parse tree for z must be of depth at least n+1. where n is the number of variables of G. Break z into uvwxy such that w is the string of terminals generated at the lower occurrence of X and vwx is the string generated by upper occurrence of X. 2. The parse tree for uv2wx2y L is given in figure 5. Suppose z L(G) and |z| u k. y=bb. Cutting out t 98 .

Some b¶s and some c¶s Case 1: vwx consists of all a¶s 99 . assume L is context free. To show that a language L is not a CFL. for some k The above contradicts the Pumping Lemma Our assumption that L is context free is wrong Example: Show that L = {aibici | i u1} is not CFL Solution: Assume L is CFL. Some a¶s and some b¶s 3. 6. Pumping Lemma game: 1. Choose an ³appropriate´ string z in L Express z = uvwxy following rules of pumping lemma Show that uvkwxky is not in L. 2. 4. Figure 5: Parse tree for uv2wx2y L Figure 6: Parse tree for uwy L To get uwy L.REVASTUDENTS. 5. Since |vwx| e n then vwx can either consists of 1. All a¶s or all b¶s or all c¶s 2.INFO and replacing it with copy of T as many times to get a valid parse tree for uviwxiy for i u 1. Choose an appropriate z = anbncn = uvwxy. 3. cut T out of the original tree and replace it with t to get a parse tree of uv0wx0y = uwy as shown in figure 6.WWW.

b¶s and c¶s. uwy L. x = b. then uv2wx2y will be a3b3c2 {L Case 3: vwx consists of some b¶s and some c¶s If z = a2b2c2 and u = a2b. where t is repeating. in the pumping lemma satisfying the condition uviwxiy for i u0. Example: z = 03130313. y = bc2. Example: Show that L = {ww |w {0. then |t| =2n-k/2. vx = 02 then uwy = tt = 0130313. z is having a length of 4n. Then uwy begins with 0n-k1n |uwy| = 4n-k.INFO If z = a2b2c2 and u = I. So uwy is not in L and L is not context free. uv2wx2y will not be having an equal number of a¶s. y = c. Suppose vwx is within first n 0¶s: let vx consists of k 0¶s. Then uwy will be some string in the form of tt. v = b. 100 . t= Suppose vwx consists of 1st block of 0¶s and first block of 1¶s: vx consists of only 0¶s if x= I. n |t| u 3n/2. satisfying the conditions |vwx| e n and vx {I. then |t| is at least 3n/2 and first t ends with a 0. If uwy is some repeating string tt. But first t and second t are not the same string. then uwy is not in the form tt. x = I. Pick any z = 0n1n0n1n = uvwxy. v = a. w = I. 1}*} is not CFL Solution: Assume L is CFL. So if |vwx| e n. Can¶t contradict the pumping lemma! Our original assumption must be wrong. w = I. v = a. where n is pumping lemma constant.REVASTUDENTS. But Pumping Lemma says uv2wx2y L.n u 0}. uv2wx2y will be a4b2c2{L Case 2: vwx consists of some a¶s and some b¶s If z = a2b2c2 and u = a. If vx has at least one 1. This language we prove by taking the case of i = 0. w = c. It is sufficient to show that L1= {0m1n0m1n | m. t does end in 0 but tt ends with 1.WWW. In all cases uwy is expected to be in the form of tt. So L is not context-free. According to pumping lemma. so first t = 0130 and second 0213. Very similar explanations could be given for the cases of vwx consists of first block of 1¶s and vwx consists of 1st block of 1¶s and 2nd block of 0¶s. then |uwy| u 3n. Both t¶s are not same. not a 1. is a CFL. If so. So second t is not a repetition of first t. x = a and y = b2c2 then. then uv2wx2y will be a2b3c2 {L If you consider any of the above 3 cases.

REVASTUDENTS.INFO 101 .WWW.

**WWW.REVASTUDENTS.INFO
**

Example:

Show that L={0i1j2i3j | i u 1, j u 1} is not CFL Solution: Assume L is CFL. Pick z = uvwxy = 0n1n2n3n where |vwx| e n and vx { I. vwx can consist of a substring of one of the symbols or straddles of two adjacent symbols. Case 1: vwx consists of a substring of one of the symbols Then uwy has n of 3 different symbols and fewer than n of 4th symbol. Then uwy is not in L. Case 2: vwx consists of 2 adjacent symbols say 1 & 2 Then uwy is missing some 1¶s or 2¶s and uwy is not in L. If we consider any combinations of above cases, we get uwy, which is not CFL. This contradicts the assumption. So L is not a CFL.

Exercises: Using pumping lemma for CFL prove that below languages are not context free 1. {0p | p is a prime} 2. {anbnci | i e n}

102

WWW.REVASTUDENTS.INFO

**Closure Properties of CFL
**

Many operations on Context Free Languages (CFL) guarantee to produce CFL. A few do not produce CFL. Closure properties consider operations on CFL that are guaranteed to produce a CFL. The CFL¶s are closed under substitution, union, concatenation, closure (star), reversal, homomorphism and inverse homomorphism. CFL¶s are not closed under intersection (but the intersection of a CFL and a regular language is always a CFL), complementation, and set-difference. I. Substitution:

By substitution operation, each symbol in the strings of one language is replaced by an entire CFL language. Example: S(0) = {anbn| n u1}, S(1)={aa,bb} is a substitution on alphabet 7 ={0, 1}. Theorem: If a substitution s assigns a CFL to every symbol in the alphabet of a CFL L, then s(L) is a CFL.

Proof:

Let G = (V, 7, P, S) be grammar for the CFL L. Let Ga = (Va, Ta, Pa, Sa) be the grammar corresponding to each terminal a 7 and V Va = J. Then Gd= (Vd, Td, Pd, S) is a grammar for s(L) where y Vd = V Va y Td= union of Ta¶s all for a 7 y Pd consists of o All productions in any Pa for a 7 o The productions of P, with each terminal a is replaced by Sa everywhere a occurs. Example: L = {0n1n| n u 1}, generated by the grammar S p 0S1 | 01, s(0) = {anbm | m e n}, generated by the grammar S p aSb | A; A p aA | ab, s(1) = {ab, abc}, generated by the grammar S p abA, A p c |I. Rename second and third S¶s to S0 and S1, respectively. Rename second A to B. Resulting grammars are: S p 0S1 | 01 S0 p aS0b | A; A p aA | ab 103

WWW.REVASTUDENTS.INFO S1 p abB; B p c | I In the first grammar replace 0 by S0 and 1 by S1. The resulted grammar after substitution is: S p S0SS1 | S0S1 S0p aS0b | A; A paA | ab S1pabB; Bp c | I II. Application of substitution:

a. Closure under union of CFL¶s L1 and L2: Use L={a, b}, s(a)=L1 and s(b)=L2. Then s(L)= L1 L2. How to get grammar for L1 L2 ? Add new start symbol S and rules S p S1 | S2 The grammar for L1 L2 is G = (V, T, P, S) where V = {V1 V2 S}, S (V1 V2) and P = {P1 P2 {S p S1 | S2 }} Example: L1 = {anbn | n u 0}, L2 = {bnan | n u 0}. Their corresponding grammars are G1: S1 p aS1b | I, G2 : S2 p bS2a | I The grammar for L1 L2 is G = ({S, S1, S2}, {a, b}, {S p S1 | S2, S1 p aS1b | I, S2 p bS2a}, S). b. Closure under concatenation of CFL¶s L1 and L2: Let L={ab}, s(a)=L1 and s(b)=L2. Then s(L)=L1L2 How to get grammar for L1L2? Add new start symbol and rule S p S1S2 The grammar for L1L2 is G = (V, T, P, S) where V = V1 V2 {S}, S V1 V2 and P = P1 P2 {S p S1S2} Example: L1 = {anbn | n u 0}, L2 = {bnan | n u 0} then L1L2 = {anb{n+m}am | n, m u 0} Their corresponding grammars are 104

ie s(a) = {h(a)}. (L2)* = a* How to get grammar for (L1)*: Add new start symbol S and rules S p SS1 | I. G2 : S2 p bS2a | I The grammar for L1L2 is G = ({S. ie h(L) ={h(a1)«h(ak) | k u 0} where h(ai) is a homomorphism for every ai §. Corresponding grammars for L1: SpAB. S1 p aS1b | I. {S p S1S2. S V1. It is enough to reverse each production of a CFL for L. S).REVASTUDENTS. Bp2B | 2 and corresponding grammars for L2: S pAB. i. Closure under homomorphism of CFL Li for every ai§: Suppose L is a CFL over alphabet § and h is a homomorphism on §. Bp1B2 | 12. i u 1} is a CFL and L2 = {0i1n2n | n u 1. {a. S2}. IV. by h(a). P. a{nk}b{nk} | k u 0 and ni u 0 for all i} L2 = {a{n2} | n u 1}. Closure under Kleene¶s star (closure * and positive closure +) of CFL¶s L1: Let L = {a}* (or L = {a}+) and s(a) = L1. T. Then h(L) = s(L). S1. i u 1} is also a CFL. Let s be a substitution that replaces every a §.. Intersection: The CFL¶s are not closed under intersection Example: The language L = {0n1n2n | n u 1} is not context-free.WWW. Closure under Reversal: L is a CFL.INFO G1: S1 p aS1b | I. But L1 = {0n1n2i | n u 1. S2 p bS2a}. where V = V1 {S}. to substitute each production ApE by ApER.e.. S). so LR is a CFL. But L = L1 L2. 105 . Ap0A | 0. c. The grammar for (L1)* is G = (V.. Ap0A1 | 01. III. b}. P= P1 {S p SS1 | I} d. Then s(L) = L1* (or s(L) = L1+). Example: L1 = {anbn | n u 0} (L1)* = {a{n1}b{n1} .

g) such that 1. qP. HA. §.INFO However. and a regular language R. qA).R is a CFL. we make the same move in PDA Pd and also we carry along the state of DFA A in a second component of Pd. ie w is in L R. L1. X) = ((r. Closure of CFL¶s under set-difference with a regular language. Proof: R is regular and regular language is closed under complement. So RC is also regular. s = HA(p. §. F) where y Q = (Qp X QA) y qo = (qp.WWW. w. then L R is a CFL. Z0. Pd accepts a string w if and only if both P and A accept w. a) 2. thus intersection of CFL¶s is not CFL a. a. qA. a. §. Z) |-*P (q. FA) for DFA to accept the Regular Language R. qo. g) is in HP(q. w. we have to run a Finite Automata in parallel with a push down automata as shown in figure 1. 106 . +. p). K) are possible if and only if (qp. CFL and RL properties: Theorem: The following are true about CFL¶s L.R = L RC. Construct PDA Pd = (Q. The moves ((qp. ie L . +. FA AND PDA Accept/ Reject Stack Proof: Figure 1: PDA for L R P = (QP. So CFL is closed under set difference with a Regular language. (r. H. L = L1 L2 . HP. w) transitions are possible. Z0. p). I. s). To get L R. Z) |-*Pd ((q. Let A = (QA.K) moves and p = H*(qA. We have already proved the closure of intersection of a CFL and a regular language. I. b. 1. qA) y F = (FPX FA) y H is in the form H ((q. FP) be PDA to accept L by final state. and L2. We know that L . Intersection of CFL and Regular Language: Theorem: If L is CFL and R is a regular language. X) That is for each move of PDA P.REVASTUDENTS.

But §* . Proof: Let L1 = §* . CFL is not closed under complementation. then §* . ie if L is a CFL then LC is a CFL. Theorem: If L is a CFL and h is a homomorphism. Symbols of h(a) are used one at a time and fed to PDA being simulated.L = LC.WWW. Contradiction! . Only when the buffer is empty does the PDA read another of its input symbol and apply homomorphism to it.INFO 2. CFLs are not closed under set-difference. and L is any language. After input a is read. So CFLs are not closed under set-difference. 3. Since CFLs are closed under union. V.L2 is not necessarily a CFL. The CFL¶s are closed under inverse homomorphism. By our assumption (L1C L2C)C is a CFL. is the set of all strings w such that h(w) L. Inverse Homomorphism: Recall that if h is a homomorphism. then h-1(L) is a CFL Buff r h( ) h Stack tack Figure 2: PDA to simulate inverse homomorphism We can prove closure of CFL under inverse homomorphism by designing a new PDA as shown in figure 2. But (L1C L2C)C = L1 L2. then h-1(L). h(a) is placed in a buffer.REVASTUDENTS. 107 £ ¤¤ £ §¦¥ ¦¥ I ut PDA © ¨ ¨ ¨ Accept/ e ect .L. ie L1 .L = LC would always be a CFL. If CFLs were closed under set difference. So our assumption is false. §* is regular and is also CFL. L1C L2C is a CFL. But CFL¶s are not closed under complementation. CFL is not closed under complementation LC is not necessarily a CFL Proof: Assume that CFLs were closed under complement. which we just showed isn¶t necessarily a CFL. called an inverse homomorphism.

a.REVASTUDENTS. x). I). . I).WWW. . P accepts h(w) if and only if Pd accepts w. h(a)). K)} y The start state of Pd is (q0. b. because of the way the accepting states of Pd are defined. T. We construct a new PDA Pd = (Qd. q0. I). F) that accept CFL L by final state. bx). I. x) such that o q is a state in Q o x is a suffix of some string h(a) for some input string a in y d is defined by o d ((q. where q is an accepting state of P. X) = {(p.X)} o If (q. (F x I)) to accept h-1(L). Z0. I) y The accepting state of Pd is (q. X) = {((q. Let PDA P = (Q. X) = {((p. Z0. K)} where b T or b = I then d ((q. (q0. . d. where y Qd is the set of pairs (q. . Thus L(Pd)=h-1(L(P)) 108 . Once we accept the relationship between P and Pd.INFO Suppose h applies to symbols of alphabet and produces strings in T*.

REVASTUDENTS.e. Ex: To recognize string that is a multiple of 4 1 0 0 0 S2 0 S1 S0 1 But can regular language recognize strings of the form 0n1n ? No 109 . language Context free language DFA Regular language Regular language A language is called a regular language if some finite automaton recognizes it.WWW.INFO Language Hierarchy and History of Turing Machine A Hierarchy of Formal Languages Turing machines PDA r.

R ) ( S 0 .Y)p (S 3.Y.WWW.1 ) p (S 2 .X) p (S 0 .R) ( S 2 . language) A language is called r.e language if some Turing machine recognizes it.L) ( S 3 .L ) ( S 3 .0 ) p (S 1.INFO Context Free Language A language is called Context free language iff some pushdown automaton recognizes it.1 ) p (S 2 .2) p(S 3 .X.0 .R) SA : Accepting state 110 .X.R ) ( S 1. Ex: To recognize a string of the form 0n1n S p 0S1 / P Limitation again Can context free language recognize strings of the form 0n1n2n ? No Recursively Enumerable Language (r.Y)p (S .R ) ( S 1.L) ( S 3 .0 .Z.e.1.1.0 ) p (S 3 .Y.Y.0 ) p (S 1.L) ( S 3 .REVASTUDENTS.1 ) p (S 3 . Ex: To recognize strings of the form 0n1n2n ( S 0 .R ) ( S 2 .

(0 | 1)* Regular Expn.REVASTUDENTS.WWW.INFO Formal Machines Overview Regex operators: * (Kleene *) | (choice) DFA Regex example: . NFA with P-moves NF A 2-way DFA DF A 111 . (concat) Regular Lang.

there exist certain obviously true assertions that cannot be proved to be true by the system.INFO PDA CFL example: 0n1n CFL CFG But there are strings that cannot be recognized by PDAs. addressed Hilbert¶s Entscheidungsproblem using a different approach. In 1936 the British cryptologist Alan Turing. and showed the existence of some problems where membership cannot be determined. is it possible to formulate a general procedureto decide whether a given element of U is a member of X? Hilbert¶s grand ideas were watered down by the incompleteness theorem proposed by the Austrian Kurt Gödel.WWW.REVASTUDENTS. and a criteria for membership. 112 . His theorem states that in any mathematical system. and a universe of elements U. Given a set X. He proposed two kinds of mathematical machines called the a-machine and the c-machine respectively. For example: anbncn Multi-stack PDA PDA with queues PDA Some Historical Notes At the turn of the 20th century the German mathematician David Hilbert proposed the Entscheidungsproblem.

It can recognize strings that cannot be modeled by CFLs like anbncn. Church and Turing proved the equivalence of Turing Machines and l-calculus. The Turing Machine starts at ³start state´ S0. mathematical model that describes what can and cannot be computed.R} is a set of TM states is a set of tape symbols is the start state is a set of halting states S x T x {L.WWW. This is called the Church-Turing thesis. H. He invited Turing to Princeton to compare Turing Machines with his own l-calculus. on which input is provided as a finite sequence of symbols. The a-machine was found to be more expressive than CFGs. H> where. T.REVASTUDENTS. S0. On reading an input symbol it optionally replaces it with another symbol. The a-machines came to be more popularly known as Turing Machines The Princeton mathematician Alonzo Church recognized the power of a-machines. A Turing Machine consists of a tape of infinite length.R} is the transition function is direction in which the head moves L : Left R: Right input symbols on infinite length tape 1 0 1 0 1 1 1 1 1 1 0 head 113 . Notation for the Turing Machine : TM = <S. and showed that they represent algorithmic computation. Turing Machines Definition: A Turing Machine (TM) is an abstract. A head reads the input tape. S T S0 HS H:SxT {L. changes its internal state and moves one cell to the right or left.INFO But Turing¶s a-machine became famous for something else other than its original intentions.

1. For purposes of simplicity we shall be using multiple tape symbols. Initially the TM is in Start state S0.. 1) (S0. _.INFO The Turing machine model uses an infinite tape as its unlimited memory.) The input symbols occupy some of the tape¶s cells. When a 0 is encountered. Input : #111100000000««. add one more 1 at the end of the string. Move right as long as the input symbol is 1. Transitions: (S0. even though unlimited memory and unlimited time is given. replace it with 1 and halt. STOP) TM Example 2 : TM: X-Y Given two unary numbers x and y. 0) ( h . STOP) 114 . b.REVASTUDENTS. The tape is of infinite length 4. b) (S1. and other cells contain blank symbols. The special states. Halting states and Accepting states. Output : #1111100000000«««. compute |x-y| using a TM. R) (S0. Ex: 5 (11111) ± 3 (111) = 2 (11) #11111b1110000«. (This is important because it helps to show that there are tasks that these machines cannot perform. Some of the characteristics of a Turing machine are: 1. 3. (S0. Solved examples: TM Example 1: Turing Machine U+1: Given a string of 1s on a tape (followed by an infinite number of 0s). R) (h. 1. take immediate effect. The TM head can move in either directions ± Left or Right. 1) (S0. 2.WWW. The symbols can be both read from the tape and written on it. #___11b___000« a) Stamp out the first 1 of x and seek the first 1 of y.

_. L) c) State s3 is when corresponding 1s from both x and y have been stamped out.Z. _) (S4. 1. R) (S2. go to some state s5 which can handle this.b) (S4. R) b) Once the first 1 of y is reached._) (S2. 0. 1) (S4.b) (S6.2) p(S 3 . This means we have stamped out one extra 1 in x. 1) (S6. b.WWW. go back to x.R ) ( S 1.0 ) p (S 1. _. if we reach the head of tape. Step 1: Stamp the first 0 with X. So.REVASTUDENTS. _. (S5.R) ( S 1. _) (S5. b. then stop.1 ) p(S 2 . and then seek the first 2 and stamp it with Z and then move left.INFO (S1. L) (S2.L ) 115 . (S3. _.R) ( S 2 . R) (h. L) (S6. L) (h. 1. STOP) Design of Turing Machines and Universal Turing Machine Solved examples: TM Example 1: Design a Turing Machine to recognize 0n1n2n ex: #000111222_ _ _ _ _««.0 ) p(S 1. b. ( S 0 . stamp it out. #. and replace the blank character with 1 and stop the process. L) (S0. L) (S4. 1. #) (S3. _) (S5. 1) (S1.X. Now go back to x to find the next 1 to stamp. While searching for the next 1 from x. So. STOP) d) State s5 is when y ended while we were looking for a 1 to stamp. But in x.1 ) p(S 2 . L) (S4.0 . 1) (S2. If instead the input ends. which we should replace.1. then seek the first 1 and stamp it with Y. R) (S5. 0) (S3. b) (S1. _. (S2.R) ( S 2 . we have stamped out one extra 1. L) (S6. 1. _) (S3. then y has finished.Y.

L) (S 3. ( S 4 .STOP ) ( S 4 .S ) ( S 2 .2 ._ ) p (S .0._ .X)p(S 0. Step 3: Move right until the end of the input denoted by blank( _ ) is reached passing through X Y Z s only. ) ( S 0 .Z) p (S 4 .L) ( S 3 ._ . seeking 0.1 ) p (h.0 . ( S 0 .2 ) p (h.R) S3 = Seeking X.Y.Y)p(S3.R) ( S 4 . stamp it with Y S2 = Seeking 2. ) ( S 1._ ) p (S .X.STOP ) TM Example 2 : Design a Turing machine to accept a Palindrome ex: #1011101_ _ _ _ _««.1 ) p (h.Y)p (S 4 .STOP ) S4 = Seeking blank These are the transitions that result in halting states.S ) 116 .1 ) p (h.2 ) p (h.0 ) p ( h._ . ( S 0 . (S 3._ ) p (h._ ) p (S 5 ._ ._ . Step 1: Stamp the first character (0/1) with _.INFO S0 = Start State. then move one step right. then the accepting state SA is reached.R ) ( S 4 .1.STOP ) ( S 4 .L ) ( S 5 .1 ) p (S 2 .0 )p(S3.WWW._ .1.1. to repeat the process.2 .2 .2 ) p (h.Y.Y._ .STOP ) ( S 0 .STOP ) ( S 1.Y)p (S 4 ._ ) p(S 3 .STOP ) ( S 2 .1. stamp it with X S1 = Seeking 1.L) (S 3. If the last character is not 0/1 (as required) then halt the process immediately. stamp it with Z Step 2: Move left until an X is reached.Z.R) ( S 4 .L) (S 3.REVASTUDENTS.1 )p(S3.STOP ) ( S 0 . then seek the last character by moving till a _ is reached.0 ) p(S 1.

REVASTUDENTS.0 .0.R ) ( S 5 ._ .0) p (S 4 .S ) ) ) The sequence of events for the above given input are as follows: #s010101_ _ _ #_s20101_ _ _ #_0s2101_ _ _ .L) ( S 4 .1) p (S 6 .WWW.S (S 0 ._ .1) p (S 6 . #_0101s5_ _ _ #_010s6_ _ _ _ #_s60101_ _ _ #_s00101_ _ _ .INFO Step 2: If the last character is 0/1 accordingly._ ..L) ( S 4 .L) ( S 6 . then accepting state is reached.0) p (S 4 . #_ _ _ _ s5 _ _ _ _ _ _ #_ _ _ _ sA _ _ _ _ _ _ Exercises: n 2n 1. 117 . Design a TM to recognize a string of the form a b .1.S (S 5.. (S 3.L) ( S 6 ._ .L) ( S 4 ._ ._ .. then move left until a blank is reached to start the process again._) p (S 0 ..._) p(SA.1.1) p (S 4 .._ .L) ( S 6 ._) p(S 0 .0) p (S 6 .R) Step 3 : If a blank ( _ ) is reached when seeking next pair of characters to match or when seeking a matching character._) p(SA._) p(SA. ( S 3 .

WWW. 118 .REVASTUDENTS. Design a TM to recognize a string of 0s and 1s such that the number of 0s is not twice as that of 1s.INFO 2.

R) .1} H : (a. For example. Inside each module.T.1) (c.d}.R) . The associations between the modules are shown in the following figure: TM: 0n1n2n 0-Stamper 1-Seeker 1-Stamper 2-Seeker 2-Stamper 0-Seeker Universal Turing Machine A Universal Turing Machine UTM takes an encoding of a TM and the input data as its input in its tape and behaves as that TM on the input data.R) and so on then TM spec: $abcd$a$bd$01$a0b1Ra1c1Rc0d0R««.0) (b.REVASTUDENTS. H={b.0) (d. 1-seeker. the problem of designing Turing machine to recognize the language 0n1n2n can be divided into modules such as 0-stamper.1. 1-stamper.0. (a.S0.H. S={a. S0=a.d} T={0. 2-seeker and 2stamper. (c. there could be several state transitions. 119 . where $ is delimiter This spec along with the actual input data would be the input to the UTM.1. 0-seeker.c. The main problem can be divided into sequence of modules.b. A TM spec could be as follows: TM = (S.INFO Modularization of TMs Designing complex TM s can be done using modular approach.WWW.d) Suppose.

REVASTUDENTS.0100. Then repeat the steps with next input. Go back and find which transition to apply.0010.0010.INFO This can be encoded in binary by assigning numbers to each of the characters appearing in the TM spec. the sequence goes through the cycle: oad p ecode p xecute p tore 120 .0011. Sequence of actions in UTM: Initially UTM is in the start state S0.0001. Hence.0001.0000.WWW. Load the input which is TM spec. Then store the changes. The encoding can be as follows: $ : 0000 0 : 0101 a : 0001 1 : 0110 b : 0010 L : 0111 c : 0011 R : 1000 d : 0100 So the TM spec given in previous slide can be encoded as: 0000.0000. Make changes.0100 «« Hence TM spec can be regarded just as a number. where necessary.

H> H: S x T à S x T x {L. Composite Tape TMs Track 0 0 1 1 0 1 0 1 0 0 « 0 0 1 1 1 1 1 1 0 « 1 Track A composite tape consists of many tracks which can be read or written simultaneously. 2. right or stay.Move back left without changing the tape 3. 11} Turing Machines with Stay Option Turing Machines with stay option has a third option for movement of the TM head: left. H. STM = <S. b.(s¶¶. R) 121 . d} T¶ = {00. b. c.a) |-. 10. 01..a) |-.Move right. T.INFO Extensions to Turing Machines Proving Equivalence For any two machines M1 from class C1 and M2 from class C2: M2 is said to be at least as expressive as M1 if L(M2) = L(M1) or if M2 can simulate M1.STM: H(s.b. S} Equivalence of STMs and TMs STM = TM: Just don¶t use the S option« TM = STM: For L and R moves of a given STM build a TM that moves correspondingly L or R« TM = STM: For S moves of the STM. s0. T = {a.WWW. M1 is said to be at least as expressive as M2 if L(M1) = L(M2) or if M1 can simulate M2. do the following: 1.REVASTUDENTS. R.S) TM: H(s. A composite tape TM (CTM) contains more than one tracks in its tape.(s¶. Equivalence of CTMs and TMs A CTM is simply a TM with a complex alphabet.

Equivalence of 2TMs and TMs 2TM = TM: Just don¶t use the left part of the tape« TM = 2TM: Simulate a 2-way infinite tape on a one-way infinite tape« « -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 « 0 ±1 1 ±2 2 ±3 3 ±4 4 ±5 5 « Multi-tape Turing Machines A multi-tape TM (MTM) utilizes many tapes.(s¶. Equivalence of MTMs and TMs MTM = TM: Use just the first tape« 122 .REVASTUDENTS. the tape is infinite on both sides.L) 2-way Infinite Turing Machine In a 2-way infinite TM (2TM).INFO H(s¶¶.WWW. There is no # that delimits the left end of the tape.*.*) |-.

INFO TM = MTM: Reduction of multiple tapes to a single tape. Consider an MTM having m tapes. A single tape TM that is equivalent can be constructed by reducing m tapes to a single tape.REVASTUDENTS.. 123 .WWW. A B C 0 1 2 3 4 5 6 7 « 0 1 2 3 4 5 6 7 « 0 1 2 3 4 5 6 7 « TM A0 B0 C0 A1 B1 C1 A2 B2 C2 A3 B3 .

INFO Multi-dimensional TMs Multi-dimensional TMs (MDTMs) use a multi-dimensional space instead of a single dimensional tape.WWW.b. T. H> where H: S x T à2SxTx{L.R} Ex: (s2.R)} 124 . H.a) à {(s3.a.REVASTUDENTS. TM Equivalence of MDTMs to TMs Reducing a multi-dimensional space to a single dimensional tape. s0.L) (s4. 0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 Non-deterministic TM A non-deterministic TM (NTM) is defined as: NTM = <S.

a) à (s4.R)} bccaaabccacb s2 bccbaabccacb s3 bccaaabccacb s4 125 .a) à (s3.WWW.INFO Equivalence of NTMs and TMs A ³concurrent´ view of an NTM: (s2.REVASTUDENTS.a.R) Simulating an NTM with an MDTM Consider an MDTM to simulate an NTM: (s2.L) (s4. two TMs are spawned: (s2.b.a.L) (s4.b.R)} è at (s2.b.L) (s2.a.a) à {(s3.a) à {(s3.a).

- TOCmodule-1
- automata
- TOC-LN
- Module 1
- ch1
- Semi Order
- Languages and Machines (Thomas A. Sudkamp)
- lec18
- Finite Set
- ch2. Language of Mathematics
- smt_1_small
- Probability - Basic Ideas and Selected Topics
- Automaton Basic Description
- ch2Set theory
- Element (Mathematics)
- Introduction_to_Bisimulation_and_Coinduction.pdf
- Nowhere Dense Set
- Function
- 0306459671 Mathematical
- Ordinal Number 8
- s fn
- Kisacanin-Mathematical Problems and Proofs Combinatorics, Number Theory, And Geometry - Kluwer Academic
- Sets
- mathematical foundation
- Sigma Algebra
- Power set
- Equivalence Class 6
- Objective Ch 2 FSC Part1
- Dense Order
- BC0039-Discrete Mathematics-Model Question Paper
- FAFLNotes-RS

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd