0% found this document useful (0 votes)
15 views

notes

The document discusses context-free grammars (CFGs) and provides various examples of CFGs for different languages, including integers, palindromes, and balanced parentheses. It explains the derivation process using production rules and presents multiple CFGs for specific languages defined by regular expressions. The document also touches on the concept of S-grammars and provides examples to illustrate the definitions and applications of CFGs.

Uploaded by

720823103021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
15 views

notes

The document discusses context-free grammars (CFGs) and provides various examples of CFGs for different languages, including integers, palindromes, and balanced parentheses. It explains the derivation process using production rules and presents multiple CFGs for specific languages defined by regular expressions. The document also touches on the concept of S-grammars and provides examples to illustrate the definitions and applications of CFGs.

Uploaded by

720823103021
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 38
CONTEXT-Fpa EE GR) 7 Context-free grammars derive the amen 8 sre ft of production can be made va “ot dependt on the symbols in th does ex: quence of allowing Only a single ons ir name from the any time, sec h fact that the substitution of the variable Test Of the ana an able appears in the sentential form. It Variable on thecal form (context). This feature is the on the left side of the production. . - DERIVATIONS c F H (the production rules are used to derive cer fain strin; age usiny . es ar cert 3 wer 1 ion. 'n strings. The generation of language using 43 We now define the notations to Tepresent a derivation, These are ducél : ee > Ifa. >be a production of Pin CFG and aand bare strings in (V,, UE)" then aab=> Bb. We say that the production o + Bis a i ‘ sabdirectly drives aBb. Now, if we suppose 0,41, ¢1,,..,0, are string in (Vy US)" and m21and Oy Pg, Op Py, Oy Oy Oy Py: 'pplied to the string ab to obtain afb or we say that then we Say => Oy. If a drives B by exactly ‘7 steps, we say a=. Example 1 Construct a context-free grammar G generating all integers (with sign). Solution. Let G=(Vjy,, P,5) where Vy ={S,, , } L=0,1,2,3, 94-4 P consists of $—> +I- < integer >> < digit > < integer >I< digit > < digit >> 01112 131516171819. L{G)is the set of all integers. L{Q)is the language of G Iti the set of terminal strings that have derivations from the start symbol. Example 2__ Construct a CFG for the language L=(a"b": n21}, Solution. Let Gbe CFG for language L=((a"b": n21)) G=(Vy/X P,5) Here, Vy, ={S, L=(a,b} and P consists of P: 1.$->aSb 2. S—>ab. 488 THEORY OF AUTOMATA & FORMA! LANGUAGES a Example 3 Construct a CFG for the language L={wcw*: w e(a,b)") Solution. Let G be the CFG for the language L={wew®: w (a,b)") G=(Vy LD P,S). Here, Vy =(5, L= (a,b, and Production rule Pis defined as S—aSa ( S—>bSb Src Example 4 Write a CFG, which generates string of balanced parenthesis. Solution. This grammar will accept the balanced right and left parenthesis. ¢¢, (),(()) i accepted {( ), ()} is also. Let CFG be G=(Vy,, P,$) ; where Vy ={9 Z=((,)} Set of Production P is given by S>Ss S>(9) Sra. Example 5 Write a CFG, which generates palindrome for binary number. Solution. Grammar will generate palindrome for binary numbers, ie, 00, 010, 11, 101 etc. Let CFG be G=(Vy, P,5); where Vy = (5, E=10,1) and Production rule Pis defined by $3051 S181 S0lLla. Example 6_ Write a CFG, which generates strings having equal number of o's and Us. Solution. Let CFG be G=(V,,,5, P,) where Vy ={5}, E= {a,b} Pis defined as S—aSbs S—»bSaS San. — 7 c ; CONTEXT-FREE GRAMMARS 189 Design a CFG which con generate string, hai Solution. Let CFG be G=(V.,5 pg) y nd ombination of Productions Pare defined ag” Sb R= lat P: Sas Ss Sra Sob 4's and b's except null string. 8 example 8__ Write a CFG for the language L(G) = {ww *: w €(0,1}"} Solution. Let CFG be G=(Vy,5, P,S); where Vy =(S, 5= 0,11 Productions P are defined as S050 S181 Sra. Example 9 4 ” Design a CFG for the language L={a"b™: n+ 1}. Solution. If n# mthen there are only two cases possible. Case 1: n>m then let l, ={a"b™: n> mi We say G, be the CFG for the language [,. G =(Vy EPS) where Vjy =(5,,A,S') Z’=(a,b) and the productions Pare defined as s'> AS, 5, a5, bla AsaAla Case 2:When naAblB B Bolin (if) Let CFG be G=(Vy, P, S); where Vy, = {5}, =(a,b,4) and production P consists of Srasbb S—+aSbbb san These productions non-deterministically produce either bb or bbb for each generated single ‘d. / ~—_ SONTEXT-FREE GRaMMaRs 191 ple 32 FREE GRAMMAR Design CFG for the language ; La wiman, Solution. Suppose CFG be G=(vy, Zp 7 m0}, where Viv =(S,A, B), Z=(a,b,c} and Pig defined as () SAAB (ii) Assan (iit) B- bce} n example 13 Write the CFG for the language ; | = (a"b"C":5,m>0) Solution. Let us assume CFG be G - G=(Vy,2, P,S) where Vy =(5, A,B), 2 ={2,b,c} and Pis defined as () SAB (i) A->aAbb| a fxample 14 (it) B> Bln Design a CFG for the language : L={a"b™ :n 20}. Solution. Let us assume CFG be G G=(Vy,E,P,S) where Vy =(S},2={a,b} and Pis defined as () S+aSbb (ii) San Example 15 Design a CEG for the language () L={a"b™c"d":n21,m>1} (i) L={a"b"c"a":n21,m21} Solution. (i) Let CFG for the given language Lbe G G=(Vy/%,P,S) where V,, = {S,X], 2 =(a,b,cd) and productions Pis defined as (a) S+aSd (b) Sraxd () X+bXe (@) Xe (i) Let CFG for given language Lbe G G=(Vy_%,P,5) ; Where Vy = (5, A,B}, = {a,b cd) and productions P is defined as (@) SAB (b) Aa Ablab (c) B> cBa| od Samole 16 Dep Limsiruct CFG forthe language containing all the strings of di = 0,1) fferent first and last symbol over 192 THEORY OF AUTOMATA & FORMAL LANGUAGES Solution. In thi, ifthe string starts with 0 it should end with 1 or if the string starts & . should en with 0, : 1, $0411 40 2 A-OAILAIA The non-terminal A->0AILAI* indicates (0+1)*. Thus the given CFG is equivalent to the regular expression (0(0+1)°1+1(0+1)* 0} Example 17 Construct CFG for the language containing at least one occurrence of a double a. Solution. The equivalent regular expression of given CFG is ((a+b)* aa (a+b)") i, the production rules are : 1, S+BAB 2 Aaa 3. Bo aBlb Bis Example 18 _ Construct CEG for the language in which there are no consecutive b's the strings may or ‘may not have consecutive ds. Solution. Let the CFG G=({S, A}, a,b}, P,S) and P consists of S—aSib Alalbl* A-aSlal* Example 19 Find a CEG for each of the languages defined by the following regular expressions : () ab* (i) a’ b° (iii) (baa + abby’. Solution. (i) The required CFG for ab* is SB BbBI* (ii) The required CFG for a* b* is SAB AvzaAl* BobBI* (iii) The required CFG for (baa+abb)* is S— AS | BS|* A baa Babb CONTEXT-FREE GRAMMARS 193 write a CEG for the regular expression 9° oot Solution. Suppose CFG for the above j (0+1) 1UPTU, 2004-05) pete Vy =(5. 4B, E= 0,1} and productions prot VE, P,S) S+AIB A>DAIA B-OBi1B), sxample 21 _ Design CFG for the regular expression (a +b)‘ aa(a+ by" Solution. Suppose CFG for the above is G=(Vy,5, P 5) where Vy =18,A]2=(4,b), and productions Pare S—> AaaA A-aAlba Ata B 22 Design a CFG for the regular expression {H(baa)" bbaa{ba)" :n 20). Solution. Let CFG be G=(V,,,£, P,S) where Vy ={5,X}, 2 = {a,b}, and productions P are SX A baaXba . X — bbaa Example 23 Write a CFG for £ = (a,b) that generates the set of (a) all strings with at least one ‘a’ (©) all sérings with exactly one ‘a’ (c) all strings with at least two ‘a’s. Solution. (a) Let CFG be G G=(Vy/E,P,S) where V,. ={5, Al, E=(a,b] and Pis defined as (i) S>AaA (i) A bAlaAln (b) Let CFG be G’ for given language G=(Vy/E-P/5) where Vy ={5, A}, £= {a,b} and Pis defined as @ S Aad (i) A>bAIA 194 THEORY OF AUTOMATA & FORMAL LANGUAGES (©) Let CFG be G" for given language G’ =(Vy,, P,$) where Vj, ={S, A}, E= (a,b) and Pis defined as () $+ Anda (ii) AaAl bA|A Example 24 Write the CFG for the language L={a'bict: i= jor j =K} Solution. Suppose L, = {a‘bict ; land L, ={a‘bict :j =k} So L=l, UL, ue Let us consider 1, , suppose CFG for L, is G, G =(Vy 2,25) where Vj, =(S,,4, B),2={a,b,¢) and P, is defined as @ 5, aB (i) A aAbla (ii) Bo Bn Now consider L,, suppose CFG for L, is G, “Gy =(Vy% PysS)) ‘ where Viy ={5,,C, D}, © =|a,b,c} and P, is defined as @ 50D i) Cran “ Gi) D> bDej Now, with the help of 1, and I; we can define CFG for. language L Let the CFG for Lis G G=(Vy,3,P,9) where Vy =(5,5,,5,,4,B,C, D}, 2= a,b,c] and Pis defined as ($4515, QS AB. @) AsaAblA ~*~ (4) B->cB]n ©) S, cD )CaCln (7%) D> bDeln Example 25 ‘ Design CFG for L= {w € (0,1)" :n, (w) + n,(w)). Solution. Let CFG be G=(Vy,Z, P, ) where Vy ={S, A,B}, £=(a,b} and Pis defined as @) S+AIB a (ii) A-va|aA|bAA| AAb| ALA ema eet (ii) By b|bB|aBB| BBa| BaB . Strings derived from A has more than Ys and strings derived from B has more Vs thané® ye CONTEXT-FREE GRAMMARS 195 wo bi ett CFG fo generate L914 a solution. Let CFG for given Lis ¢ G=(Vy,,5, P, 5) vy 2th, E=labic productions Pis defined as () S$ aSclac|be|bAc [UPTU 2008-09] where (ii) AS bAc| be ample 27 Find the CFG for the set of all regular expressions on the alphabet {a,b}. Solution. Let CFG for given Lis G=(V,,,, Ps) — ere Vy = (5,4, Bl, 2= {a,b} and productions Pis defined as () SAABIBA (i) A-saA\n (ii) By 5B] $-grammar Acontext-free grammar G=(V, 5, P, S)is said to be a simpl Ya: productions are of the form simple grammar or S-grammar if all its A>ax where AeV,aeZand x eV", and any pair (A,a) occurs at most once in P. Example 28 The grammar S -» aS|bSS|c is an S-grammar. Solution. The grammar S — aS|bSS|aSS|c isnot an Sgrammar, because the pair (S,a) occurs in the two productions $ > aS and $—> aSS. Every S-grammar is unambiguous grammar. Example 29 Find an S-grammar for L={a"b" :n21}. Solution. L={a"b":n21} S-grammar for Lis Sad . A aAB\b Bob & Sample 30_ Sb lab. ted by the above grammar. e can generate with the above given The context-free grammar. is given as $4 Find the context-free language (CFL) general og, Botion. Let us start generating the strings that w' 196 THEORY OF AUTOMATA & FORMAL LANGUAGES __ a ‘Now, number the productions as Sab Ah) Sab / 0) Let us start with minimal length string, From production (2) we can get string abin one sg, as S => ab. To start with new derivation, we can have S=>aSb or S=aSb or S=aSb =aabb => aaSbb => aaSbb => aaabbb => aaaSbbb => aaaabbbb Thus, the language can be listed in the form of set as L= ab, aabb, aaabbb, aaaabbbb,...} ie, L={a"b": n21} Example 31_ Find the CFL associated with the CFG given below : S>aBlbA AvalaSlbAA Bb 1bS1aBB Solution. Let SB wl) S— ba A>a lil) A> aS -Aio) A> bAA (0) Bob -(e) B-bS «li B> aBB (i) Start deriving different strings using above CFG. @) S=aB (i) S>bA =ab =ba (i) S>aB = abs = abaB or abbA => abab abba a — Cc 0 NTEXT-FREE GRAMMARS 197 B (o gene = aaBB ©) Soba = aabB | a, | bas = aabb bak S20A Pibaah = baS (vi) S>bA = baba => bbAA = babbAA => bbaSA = babbaSA = bbaaBA = babbaaBA | = bbaabA = babbaabA | = bbaaba = babbaaba | ‘Thus, we can see that all the strings that we have derived have equal number of ¢s and Vs. So, the CFL generated by the given CFG is 44__ PARSE TREE OR DERIVATION TREE (the strings generated by @ context free gr hierarchical parse tree oT 44.1. Characteristics of Parse ‘A Parse tree for a contex a 6 pla The rule of formation ced by ‘bd if there is a producti . Every verte . The root of parse tree has ! A vertex ‘1 is a lea! L= (x : x containing equal number of ¢s and Ys} ammat (Vy, 5P,S) can be represented By @ structure called tree)Such trees representing derivations are called derivation trees or syntax tree.) Tree t-free grammar x of a parse tree has a label which is a variable or termi abel S (start symbol). is a variable. G has the following characteristics: inal or “. The label of an internal vertex is Ifa vertex Ahas k children with Iabels Ay, Azer Ay then A> fy, Agron will Be production in context free grammar G f if its label isae Dor” ther if its label is A terminal A can be / \ b a pathen the tree is ‘nf is the only son of its fa is that nom" jon A NOTE to draw grrow on the edges because The direction of = There is no need production is always downward: —__-. 198 THEORY OF AUTOMATA & FORMAL LANGUAGES _ Forexample, let G=({5, A}, {4,b], P, S) where Pconsists of Sra A SlalSS, A564) ba Fig. 4.1 NOTE If we draw the sons of any vertex keeping in mind the left-to-right ordering, we get the left-to-right ordering of leaves by “reading” the leaves in the anti-clockwise direction. Yield of a Derivation Tree. The yield of derivation tree is the concatenation of labels of leaf | nodes in the left-to-right ordering without repetition. The yield of the derivation tree in Fig. 4.1is ‘aabad. Subtree of a Derivation Tree. A subtree of a derivation(tree is a tree having following characteristics : (i) The root of subtree is some vertex v of tree. (ii) The vertices of subtree are descendents of ‘’ with their labels and (iti) The edges of subtree connect the descendents of v. Fig. 4.2 A subtree looks like a derivation tree except that the label of the root may not be S. Its called an A-tree, if the label of its root is A. . Derivation. The yield of a derivation tree or parse tree is also known as derivation. ‘The structure of derivation tree depends upon the sequence and selection of production rules fo _ CONTEXT-FREE GRAMMARS 199 derivation tree. Therefore, a derivation may b a a most derivation. be obtained either by left most derivation aa . derivation A > wis call ‘i rp ‘ led a leftmost derivation if we apply a prod sefemost variable at every step. ply a production only to the A derivation A=> wis a rightmost derivation i able at every step. lerivation if we apply production to the rightmost Consider the grammar Gwith production. S—aSS\b Now, we have S=>aSS => aaSSs = aabSs (Left-most derivation) => aabaSSS . => aababSs = aababbS = aababbb ‘The sequence followed is “left-most derivation”. Now right-most derivation = S=aSS = aSb => aaSSb => aaSaSSb = aaSaSbb => aaSabbb => aababbb and the mixed derivation as S=aSS = asd => aaSSb => aabSb = aabaSsb => aabaSbb => aababbb ! 200; THEORY OF AUTOMATA & FORMAL LANGUAGES: Example 32 ACFG given by the productions is S—+alaAS A>bS Obtain the derivation tree of the word abaabaa. Solution, String 2baabaa is derived from Sas Ln S=aAS d | N = abSs oa {\/) => aba(a AS) j /\ j => abaabaS ab Sa = abaabaa | and the derivation tree is.as shown in adjoining figure : « Example 33 \ Derive the string “aabbaa” and construct a derivation tree for “aabbaa” for a grammar G with productions S— aAS|a, A—+ SbA| SS| ba. TUPTU MCA 2013-14 Solution. S—aAS\a A- SbA|SS|ba String “aabbaa” is derived from Sas S=> aAS => aSbAS => aabAS => aabbaS => aabbaa and the derivation tree is as shown in figure. y ee ven the grammar G=(Vy,EP, 8); where Vy 6 - EOESELEOEUEyig) EET Obtain the derivation tree, CONTEXT-FREE GRAMMARS 204 land P consists of solution. E /\ \ f — 4 ie 4I\ {| [NK] a Ce) id Ei Lt | | id id => idvid+id => (id+idysid, Bvomple 35__ Show the derivation steps and construct derivation tree for string aabbbb by using left most devotion with the grammar defined as S— ABIA AaB B Sb Solution. We start with the start symbol and the production. AB 7 238 ao aSbB aABbB /\ KS aaBBbB aaSbBbB /\ aanbBbB Lo N\ aabSbbB aababbB /\ /\ bbbSb aun “ALS aabbbb. j ‘nd the derivation tree is as shown along side. “ wn UUHUYHEUYEUUOY >—o. (left-most derivation tree) ~_ ) 202 THEORY OF AUTOMATA & FORMAL LANGUAGES Example 36 Let G be the grammar S—>O0BI1AA -»010SILAA, B-»1/1SI0B For the string 00110101, » (a) left most derivation (b) right most (c) derivation tree. Solution, (a) $=> 0B (0) $30B => 00BB = 00BB = 001B = 00B1S = 0011S => 00B10B =>00110B => 07B101S => 0011018 = 0° BLO10B . = 07170108 => 0? BI0101 = 07170101 f => 07110101 © 4.5 AMBIGUITY IN CFG A terminal string w < L(G)is ambiguous if there exists two or more derivation trees for w (or there exist two or more leftmost derivations of w). A CFG Gis ambiguous if there exists some w € L(G) which is' ambiguous. For Example, G=({S},{a,b,+,*}, P, S) where P consists S—+S+S1S* Slalb, there are two derivation trees for a-+ab. “IN sos s {\ -/N\ fo AW I a db a a ‘Thus, the given grammar is an ambiguous grammar. CONTEXT-FREE GRAMMARS 203 Since derivation trees, leftmost derivations and rightmost derivations are equivalent rotations, the following definitions are equivalent: Let G=(Vy,5,P,S)be a CFG. A string we L(G) is said to be “ambi, derivation trees for that string in G, ACEG given by G=(Vy,¥, P,5) L(G) which is ambiguously derivabl iguouly derivable” if there are two or more different )is said to be “ambiguous’ if there exists at least one string in le, otherwise it is unambiguous. __ Ambiguity is a property of a grammar, and it is usually, but not always possible to find arr equivalent unambiguous grammar. An “inherantly ambiguous language’ is a language for which no unambiguous grammar exists. Example 37 Show that the grammar G with production S—alaAblabSb A>aAAblbS . is ambiguous. Solution. S=>abSb (eS abSb) => abab “ ( $3) Similarly, S=> a Ab ¢ = abSb = abab Since, ‘abab’ has more than one derivations. So the grammar Gis ambiguous. Example 38 : S>aAb) S+b9) The following grammar generates prefix expressions with operands x and y and binary operators +,— and + and the production rules are E->+ EE \“EE|—EE\xly, Find leftmost and rightmost derivations and a derivation tree for the string +*—xy xy. Solution. String is + + —xyxy : Leftmost derivation : Rightmost derivation : Derivation tree « . E E> +EE- E=>+EE : = 44EEE => +Ey “IN, => +*-EEEE = +*EEy UW = 40-2 EEE > +eExy fe By = +¥-xyEE | ste EExy J\\ \ => te-xyxE => +e-Eyxy - ee * = +eoryxy. ae eeoxyxy, ~ || xy 204 THEORY OF AUTOMATA & FORMAL LANGUAGES Example 39 Check whether the given grammar is ambiguous or not : S-iCtS ; S—>iCtSeS ; S7a; Cob Solution, To check whether given grammar is ambiguous or not we will have some Stig for derivation tree such as ibtibtibtaea Now, we draw the derivation tree. ‘ AS, AN, [LAY OLAS, | i t ” | e—o ® Thus, we have got more than two parse trees. Hence the given grammar is ambiguous, Inherent Ambiguity If every grammar that generates L is ambiguous, then the language is said to be “inherently ambiguous”. Example 40 Prove that the language L={a"b"c™)U{a"b™c"} with n and m non-negative, is an inherently ambiguous context-free language. Solution. Suppose L= 1, UL, where L, ={a"b"c™) and L, =(a"b"c""} 1, is generated by S, > S,clA A>aAb|n and L, is generated by 5, -> aS, |B Bo bBe|a Then Lis generated by the combination of these two grammars with the additional production S515, This grammar Lis ambiguous since the string a"b"c" has two different derivations one starting with S—> S, and the another with S— s,. Here 1, and Iz have conflicting requirements, the frst putting a restriction on the numberdt d's and ¥s, while the second does the same for ¥s and cs, Few tries will quickly convince us of the impossibility of combining these requirements in a single set of rules, So this is inherently unambiguous grammar. 4 CONTEXT-FREE GRAMMARS Je 41 om eat the following grammar is ambiguous sh S— AB|aaB A-alAa Bob solution. S-> ABIaaB A al Aa Bob gappose the string w = aab are two left-most derivations for w =aab, $= aaB => aab- and = S=> AB=> AaB=> anB=> aab SN ow OMS | fA | 6 »—> Exumple 42 {UPTU 2003-04) Let L=(a"b":n20} show that I? is context-free language. Solution. L= {a"b":n 20} Let Gbe the CFG for L G=(Vy/E,P,5) Vy = (3) E= (a,b) and Pis defined as (i) S—> aSb, (ii) S->* Now consider a grammar, whose productions are s'55 S— aSbla When we analyze these productions, we find that itis the CFG for the language . So I? is CFG. hy \ gee 7 206 THEORY OF AUTOMATA & FORMAL LANGUAGES _ 4.6__ REMOVAL OF AMBIGUITY a / In programming languages, where there should be only one interpretation of 4, statement, ambiguity must be removed when possible. Often we can achieve this by Tewriting the grammar in an equivalent unambiguous form. Example. Grammar Es! E+E+E E+E*E E>(E) Isalble is ambiguous and the ambiguity is due to the precedence of operator. If we correct the precedence then ambiguity may be removed. Here, two causes of ambiguity in the grammar : 1. The precedence of operators is not respected. 2. The sequence of identical operators can group either from left or from the right. We would see two different parse trees for E+ E+ E. Since addition is associative, it docs not matter whether we group from the left or the right, but to eliminate the ambiguity, we must take one. The conventional approach is to insist on grouping from the left. The unambiguous grammar is E>T TOF FoI E> E+T ToT+E F->(E) Isalble The sole parse tree for a+b* cis _ CONTEXT-FREE GRAMMARS 207 7 J PLIFICATION OF CFG A nacFG G it may not ke Necessary to use ving sentences. So in a context-free | s in G which are not useful for deriv; I all the pfor der symbols in V,, UY.or all the productions in a 5 nBuage L(G) we try to eliminate symbols and ction: od . _. ‘ation of sentences, We i . (a) Variables not deriving terminal strings ehminate (b) Symbols not appearing in any sentential form. (¢) Null productions. . (@ Productions of the form A R ail Construction of Reduced Grammars step 1. We construct a grammar G, equivalent to the gi vatiable in Gy derives some terminal string e given grammar G so that every step 2. We construct a grammar G' =(V; i 'y/X P’,S) equivalent to bol in G’ appears in some sentential form of G. Ssetaterery NS example 43 _ Find the reduced grammar equivalent to the grammar G whose productions are : ~ $y ABICA:B-$ BCIAB A‘ >a, CyaBIb. - ar Solution. Step 1. Find the equivalent grammar G, such that each variable in G, derives some terminal string. * (@)’Constraction: of Vii, £ mt W, ={4, Cas A-raand C bare the productions with a terminal’ strings on RH.S. W, ={A,C}U{A,: 4, > a with a e(ZU[A,C}") ={A,G UI) as we have SCA. W, ={4,C,S}U(A,: A, 9 with a €(ZUIS, A,C*}) 2, Vp =Wy=(S AG. fe 7 Gyr: - B={A, al A, ae(Vy, YE)') B=(S>CA Ava C7 Thus, G, =((5, 4,0, ab, (SCA Aa Ch 9 Step 2. We construct a grammar G’ =(Vx/2", P' S)equivalent to G, so that every symbol in © appears in some sential form of G. W, ={5} ~ M, 5. ULK Vy, UE: ther exists a prodicton A+ with A ey and a contig ={9 VIC A) ={5,C, A} 208 THEORY OF AUTOMATA & FORMAL LANGUAGES W, =(5,C, A} U (a,b) =[5,A,C,a,b} Ww, =W;. P'=(Aa: AeW,) =(S>CA A+4C>} ~~, Therefore, G =((5, AG, (a,b, (SCA Aa, C-F5, 9) is the reduced grammar, Example 44 Consider the CFG, G=(Vy, © P,S); where Vy =(S, A,B, Z=(0, 1) and P=(S— AILBI1A SB Soll A +0 Bo BR Remove the useless symbols from it. Solution. Step 1. Find the equivalent grammar G, such that each variable in G, derives some terminal string, (a) Construction of Vy, = W, = (5,4) as S11 and A—0 are the productions with a terminal string on RHS. W, = (5,4) U{A,: A, rewith a e(ZUIS, A)" as W=W, + Vy =154) (®) Now, 8 ={S>114 $1140} Thus G, = ({S, A}, 0,21, (S114 $11, A->0}, 9) Step 2. Now, we construct a grammar G’ =(V},,5’, P’,5) equivalent to G,, so that every symbol in G’ appears in some sential form of G’. W, ={9) W, = {9 V{1, 1, A 1, 1} ={5, 4,1) W, ={5, 41) v (0) =(S, 40,1) =W,. So, P =(S +114 S11, A->0} Therefore, G = ({S, A), (0,1), {S-+114 S11, A-+0}, 9). Hence after removal of useless symbols we get Ss11All1 A70. Se . CONTEXT-FREE Grammars 209 promple 45 For given CFG, find equivalent CEG with no useless vay S— AB) AC ‘88 variables (UPTU B.Tech. 2013-14] A> aAb|bAa\aq B- bbA| aaB\ AB C abCalaDb D- bD\ac Solution. Step 1. Find the equivalent Brammar G,such that each variable in G, derives some terminal string, 4 (a) Construction of Vy, W, = {Alas A> ais the Production witl W, = (A}U(B} as B—> bbA=(A, B, Ws = 14, BJU {8} as we have $+ AB=(5, 4, B th a terminal string on RH. Wz ={S, 4, Bhug=w, Vu, =1S, A, Bh B= (A a,S—> AB, B-> bbA, A aAb|bAa, B-> aaB| AB) Thus G, =({S, A, Bh, {a,b}, {SAB A ~ @, B— bbA, A aAb| bAa, B~» aaBl AB}, S) (b) Now we construct a grammar G' =(Vj,,2', P’,S)equivalent to G, so that every symool in C’ appears in some sential form of G’ W, = {5} = {S}U[A, B}={S, A, B} Ws = (5, A, B}U {a,b} ={S, A, B.a,b) =S— AB A->a|aAb|bAa B- bbA|aaB| AB Thus, after removal of useless symbols, we get S— AB A alaAblbAa B— bbA|aaBl AB Example 46 Find the reduced grammar that is equivalent to the CFG given below : S—aClSB A>bSCa B aS BIbBC Ca BClad. \ 240 THEORY OF AUTOMATA & FORMAL LANGUAGES ee Solution. Step 1. Find the equivalent grammar G, such that each variable in G, derives Some terminal string. (a) Construction of Vy, : W, ={C) as Cad W, ={C) U {5} as $+ aC ={C,9) W, ={C,S} U{A} as A> bSCa=(5,A,C} S, 4, Ula =(5, 4,0 =Wy. S, A, C} Sa, A>bSCa, Crad) Step 2. Now we construct @ grammar G'=(Vj,5", P’,S) equivalent to G,, s0 that every symbol in G' appears in some sential form of G’. (S,C, a} (Sac, Cr ad} Therefore, G' = ({S,C}, {a,d}, {S—> aC, C> ad}, 9) Example 47 Consider CFG : . S— ABla Ax+b Identify and eliminate useless symbols Solutions. Step 1. Find the equivalent grammar G, such that each variable in G, derives some terminal string. S.A) U$=(5, 4) =W, Vy, ={S,A} and BR =(S+4, A+} Step 2. Now we construct grammar G’ equivalent to G,, so that every symbol in G’ appearsin some sential form of G. W,=(5, W, =19 Ula) =15,9) W, =W,. So P'=(S—a), Therefore, G' = ({S}, {a}, Sa, 9), —— __CONTEXT-FREE GRAMMARS 211 in Elimination of Null Productions context-free grammar may have a nere Ais a variable, is called . form A-,. A production of the form nw ’ null production =< A le 48. Consider the grammar G whose productions mar G, without null productions generating (cp ae AB AA, B->n, D-vb, Construct a Solution. Step 1. Construction of set W of all nullable variables, 8 A variable Ain a context-free grammar is nullable if A> » Ww =(4, €V,: 4, > isa production in G) ={4B WwW, =W, U [Ae Vy: there exists a production A-> a with a €W,) A, BU (5) =(5, A,B UW, so W = (5, A, B hy. Step 2. Construction of P’. (@) Any production whose R.H.S. does not have any nullable variable is included in P”. ie, D->bis included in P’. (b) The productions are obtained either by not erasing any nullable variable on the R.H.S. of A-9%, Xp X OF by erasing some or all nullable variables provided some symbol appears on the RHS. after erasing S-raS gives, SraS and Sa SAB gives, S++ ABS+AS>B .. The required grammar without null production is G, = (1S, A,B Dh, (a4, PS) where P' consists of S—>a, D-»b, aS, S—> ABS 4 SB. Example 49 _ Remove the null production from the following CFG by preserving meaning of it S>XYX X+0Xila Yo1Yla Solution. Step 1. Construction of set W of all nullable variables. W, = (X,Y) as X->aand Y 90 W, = {X,Y} U{S) as S>XYX. W, ={X,Y,5} 8, W=(5X71 212 THEORY,OF AUTOMATA & FORMAL LANGUAGES - ee Step 2. Construction of P’ $+ XY X gives $->X, SPY, S>XY, S4Y% S—>XX after erasing some or all nullable variables provided some symbol appears on the R.H.S, X 0X gives X +0 and X+0X Y +1Y gives Y > 1 and Y>1Y Now collectively, we can write the CFG without null productions is G, = (15,-X,Y1, (0.1), PY, §) where P’ consists of S— XIVIXYIYXIXX X010X Yoully. Example 50 Construct CFG without null production from the one which is given below : S—alAblaBa A>bla BoblA Solution. Step 1. Construction of set W of all nullable variables. W, =(A}as Aaa W, ={A} U{S,B as S Aband B-> A =(54B So, W=15,AB Step 2. Construction of P’. (a) Any production whose R.H.S. does not have any nullable variable is included in P. ie, A—>band Bob S—aare included in P’. (&) $+ Abgives S band $ Ab, Sra Bagives Sa San SaBa BoA Thus the required grammar without null production is G, =((S, A,B, (a,b), P’, 5) where P’ consists of S—alblAblaala Ba A>b B+ bIA _ CONTEXT-FREE GRAMMARS 213 = le 5 ne CFG for regular expression r=(0.)* Boyan yy : +b)", which is free f Il producti i the res from null production. solution. Let Gbe the required CFG with null production then their prod es are: S—> BAB en their production rules are : Aa bb B—>aB\bBI A Now we remove null productions. First we construct set W of all nullable variables. W, = (Bas Bn W, = (BUS W=(B Now the constructions of P’ are S— BAB gives S—> BABand S-> A Abb B->aBgives B->aand B->aB B-> bBgives B> band Bb ‘Thus the required grammar without null productions is G,=((S,4,B, {a,b}, P, S) where P’ consists of S—> BABIA Abb B-> alaBlblbB 47.3 Elimination of Unit Production Auunit production (or a chain rule) in a context-free grammar Gis a production of the form A-» Bwhere Aand Bare variables in G’ Baomple 52_ Let Ge $+ AB A—>a, B—> Clb, C > D, D-> Eand Ea, Eliminate unit productions and get an equivalent grammar. TUPTU 2009-10) Solution. Step 1. Construction of the set of variables derivable from variable. Wo (5)=(9} W,(S) = Wo (S)U [Be Vy : C— Bis in Pwith C eWo (5) =(§US=19-. W(S)=1F Similarly, W(A)= {4} Wo (B)=(B W, (B)={B V{={B W, (B)= (BQ) UID) =(B,C,D} mA 214 THEORY OF AUTOMATA & FORMAL LANGUAGES _ _— W,(B)= (B,C, D) U{E} = (BC, D, E) W(B)={B,C, D, E) W(C)={C,D, W(D)=(D, E} W(E)={E} Step 2. Constructions of A-productions in G, : (a) non-unit productions in Gie, Aa, Bb, Eva, SAB () Ava whenever B-> ais in Gwith BeW(A) Boa (: E-vaand EeW(B) Coa (: E-vaand Eew(C) Da (: E-aand EeW(D) The productions are $+ AB A->a, B->bla, C->a, D->a which has no unit production, Example 53_ Identify and remove the unit productions from following grammar G: S— Alb Aa Bib Bo Sja Solution. Here unit productions are SA A>B Bos Now eliminate unit productions. Step 1. Construction of the set of variables derivable from variable : Wo(S)= {5} W,(5)={S}U{A} ={5, A} W,(S)= (5, A}U (B}= (5, A,B} W,(5)={S, A, B}U{S}={5, A, B} W(S)={5, A, B} Wo(A)=(A} W,(A)= [A] U{B} = (A, B} W,(A)= {A, B}U{S}= (5, A, B) W(A)={5, A, B} Similarly, W(B)=(BS, A} 5 cc step 2+ The productions are : ONTEXT-FREE GRAMMARS 215 (a) Nor-unit productions in G, S+bb Axb Boa ()) We know A-> a whenever B-> a and Bew(A) So S—b and Sa (. AeW(S) and BeW(S)) similarly, A->bband A> a a (» SeW(A) and BeW(A) and $— bb and B->a exists) B= bb an = Thus the new ade : (+ SeW(A) and AeW(B) and 5 bb and A->a exists) S—> bb|alb A bbhalb B— bbla\b example 54 Eliminate the unit-production from the CEG with P given by S$ AalB Bo Albb A-albelB Solution. Step 1. Construction ofthe set of variables derionble from variable Wo (S)= {9 W, (5) =We (S)L BI since S > B ={5,B W,(5)= 15,8 U {Al since BA ={5 4B w(S)=15 4B W,(A)= {4} W, (A) =Wo (A) (BI: since AvB ={AB w(A)={AB W, (B)= (8 W, (B= Wo (BU t = {BA W(B)={A, B Ay since B> A Step 2. The productions are (@) Non-unit productions in G S— Aa Bobb A albe () Salbesince A->albcand AeW(5) S— bb since B-> bb and BeW(5), A> bb since B-> band BeW(A) B-albcsince A-albcand AeW(B) Thus the new production rules are : S—albclbbl Aa A> albblbc B albblbc. If Gis a context-free grammar, we can construct an equivalent grammar G’ which is teduced and has no null productions or unit productions, In step 1, we eliminate null productions to get G,. In step 2, we eliminate unit productions in G, to get G. In step 3, construct a reduced grammar G’ equivalent to G. G is the required grammar equivalent to G NOTE Ifwe change the order, we may not get the grammar in the most simplified form. ~ a 4.8 NORMAL FORMS FOR CFG za Ina CFG, the RHS. of a production can be any string of variable and terminals. When the productions in G satisfy certain Testriction then Gis said to be in ‘normal form’. Much complex CFG gets reduced to simple forms after modifying it or Te-representing it using these normal forms. Among several ‘normal forms’ two important are : & CHOMSKY NORMAL FORM (CNF) (i) GREIBACH NORMAL FORM (GNF) 4.8.1 Chomsky Normal Form (CNF) A CFG Gis said to be in CNF if every production is of the form either A-saor A BCand Saifrel(G) Seduction to CNF 7 Let us consider a CFG having Productions $+ ABlaC, A->b, B+ aAand C+ ABS. CONTEXT-FREE GRAMMARS 217 Inthese, except S—> aC, B+ aA, C+ ABS, ali the oth spvein cNF For the prodition of the form $~» aC we introduce new variable C, such that geac can i PI e ee > CiG C, >a which are in the form required for CNE Similarly, gad can be replaced by B-+C, A and there is no need to write C, +4 again. For the roduction of the form C- ABS, we introduce new variable D such that D-> BS so C> AD, ich are in CNF. : Let G=(Vyy,E P,S) be a CFG. A context-f form by using following steps : er productions are in the form required ree grammar is converted into Chomsky normal Step 1. Elimination of null and unit productions, Step 2, Elimination of terminals from right-hand side. Step 3. Restricting the number of variables on the right-hand side. example 55 A Find thé grammar in Chomsky Normal Form equivalent to S>a AbB A->a Ala, Bb Blb. Solution. Step 1. Eliminate all null and unit productions. There is no such productions so we proceed to step 2. Step 2. Elimination of terminals from R.H.S. (All the productions of the form Non-terminal + terminal or Non-terminal -> Non-terminal Non-terminal are included. (i) If there is a production of the form X + A, A, ... A, with some terminals on RH. If A; is a terminal, say @,, add a new variable C,, to Vy and C,, a, to B. In production X + A, 4... A, every terminals on RH‘. is replaced by corresponding new variables on the RHS. are retained and the resulting production is added to P,. Here A->aB->b are added to R. S+aAbB Aad, Br bByields $C, AC, B C, aC, >b A+C,A B+C,B and Vy=(5, A,B C,, CG} Step 3. Restricting the number of variables on R.HL.S. Consider A—> A, A, ....4, where m23. We introduce new productions A> A,C, C, > 4,C, Cy AgCy «oes Cpa > Anya Ay and new variables C,,Cy,-..Cy.2- These are added in P’ and Vj, respectively. Here, , consists of SC, AC, B A>C, A B>C,B C, >a C,>b Ara, Bob. SC, AC, Bis replaced by $+ C, C,,C, + AC, C, + C, Band remaining productions are added to production P,. So G = (15, ABC, Cy Cy -Col, tab, Pay S), where P, consists of SC,C, G PAC, GPCBAPCA BIC BC, a. Cb Ada Bob Which is in CNF and equivalent to given grammar. JUAGES 218 THEORY OF AUTOMATA & FORMAL LANGUAGES _ Example 56__ # Convert the grammar with productions S— ABa Aa aab BAC to Chomsky Normal Form. Solution. Step 1. Eliminate all null and unit productions. There is no such productions, So py, ce step 2. Step 2, Introduce new variables C, , C,, C. S—» ABC, A>C,C,C, BAC, Goa Gb Cae Step 3. But we know CNF can be defined as Non-terminal -»Non-terminal Non-terming] So, introduce additional variables to get the first two productions into normal from, SAC, c> BC, A>C,C GCG, B- AC, Ca Gb Ce 4.8.2 Greibach Normal Form (GNF) {UPTU MCA 201541 form met fiee grammar is said to be in Greibach Normal Form if every production so Aaa where ae AeVy and aeVy, It shows that ais a string of zero or more variables, Grammars in GNF are typically complex and much l nich te} a M longer than the CFG from w! were derived. GNF is useful for Proving the equivalence of context-free grammars = Reduction to GNF CONTEXT-FREE GRAMMARS 219 gvomple 57_ Solution. Step 1. Eliminate “pr Normal Form and rename the variables gg 4 ta"® then construc a grees are : (a ; @ grammar in Chomsk: Therefore grammar Gis defined ag | °”"”4+ With S= 4, and so on, = C=(Vy,EP, S) where Vi TA Apne Aydand S| productions are A; + A,A,la; 4, + AATh Aare renamed as A, and Ay respectively. So the Step 2. To obtain the productions in the form A, > ayor 4 © Avproductions in the form A, -> Avy, such that i A, A, a (ie,, the form A Ay A ~ bis also in required form since A, — bis of the form A, ay. The production A, — A, A, is not in required form. A, > A,A,A, or aA, (Replace 4, > A, A, and A, a) So, A,-productions are : ie, A, > AAA, A, aA A, >b Step 3. Convert A,-productions to the form A, ay. Here productions of the form A, > A,y are eliminated by using following lemma. Lemma : Let G=(V,,,©, P, §) be a CFG. Let the set of A-productions be A—> Aa, |... Act, |B, LIB, B's do not start with A). Let Z be a new variable. Let G, =(Vy U(Z}E.P,5) where Bis defined as follows: () The set of A-productions in F, are A->B, |By «By. A>; ZIBp Zl---IB, Z i wll (ii) The set of Z-productions in F, are Zo 1a-le, ZayZ 1022 | ions for the other variables are a: slot, sin P, Then G, is a CFG and equivalent (iii) The producti toG "220 THEORY OF AUTOMATA & FORMAL LANGUAGES _ Here A; -> A; Ay A, isnot required form. We apply lemma to A,-productions. Let, bog, new variable. ae Here a= A, A. A, aA, A, ->b (already) A, aA, Z, A, >bZ, Z, 9A, A Z, 9A, AZ, Step 4. Modify A,-productions to the form A, —> ay for i=1,2,...,n—1 At the end of step 3, the A, productions are the form A,—ay. The A, productions are of the form A, , ~ ay’ Ana > Aye. Here, A,-productions are A, > aA; |bla A, Z, | bZ,.- Among 4,-productions 4, — A, A, is not in required form. It is reduced to A, aA, Ay | bA, laA,Z, Ay | BZ Ap. (Replace A, production) So, A;-productions are A, —> ala A, Ay |bAy la A,Zy Ap | BZ Ap. Step 5. Modify Z,-productions, which is not the required form. | Here the Z,-productions to be modified are Z, > A, Ay, Z) > Ay A, Zp | Z, aA, A,|bA,1aA,Z, A, 1bZ,A Z, aA, A, Z, aA, Z, A, Zy|bZy Ay Zp. Hence the equivalent grammar is G=((A,, Ay, Zo}, {a,b}, B,, A;) where P, consists of A, Pala A, AyIb A, |aA, Zy Ay IbZ5 Ay A, aA, Ibla A, Z, |bZ, Z, aA, A|bA la A, Z,A,1bZ5 A, 2, 0A, A, Z,|b A, Z,|aA, Zp A, Zp bZy Ay Zy Example 58_ Consider the grammar ({S, A, By, {a,b}, P, S) that has the productions S—>bAlaB A>bAAIaS\a B-aBBlbslb Find the equivalent grammar in CNF. Solution. Step 1. Eliminate all null and unit productions. There is no such productions. $0 we proceed to step 2. Step 2. We know CNF can be defined as Non terminal — Non terminal Non terminal Non terminal > terminal. —— CONTEXT-FREE Gi Ri Here productions Aa and B+p punans 228 iyctions: say, pot Cc, a c,h are alread Y In CNP. If we introduce two more e grammar can be written as $C, AIC, B A>C, AAIC, Sla and C, og BC, BBIC, Ib and c, +p still there are two productions which are n ‘ot in CNF. AC, AA !, namely B>C, BB the a0" After further ° reat ioe and introducing two more new symbols C, and C; defined as C, > BB ‘Thus, we can write the given grammar in CNF as SC, BIC, A A>C,G1C, Sla BC, CIC, Sib C, >a C36 CAA C, > BB Example 59 Reduce the given CFG with P given by S—rabSblalaAb A>bSIaAAb to Chomsky Normal Form (CNF). Solution. Step 1. There are no null and unit Step 2. S—>ais in CNF. For S$ abSb, we have SC, C, 5G G74 cb For S—aAb, Wehave SC, AC, productions. 222 ‘THEORY OF AUTOMATA & FORMAL LANGUAGES For A->bS we have A->C, S For A-+a AAb we have A+C, AAC, So new production rules are : $C, C, SC, SC, AG, A>C,5 AC, AAC, Cc, a Cb Sra In these productions three productions are not in proper form, namely, $C, C, SC, $C, AC, A>C, AAG,. ‘Hence we assume new variables C,, C>, C,, C, and C; with productions given as below : For SC, C, SC,, we have SC, G, G2G,G,C 956, For SC, AG, we have SC, Cy, Cs > AC, For AC, A AG, we have AC, Cy, Cy ACs, Cy > AC,. So, the grammar in CNF with productions given by S36, G S4C, C, GGG + SC, C+ AG, A>C, Cy Cy > AC, Cs > AC, A+G,S Ca Gb and Soa. le 60. oO oe | the following CFG into CNE co CONTEXT-FREE GRAMMARS 223 S—aaaaS _ pan ie, Lela: nay S—aaaa ion. There are no null . golution. null and unit Productions, Now, if we add a rule A-—awhich is in CNF, then the gi , e i SpAAAIAS Sven production rules become $3 AAAA Let us take SA AAAS can be replaced by C,. If we define C, > AAAS, then rule becomes $+ AC, and C, 9A AAS. is replaced by C, C, > AASand C, > AC, and C, >A AS. is replaced by C, C, > ASand C, > AC;. So, we rewrite the rule S—>aaaaS as SAG G > AC, CG, > AC; C, > AS Now ruleS> AA AA If we break this as $>C,C, and C,>AA and C, AA then finally we can collectively show the CFG converted to CNF as $3 AG G7 AC, C, 3 AC; C, + AS S$ C,Cs C, > AA C5 AA Ava 226 THEORY OF AUTOMATA & FORMAL LANGUAGES ‘Thus the required grammar in GNF as A a, Aylb Ay Ay A, Ayla ZA, Ag Ib As Ap ZA, Ay |b Ay Ay 0A, Ib Ay Ay Ay 10Z Ay Ib Ap AyZ Alb Ay > Alb AyAy |aZIb.Ag a Za, Ay A, Ay Ag 1b Ay Ay Ay Ay Ay Ay laZ Ay Aa Ay Ar Z->b Ay Ay ZAy Ay Ay Ag! BAs Ay AlAs Ap As Ar Z Zo bAy Ay Ay Ay Ay A ZIAZA, Ay Ay Ag Ay Z1DAs Ay ZA, Ay As Ar Z Z>bA, A, A, Z Example 63 Find a grammar in CNF equivalent to the grammar § —>~SI[S >)Slpl¢ Here S being the only variable. Solution. The given grammar has no unit or null productions Sop S—> gare in required CNF form. Now S—>~S induces $-» AS and A>-~. ° $-»[S>)S]induces $—» BSCDSE Bo C2753. D>) E>] Now, 5 BSC DSE gives S— BC, GSC, GCC; G7>DG Cy 9SE Thus the final productions are : S$ plql ASIBC, A7~ Bol Ca> D>) E>] C, > SC, GCC, C, + DC, and C, =SE

You might also like