Cotext-free grammars ad laguages Basics of strig geeratio methods
What s so great about regular expressios? A regular expressio is a strig represetatio of a regular laguage This allows the storig a whole (evetually ifiite) laguage as a (sigle) strig 2
Example Let L = { w : w {0,}* w eds with } The regular expresssio (0 + )* o describes completely this laguage. Therefore L, which is a ifiite laguage, ca be "stored" by storig R (a strig of legth 8). 3
A laguage that is ot regular Theorem 8.: The laguage over the alphabet {0, } L = { w:( N) w = 0 } is ot regular. Proof: By cotradictio. Assume that L is regular.ice L is ifiite, its regular expressio R must cotai the star closure of a o - empty, o - ull regular expressio.ice the basic ( i.e. alphabet) symbolsare 0 ad, such expressio must be either : 0*,*, (0 + )*, or (0o)*. Now, sice L((0 + )*) = {0,}*, (0 + )* produces more strig tha those i L, ad therefore (0 + )*. 4
Proof (cot.) ice L((0o)*) = { λ,0,00,000,...}, (0o)* produces strigs that are ot i L. Therefore, (0o)*. Assume that = 0*. The, the laguage : M = { w : ( x, y {0,}*) ( N) w = must be a sub - laguage of L. But this is ot the case, as well. imilarly,if = *, the laguage : Q = { w : ( x, y {0,}*) ( N) w = must be a sub - laguage of L. This is also false. Therefore, there is o regular expressio for L. This cotradicts the assumptio that L is regular. x0 x y} y} 5
Motivatig grammars The laguage L = { w : w {0,}* ( N) w = hows the existece of o-regular laguages. Ca they be described i a sigle strig? The aswer comes with the itroductio of grammars. Grammars are desiged for: 0 } describig, aalyzig ad geeratig laguages 6
Defiitio of cotext-free grammar Defiitio 8.: A cotext-free grammar (CFG) is a four-tuple G = (, A, R, ) where. is a fiite set of symbols called variables; 2. A is a alphabet 3. I A = φ 4. R ( U A ) * is the set of rules ( v, w ) R is usually deoted v w ad called a productio 5. is the startig or iitial variable. 7
Example Let G = where ({ },{0,}, R, ) R = { 0 Here meas or. Thus, Ad thus, 0} 0 0 0 R = { 0, 0} 0 8
The strig derivatio process The geeratio of a strig with CFGs is called derivatio trigs are derived as follows:. elect a productio whose left had side is the start variable. Ex: 0 2. elect a variable i the rightmost side of this productio. Ex: i the previous CFG we have o choice but, as is the oly variable. 9
trig derivatio process (cot.) 3. elect a productio iitiated with the chose variable. Ex: Assume that we choose: 0 4. Replace the selected variable with the rightmost side of the selected productio. Ex: Choice 3. yields 0 00 5. Cotiue util o variables are left. I the example, o variables are left. The derivatio eds with 00 0
Deotig derivatios Let G = (, A, R, ) be a cotext-free grammar. Assume that w ( A)* is a strig derived from G through a sequece of productios. The, we write w G, w G or, if the umber of productios is ot importat,
Example With the CFG of the previous example: 0 00 (after replacig with 0) 000(after replacig These derivatios are deoted: 0, G, 00, G,2 000; G,3 ad respectively. with 0) 2
The laguage of a cotext-free grammar Defiitio 8.:The laguage of a cotext-free grammar G =(,A,R,), deoted L(G), is the set of all strigs i A* that are geerated (this is, derived with) by G. Thus, L( G) = { w : w A* ( N) w} G, Defiitio 8.2: If a laguage over a alphabet A ca be geerated by a cotext-free grammar, the the laguage is said to be a cotext-free laguage 3
Represetig cotext-free laguages ice a CFG is a four-tuple of fiite mathematical objects, it has strig represetatios. Thus, cotext-free laguages ca be stored (without loss of iformatio) as a strig represetig its cotext-free grammar Ulike regular laguages whose regular expressios are easy to fid, either fidig a cotext-free grammar for a laguage or idetifyig the laguage of a cotext-free grammar, may be much more complex 4
A simple example of laguage idetificatio Theorem 8.2: The laguage of G = is W = { w: ( N) ( ) w = 0 Proof: ({ },{0,},{ 0, 0}, ) } Recall that L( G) = { w : w A* ( N), w} G, To show : (a) W L( G) (b) L( G) W Proof of (a) : We eed to demostrate the predicate :( N) Q( ), where Q( ) :" ( a derivatio) Assume first that =. The, sice the productio 0 R, There is ideed a derivatio 0 0. G, G, " 5
Proof (cot.) Assume 2. We show Q( ) by iductio o the auxiliary predicate : Let P( ) :" Base case : = 0 G, ice 0 R, there exists the derivatio Iductive hypothesis: ( Iductive thesis : P ( ) ( N)( 2 ) Thus, ( N)( " > ) P( ) P ( + ) Proof : By hypothesis, there exists the derivatio ice 0 R, there exists the derivatio + + 0, as well. G, + Now, by applyig 0 R we get 0 G, 0 ) Q( ) is a true predicate. 0 G, 0 G,. 6
0w Proof (cot.) Proof of (b) : It will be show that G caot geerate strigs that are ot i W. Observe first that G has two rulesstartig with thestart variable,amely 0 ad 0. Therefore, G has oly twosubstitutios.thus, ( w) w = w w w w w i ( A )* ( i)( i i+ w w =,..., ) w 0w We will demostrate by iductio that there is at most oe variable P( ) :" Iductive thesis: 0. w Iductivehypothesis:( ) i Base case: =. I this case, clearly, G, i+ w w w has oe symbol or oe". i = per strig derived by G.This is, we'll demostrate : G, 0 G, w w has oe symbol w w has oe symbol or oe. G, + G,. or oe. 7
Proof (cot.) Proof of the iductio : Assume that G, + w = Therefore, w =. w m ( i)( < i < variablei w. Therefore, w has oly oe start variable. m) w i =. The, there exists w ( A )*, such that w w usig 0 to substitute its ow. This is so, because the oly other possible productio ( i. e. 0) does ot preserves. But the, because of the form of the productio, w i w = = 0 w w w i+ i 2 w i+ 2 w m w is the oly start G, 8
Parsig regular expressios Is the same as aalyzig a regular laguage: a recursive process for idetifyig the expressio s basic regular compoets. The recursio decomposes first the lowest hierarchy operatio Ex: The aalysis of a + b* c is represeted i the tree a + b* c a + b* c b* c b * 9
Method 8.: Cotext-free grammar geeratig the laguage of a regular expressio. Write the parsig tree of the regular expressio 2. Replace the root ad each iteral ode that is ot a operatio, with a variable 3. Keep the termials as the leaves, except for termials uder a subtree rooted by a variable havig the operatio * as child. I these cases, replace the termial a, with the strig au, where U is a variable 4. Write the rules as follows: 4. The root is the startig variable, ad each paret-childre edge correspod to a rule. For buildig these rules: 4.2 Assig to each triple of siblig odes,, U the strig U 4.3 Assig to each triple of siblig odes, +, U the strig U 4.4 For each ode havig U ad * as childre, create λ U 4.5 For each leaf au i a sub-tree rooted by a variable havig * as child, create U ad the rule U λ 20
Example : Cosider R = 0+* 0+ Parsig tree after replacemets Correspodig set of rules: +, 0, 0, { 3 2 2 R = 2 * 0 0 4 3 2 o + }, 3 4 4 3 λ λ
Two productios with the previous grammar The resultig grammar is: G=(,,..., },{0,}, R, ) { 7 Where R is the set of rules defied i slide 2 The strigs w=0 ad w =, which are i L(0+* 0+), are produced by G as follows: 0 The strig w= 0, which is also i L(0+* 0+), is produced by G as follows: 4 2 0 3 3 0 4 0 0 4 3 0 0 0 22
trig parsig Defiitio: The parsig of a give strig is the geeratio of a tree represetatio of the derivatio of the strig by a grammar. The tree represetig the strig geeratio is called parse tree. Example: Parse tree for w=0 uder the previous grammar 3 2 4 3 4 3 4 λ 0 23
Grammars vs. regular expressios Oe ca (easily) be coviced that the laguage of the grammar G i the previous costructio is the laguage of the regular expressio. This is, L(G)=L(0+* 0+) Q: Does this double represetatio costitutes a redudacy? A: NO!. Ulike a regular expressio whose role is merely descriptive, the grammar built by Method 8., (as ay other grammar) costitutes (the basis of) a computatioal method (ot ecessarily the most efficiet) for geeratig the strigs of a laguage described by a regular expressio INDEED, uderlyig grammars is the idea of storig a laguage ot as a (static) mathematical descriptio but as a computatioal method BE FULLY AWARE OF THI CONCEPTUAL FACT!!!! 24
A fudametal theorem Theorem: Every regular laguage is a cotext-free laguage cheme of the proof: Let L be a regular laguage ad L is completely described by a regular expressio R. Let G w L.ice L is regular, be the grammar built from the parsig of Build the parsig tree of tree to show that If, o the other had, L The, there is o parse tree for w i terms of This implies that w is ot described by R. But this is a cotradictio with the assumptio that w L. R. w uder R. Use this parsig w.this shows that G L L( G), the ( w L) ( L( G). w). G the grammar G. 25