Homework Context Free Lnguges III Prse Trees nd Homework #5 (due 10/22) From textbook 6.4,b 6.5b 6.9b,c 6.13 6.22 Pln for tody Context Free Lnguges Next clss of lnguges in our quest! Lnguges Recll. Wht is lnguge? Wht is clss of lnguges? Context Free Lnguges Context Free Lnguges(CFL) is the next clss of lnguges outside of Regulr Lnguges: Mens for defining: Context Free Grmmr Mchine for ccepting: Pushdown Automt CFLs nd Regulr Lnguges Venn digrm of lnguges Context Free Lnguges Regulr Lnguges Finite Lnguges 1
Context Free Grmmrs Let s formlize this bit: A context free grmmr (CFG) is 4-tuple: (V, Σ,, P) where V is set of vribles Σ is set of terminls V nd Σ re disjoint (I.e. V Σ= ) V, is your strt symbol Context Free Grmmrs Let s formlize this bit: Production rules Of the form A βwhere A V β (V ) * string with symbols from V nd We sy tht γ cn be derived from α in one step: A βis rule α = α 1 A α 2 γ = α 1 βα 2 α γ Pln for tody Ambiguous Grmmrs nd Prse Trees Questions? Prse Trees Grphicl mens to illustrte derivtion of string from grmmr Root of the tree = strt vrible Interior nodes = other vribles Children of nodes = ppliction of production rule Lef nodes = Terminl symbols Another exmple Find CFG to describe: L = { i b j c k i = k} B (1) c (2) B bb (3) B Λ (4) Cn lso write s B c B bb Λ Another exmple Let s derive string from L: bbcc c rule 2 cc rule 2 Bcc rule 1 bbcc rule 3 bbbcc rule 3 bbλcc rule 4 = bbcc 2
An inorder trversl of the tree will give the the string derived. Prse Tree c c B b b B B Λ Rule 2 Rule 2 Rule 1 Rule 3 Rule 3 Rule 4 Recll our exmple from lst time Defining the grmmr for lgebric expressions Production rules + (1) (2) * (3) / (4) () (5) (6) One more exmple Prse Tree how derivtion for + * + rule 1 + + * rule 3 + * + * + * + rule 1 + + * rule 3 + * + * One more exmple Prse Tree Another derivtion for + * * rule 3 * + * rule 1 + * + * * + * rule 3 * + * rule 1 + * + * 3
Prse trees + * * + A CFG is sid to be mbiguous if there is t lest 1 string in L(G) hving two or more distinct derivtions. me string, 2 derivtions Fmous progrmming lnguge mbiguity Dngling else <> if (<expr>) <> if (<expr>) <> else <> <some_other_> if (expr1) if (expr2) f(); else g(); Fmous progrmming lnguge mbiguity if ( expr ) else expr1 if ( expr ) g(x); To which if does the else belong? expr2 f(x); In this derivtion, the else belongs to the 1 st if Fmous progrmming lnguge mbiguity Fmous progrmming lnguge mbiguity if ( expr ) expr1 A wy to fix this <> <s1> <s2> <s1> if (<expr>) <s1> else <s1> <other> <s2> if (<expr>) <> if (<expr>) <s1> else <s2> if ( expr ) expr2 f(x); else g(x); <s1> represents if sttements with mtching else <s2> represent if sttements with t lest 1 unmtched if The <s1> in the rule for <s2> will ssure tht ll sttements between if nd else will not hve dngling else. 4
Fmous progrmming lnguge mbiguity s2 if ( expr ) s1 expr1 if ( expr ) s1 expr2 f(x); else s1 g(x); ome lnguges re inherently mbiguous All possible grmmrs tht generte the lnguge re mbiguous Unfortuntely, there is no lgorithm tht cn tells us whether grmmr is mbiguous or not. howing grmmr is mbiguous is esy Find string x in the L(G) tht hs two derivtions howing prticulr grmmr is not mbiguous is usully difficult. howing tht ny grmmr is not mbiguous is not possible. Derivtions Leftmost derivtions A leftmost derivtion is one where the leftmost vrible in the current string is lwys the first to get replced vi production rule. A rightmost derivtion is one where the rightmost vrible in the current string is lwys the first to get replced vi production rule. Derivtions + + + + + + As it turns out (we won t prove this) In unmbiguous grmmrs,leftmost derivtions will lwys be unique. In unmbiguous grmmrs,rightmost derivtions will lwys be unique. rightmost derivtion leftmost derivtion 5
Removing mbiguities ince some lnguges re inherently mbiguous This cnnot lwys be done However, On cse by cse bsis, mbiguities cn be eliminted Exmple Abbrevited grmmr for lgebric expressions Production rules + (1) * (2) () (3) (4) Exmple This grmmr hs two problems 1. Precedence of opertors is not respected * + should be interpreted s (*) + 2. equence of identicl opertors cn be grouped either from the left or the right + + cn be interpreted s either (+)+ or + ( + ) Exmple olution Introduce some new vribles Fctor expression tht cnnot be broken up by either * or + () Term expression tht cnnot be broken up by + All Fctors T * F Expression ll possible expression All Terms + T Exmple Our new grmmr + T T T T * F F F () Note tht ll recursion is leftmost * hs higher precedent thn + + + + * is interpreted s ((+) + ) + (*) Exmple + + + * + T + T T * F + T F F F 6
Exmple It cn be shown Tht every string x, tht is generted by this new grmmr, hs only one leftmost derivtion As such this new grmmr is unmbiguous Done using induction on the x. ummry A grmmr is mbiguous if there is string generted by the grmmr tht hs two distinct derivtions. ome lnguges re inherently mbiguous All grmmrs tht generte the lnguge re mbiguous There is no lgorithm to determine if ny given grmmr is mbiguous Proving grmmr to be mbiguous is esy Proving tht grmmr is not is hrd. Questions? 7