Formal Languages & Automata
Formal Languages & Automata
ata*Saatath S=S*S=>S+S*S=atS*S>ata*Saatath Therefore, a + a * b is ambiguous, Q.5(a) What do you mean by Reduced form of a CFG ? Explain with example. Ans. Reduced Form : A grammar is said to be in the reduced form if there are productions of the form w,Aw, — w,, where ‘w’ does not contain a Non-terminal, This should be applicable for all the Non-terminals that at one point or another Non-terminal should result in@ terminal string. Consider the following grammar,Ss > BC Sb Bob Observe that from the start symbol we generate two Non-terminals ‘B’ and ‘C’ and out of the ‘B’ will produce a terminal but we can not eliminate the Non-terminal ‘C’ from the string such a grammar is not in the reduced form. Example : Check whether the following grammar is in reduced form. S + AB . SCA B — BC B- AB Ana C > aBlb Observe that to terminate the generation process we should use any of the following rules, Ana Cb Hence whenever from given Non-terminal we get the Non-terminal ‘4’ and ‘C’ we can generate the string only terminal: Observe that from’*S’ we can generate ‘C4’, hence ‘S’ will generate a terminal string. oe S CA : APU SLIC: Q.5.(b) What are normal forms of CFG ? Explain convert a CFG into CNF. S - AACD, A aAblA, C > aCla, D > aDalbDb|A. Ans. Normal forms for Context-Free Grammars : In a context-free grammer, the R.H.S. ofa production can be any string of variables and terminals. When the productions in G satisfy certain restrictions, then G is said to be a ‘normal form’. Their are mainly normal forms - the Chomsky normal form (CNF) and the Greibach normal form. Chomsky Normal Form : In the Chomsky normal form (CNF), we have restrictions on the length of R.HLS. and the nature of symbols in the R.H.S. of productions. Definition : A context-free grammar G is in Chomsky normal form if every production is of the form 4-9 a, or A> BC, and S— A isin Gif A L(G). When A is in L(G), we assume that S does not appear on the R.H.S. of any production. For example, consider G whose productions are S > AB| A, A > a, Bb, Then Gis in Chomsky normal form. Greibach Normal Form : Greibach normal form (GNF) is another normal form quite useful in some proofs and constructions. Definition : A context-free grammar is in Greibach normal form if every production is of the form A — aa, where 1 € Vyanda€ £(amay be A), and SA isin GifA€ L(G). When A L(G), we assume that S does not appear on the R.H.S. of any production. For example, G given by S—> aAB| L, A bC, Bb, C9 is in GNF.Concersion of CFG into CNF : Let G be the grammar w! th productions S — AACD A > adb|A Cc >aC\a D -»aDa| bDb\ A Let us go through each step of the conversion to NF. (1) Eliminating A-productions : The nullable variables are A and D, produces the grammar with productions 4 5 AACD| ACD | AAC| CD | ACI C A > aAb|ab C7 aCla D — aDa|bDb|aa|bb (2) Eliminating unit productions : Here we may simp S 7aC\a ly add the productions and delete S > C. (3) Restricting the right si two or more variables : This step yields the productions § -» AACD| ACD| AAC| CD | AC| X,C | ati? X AX, |XX, CX Cla Day» X,DX, | X,DX,1%,X,1 XX des of productions to single terminals or strings of # " to mb (4) The final step to CNF : There are six productions whose right sides are too long, Applying our algorithm produces the grammar with productions AT, T, 2 AT, T, > CD 3M, U,30D 3Ay, VAC > CD|AC|X,Cla 7M, W, — Ax, SMVDINF>u4uuDN L a. a 8 Section - C Q.6. Construct a PDA accepting {a" b" a” | m, n> 1} by null store. Construct the corresponding CFG accepting same set. ‘Ans. The pda A accepting {a"ba"| m, n> 1} is defined as follows :Uap Ses i eae 2 A = (£409 9,35 {4, 5}, (a, Z,}, 5, dus Zs 0) where 6 is defined by Ri: Gy a, Zq) = {Gy aZ,)} R, + 8p a, a) = {(q,, aa)} R,: 84, b, a) = {(q,, a)} Ry: 8g, 6,4) = (fq, a)} R,: 8, 4,4) = ({9,, A} Ro: 8G,A,Z,) = ({9,. )} ‘We start storing a’s until a b occurs (Rules R, and R,). When the current input symbol is 6, the state changes, but no change in PDS occurs (Rule R,). Once all the 5’s in the input string are exhausted (using Rule R,), the remaining a’s are erased (Rule R,). Using R,, Z, is erased. So, (oy 2b" A”, Z,) FH (qs As Zp) EQ» As AD This means that aba" € N(A). We can show that N(A) = {a"b"a"| m,n > 1} by using Rules R,-R, Define G=(V,, {a, b}, P, S), where V, consists of [40> Ze» Fads [4s Zyp Gol» [Gor 4 Iolo [9,2 % 4] [de 2 115 Uys Zs Ts [de> a Gh [9 9,1 The productions in P are constructed as follows : The S-productions are 5 PS Ads Zo Gh PS [doZy 4] IQ: 4, Z,) = {(diy.Z,)} induces + FS 3 Go Zp Go) > 440» 4s ll 4o> 2 Gel Pot [or ZG) > aly & FIGs Zp Gl Pst [de ZH) > aly 4 M14» 2 4) Pot Me 2 1) > Ady % HMI Zy 41) 8(g,, 4, a) = {(q,, aa)} yields 1 Gor 4 4) > 4 % Gll4or 4 4] 5 Ger I] > 14> @ YMG» & 9] = [9 % 9) > alg, @ IMG 4 94] Pt [4491 > a4, @ 9,119, % 9%] 5(q,, b, a) = {(q,, a)} gives Pay t [do 4 Go) > FL» & 4] Pit [9 4 9,] > 519, 4 9] 8(q,, b, a) = {(q,, a)} yields Pit [4% 9) > 9, 4 4) Pit [4% G1 > Old a9) 8(q,, 4, a) = {(q,, A)} gives Pit [yaglra » et5g A, Z,) = {(q,, AD} yields Py t (Zea) A Q.7.(a) Design a TM for subtraction for m— 1, where m > " and both mn>=1, Ans, Step : y : © Step-1 : 1f0 found convert all 0's into 0's and go right then convert Cinto C and gy right. i : © Step-2: IfX found then convert all X into X and go right or if 0 found then conver, 0 into X and go left and go to next step otherwise go to 5th step. i © Step-3 : Then convert all X into X and go left then convert C into C and go left © Step-4 : Then convert all 0°s into 0’s and go left then convert B into B and go right then convert 0 into B and go right and repeat the whole process. / © Step-5 : Otherwise if B found convert B into B and go left then convert all X intoB and go left then convert C into B and go left and then stop the machine. Q.7.(b) Write short note on Halting Problem of TM. Ans, Halting Problem of Turing Machine : This technique is used to prove the undecidability of halting problem of Turing machine. We say that problem 4 is reducible to problem B if a solution to problem B can be used to solve problem A. For example, if 4 is the problem of finding some root of x4 — 3x2 + 2 = 0 and B isthe problem of finding some root of x? ~2=0, then A is reducible to B. As x2 — 2 is a factor! x4 — 3x2 +2, a root of x» -2=0 is also a root of x4~3x2 +2=0, Theorem : HALT yy4 = {(M, w)| The Turing machine M halts on input w} is undecidable Proof : We assume that HALT, is decidable, and get a contradiction. Let M, bet TM such that (M,) = HALT 7, and let My halt eventually on all (M, Ww). We construct a TMM) as follows: 1. For Mp, (M, w) is an input, 2. The TM M, acts on (M, w). 3. If Mj rejects (M, w) then My rejects (M, w). a Se Rn ee aN Oe ML De SOBRIS REO RINIER S ES ERTAEN aee 4. If M, accepts (Mj, w), simulate the TM M onthe input String w until M halds. 5. If M has accepted w, My accepts (M, w); otherwise My rejects (M, w). When M, accepts (M, w) (in step 4), the Turing machine M halts on w. In this case either an accepting state q or a state q’ such that 8(q',a)is undefined till some symbol a in w is reached. In the first case (the first alternative of Setp 5) M accepts (M, w). In the second case (the second alternative of setp 5) My rejects (M, w). It follows from the definition of. My that M, halts eventual ly. Also, (M2) = {(M,w)| The Turing machine accepts w} =A This is a contradiction since ise is undecidable. Section - D Q.8. What are Primitive Recursive Functions ? Explain and prove that the following functions are primitive recursive : (i) Transpose (ii) Concatenation (iii) Identity. Ans. Primitive recursive function : (a) ifitis one of three initial functio (b) ifitcanbe obtained by applying composition & recursion a finite number of time to the set of initial function. > For ex. Atotal function fover Nis called primitive recursive ns, f(%, ») = »isa primitive recursive function. () Transpose Function: The transpose function can be defined by trans(x) = x7, Then “ PUBL if ) trans(A) = A trans(ax) = concat(trans(zx), a(x)) trans(bx) = concat(trans(x), b(x)) Therefore, trans(x) is primitive recursive. (ii) Concatenation Function : The concatenation function.can be defi concat(x,, x,) = x,x, concat(A, x,) = id(x,) concat(ax,, x,) = cons a (concat(x,, x,)) concat(bx,, x,) = cons b (concat(x,, x,)) So concat is defined by recursion using id, cons a and cons b, Therefore, concat is primitive recursive. (iii) Identity Function : Let us denote the identity function by id. Then, id(A) = A id(ax) = cons a(x) id(bx) = cons (x) So id is defined by recursion using cons a and cons b, Therefore, the identity function is Primitive recursive, ined byies of each class of gramma, Q.9. Write and briefly explain the charact classified according to Chomsky hierarchy. Determine the type of grammar G ; @ Sad, A aAB,B>b,A>a (ii) S > aAB, AB C, A b, B> AB. Ans. Chomsky hierarchies of grammars = grammars by the Chomsky hierarchy. Chomsky provided an inital classificat type: We can exhibit the relationship between ion into four languages Type -0 (Unrestricted grammar) Type — 1 (Context sensitive grammar) Type — 2 (Context free grammar) Type -3 (Regular grammar) Type 0 languages are those generated by unrestricted grammars, that is, the recursively enumerable languages. Type | consist of the context-sensitive languages, Type 2 consists of the context-free languages and Type 3 consists of the regular languages, Each language family of type k is a proper subset of the family of type k - 1. Following diagram shows the original Chomsky Hierarchy, Unrestricted (Type -0) Grammars : The unrestricted grammar is defined as = Vy Vp PS) where = a finite set of non-terminals = a finite set of terminals a 1 = starting non-terminal, Se V nTech F*Semesier, Molle! Tesi Paper MS and P is set of productions of the following form ap where o and B are arbitrary string of grammar symbols with a # €. These grammars are known as type-O, phase-structure or unrestricted grammars. Context Sensitive Grammar : Let G = (Vi Vp, P, S) be context sentive grammar where V,, = finite set of non-terminals V, = finite set of terminals S = starting non-terminals S € V,, and P is the set of fules called productions defined as a>Bp where B is at least as long as a that is clearly lel < (Bi The term “Context-sentive” comes from a normal form for these grammars, where each production is of the form ct 4 a — a, B a with B # €. Replacement of variable A by string B is permitted in the “Context” of a, and a. Context-Free Grammars : Mathematically context-free grammar is defined as follows: : “A grammar G = (V,, V, P, S) is said to be context-free” where V,, = A finite set of non-terminals, generally represented by capital letters A, B, C, D, . A finite set of terminals, generally represented by small * letters, like a, b,c, def « S = Starting non-terminal, called start symbol of the grammar. S belongs to Vip P = Set of rules or production in CFG. Gis context-free and all production in P have the form a>Bp where a eV, and Be VV," Regular Grammars : A regular grammar may be left linear or right linear.