Download Ambiguity of Grammar in Context Free Grammar - Review Sheet | CMSC 330 and more Study notes Programming Languages in PDF only on Docsity! 1 CMSC 330: Organization of Programming Languages Context-Free Grammars CMSC 330 2 Review • Why should we study CFGs? • What are the four parts of a CFG? • How do we tell if a string is accepted by a CFG? • What’s a parse tree? CMSC 330 3 Review A sentential form is a string of terminals and non- terminals produced from the start symbol Inductively: – The start symbol – If A is a sentential form for a grammar, where ( and (N|)*), and A is a production, then is a sentential form for the grammar • In this case, we say that A derives in one step, which is written as A CMSC 330 4 Leftmost and Rightmost Derivation • Example: S a | SbS String: aba Leftmost Derivation Rightmost Derivation S SbS abS aba S SbS Sba aba At every step, apply production At every step, apply production to leftmost non-terminal to rightmost non-terminal • Both derivations happen to have the same parse tree • A parse tree has a unique leftmost and a unique rightmost derivation • Not every string has a unique parse tree • Parse trees don’t show the order productions are applied CMSC 330 5 Another Example (cont’d) S a | SbS • Is ababa in this language? A leftmost derivation S SbS abS abSbS ababS ababa Another leftmost derivation S SbS SbSbS abSbS ababS ababa CMSC 330 6 Ambiguity • A string is ambiguous for a grammar if it has more than one parse tree – Equivalent to more than one leftmost (or more than one rightmost) derivation • A grammar is ambiguous if it generates an ambiguous string – It’s can be hard to see this with manual inspection • Exercise: can you create an unambiguous grammar for S a | SbS ? 2 CMSC 330 7 Are these Grammars Ambiguous? (1) S aS | T T bT | U U cU | (2) S T | T T Tx | Tx | x | x (3) S SS | () | (S) CMSC 330 8 Ambiguity of Grammar (Example 3) • 2 different parse trees for the same string: ()()() • 2 distinct leftmost derivations : S ⇒ SS ⇒ SSS ⇒()SS ⇒()()S ⇒()()() S ⇒ SS ⇒ ()S ⇒()SS ⇒()()S ⇒()()() • We need unambiguous grammars to manage programming language semantics CMSC 330 9 More on Leftmost/Rightmost Derivations • Is the following derivation leftmost or rightmost? S aS aT aU acU ac – There’s at most one non-terminal in each sentential form, so there's no choice between left or right non- terminals to expand • How about the following derivation? – S SbS SbSbS SbabS ababS ababa CMSC 330 10 Tips for Designing Grammars 1. Use recursive productions to generate an arbitrary number of symbols A xA | Zero or more x’s A yA | y One or more y’s 2. Use separate non-terminals to generate disjoint parts of a language, and then combine in a production G = S AB A aA | B bB | L(G) = a*b* CMSC 330 11 Tips for Designing Grammars (cont’d) 3. To generate languages with matching, balanced, or related numbers of symbols, write productions which generate strings from the middle {anbn | n 0} (not a regular language!) S aSb | Example: S aSb aaSbb aabb {anb2n | n 0} S aSbb | CMSC 330 12 Tips for Designing Grammars (cont’d) {anbm | m 2n, n 0} S aSbb | B | B bB | b The following grammar also works: S aSbb | B B bB | How about the following? S aSbb | bS |