lecture-5.md (2309B)
1 +++ 2 title = "Lecture 5" 3 +++ 4 5 # Context-free languages 6 7 ## Context-free grammars (CFG) 8 9 Grammar G = (V,T,S,P) is \"context-free\" if all production rules are of 10 the form A → u, where A is a variable and u is variable or terminal. 11 i.e., LHS of all rules is a single variable. 12 13 A language L is context-free if it has a context-free grammar. 14 15 Let G be grammar, consider derivation S ⇒<sup>\*</sup> w. 16 17 - G right linear: w contains at most one variable 18 - G context-free: w can contain multiple variables 19 20 Which variable do you expand? Leftmost, or rightmost. The result 21 doesn\'t depend on the strategy, but on the choice of rules. 22 23 ## Derivation trees 24 25 Derivation tree for a CFG has nodes with labels that can be variables, 26 terminals, or λ. 27 28 - root is labeled by start symbol 29 - if there is production rule: 30 - A → x<sub>1</sub> ... x<sub>n</sub> with n ≥ 1: node labelled A can have children 31 labelled x<sub>1</sub>...x<sub>n</sub>. 32 - A → λ: node labelled A can have one child with label λ 33 - every node with label that\'s a terminal is a leaf (has no children) 34 35 Labels of leaves of derivation tree, from left to right, skipping λ, 36 form a word in L(G). 37 38 ## Ambiguous grammars 39 40 A CFG is ambiguous if there is a word in the language that can have 41 multiple derivation trees. 42 43 An example of ambiguity is the dangling else problem. Joerg gave an 44 example in ALGOL, but it\'s the same in C (and is solved by always 45 making else bind tightly, i.e. to the nearest if): 46 47 ```c 48 if (condition) 49 if (condition2) printf("Whatever"); 50 else printf("Something else"); 51 // without braces or indentation, you don't know whether the else corresponds to the 52 // inner or outer if. 53 ``` 54 55 Ambiguity is undecidable -- you can\'t algorithmically determine whether 56 a CFG is ambiguous. 57 58 Inherently ambiguous language: if the language is context free, and 59 every grammar for that language is ambiguous. 60 61 ## Normalising 62 ### Removal of λ rules 63 64 λ-production rule: a rule where a variable can rewrite to λ 65 66 Steps: 67 68 1. Note which variables rewrite to λ (any number of steps). 69 2. For all variables B from step 1: for every rule A → xBy, add a new 70 rule A → xy. 71 - watch out for transitivity: i.e. if A → B and B → λ, then A → λ. 72 3. Remove all rules with λ on the right hand side.