Lecture : Parsimonious Menu Fix proof from last class Interpretive Dance! Parsimonious (Parsimoniously) P Comments Available oday P will be returned uesday cs0: heory of Computation University of Virginia Computer cience David Evans http://w.cs.virginia.edu/evans Closure Properties of CFLs Complementing Non-CFLs If A and B are context free languages then: AR is a context-free language RUE A* L = { w Σ* is not a CFL. Is its complement? is a context-free language RUE A is a context-free language (complement)? A B is a context-free language RUE A B is a context-free language? CFG for L (L ) All odd length strings are in L Odd Even Even XY YX X ZXZ 0 Y ZYZ Z 0 Odd 0R R 0 R 0Odd Odd How can we prove this is correct? 5 What is the actual language? Yes. his CFG recognizes is: 00 0X X0 X 0X0 X 0X X0 0 ε Bogus Proof! 0X 0X0 00 L 4 odd generates all odd-length strings Odd 0R R 0 R 0Odd Odd Proof by induction on the length of the string. Basis. Odd generates all odd-length strings of length. here are two possible strings: 0 and. hey are produces from the rd and 4th rules. Induction. Assume Odd generates all odd-length strings of length n for n = k+, k 0. how it can generate all odd-length string of length n+. 6
Odd generates all odd-length strings Odd 0R R 0 R 0 Odd Odd Induction. Assume Odd generates all odd-length strings of length n for n = k+, k 0. how it can generate all odd-length string of length n+. All n+ length strings are of the form abt where t is an n- length string and a {0,, b {0,. here is some derivation from Odd * t (by the induction hypothesis). We can generate all four possibilities for a and b: 00t: Odd 0R 00 Odd * 00t 0t: Odd 0R 0 Odd * 0t 0t: Odd R 0 Odd * 0t t: Odd R Odd * 0t CFG for L (L ) Odd Even Even XY YX Odd 0R R 0 X ZXZ 0 R 0 Odd Odd Y ZYZ Z 0? Proof-by-leaving-as- Challenge Problem (note: you cannot use this proof technique in your answers) 7 8 Even trings how Even generates the set of all even-length strings that are not in L. Even XY YX X ZXZ 0 Y ZYZ Z 0 Proof by induction on the length of the string. Basis. Even generates all even-length strings of length 0 that are not in L. he only length 0 string is ε. ε is in L since ε = εε, so ε should not be generated by Even. ince Even does not contain any right sides that go to ε, this is correct. Closure Properties of CFLs If A and B are context free languages then: A R is a context-free language RUE A * is a context-free language RUE A is not necessarily a context-free language (complement) A B is a context-free language RUE A B is a context-free language? Left for you to solve (possibly on Exam ) 9 0 Where is English? 0 n n n 0 n n A English he cat likes fish. he cat the dog chased likes fish. he cat the dog the rat bit chased likes fish. his is a pumping lemma proof!
= DFA = CFG Chomsky s Answer (yntactic tructures, 957) Current Answer Most linguists argue that most natural languages are not contextfree But, it is hard to really answer this question: e.g., he cat the dog the rat bit chased likes fish. English? 4 Where is Java? 0 n n n 0 n n A Interpretive Dance 5 6 Where is Java? 0 n n n 0 n n A What is the Java Language? public class est { public static void main(tring [] a) { println("hello World!"); est.java:: cannot resolve symbol symbol : method println (java.lang.tring) // C:\users\luser\est.java public class est { public static void main(tring [] a) { println ("Hello Universe!"); In the Java Language est.java:: illegal unicode escape // C:\users\luser\est.java Not in the Java Language 7 8
// C:\users\luser\est.java public class est { public static void main(tring [] a) { println ("Hello Universe!"); canning error errors tatic semantic errors > javac est.java est.java:: illegal unicode escape // C:\users\luser\est.java est.java:6: 'class' or 'interface' expected est.java:7: 'class' or 'interface' expected est.java:4: cannot resolve symbol symbol : method println (java.lang.tring) location: class est println ("Hello World"); 4 errors Defining the Java Language { w w can be generated by the CFG for Java in the Java Language pecification { w a correct Java compiler can build a parse tree for w 9 0 +M M () number Programming languages are (should be) designed to make parsing easy, efficient, and unambiguous. Derivation + M M M * + * Unambiguous + * () number + * + * + * + * Ambiguity How can one determine if a CFG is ambiguous? uper-duper-challenge problem: create a program that solve the is this CFG ambiguous problem: Input: CFG Output: Yes (ambiguous)/ No (unambiguous) Warning: Undecidable Problem Alert! (Not only can you not do this, it is impossible for any program to do this.) (We will cover undecidable problems after pring Break) +M M () number Programming languages are (should be) designed to make parsing easy, efficient, and unambiguous. Derivation + M M M * + * 4 4
Easy and Efficient Easy - we can automate the process of building a parser from a description of a grammar Efficient the resulting parser can build a parse tree quickly (linear time in the length of the input) Recursive Descent Parse() { (); () { try { (); expect( + ); M(); catch { backup(); try { M(); catch {backup(); error(); M() { try { M(); expect( * ); (); catch try { (); catch { backup(); error (); () { try { expect( ( ); (); expect( ) ); catch ; try { number(); catch ; +M M () number Advantages: Easy to produce and understand Can be done for any CFG Problems: Inefficient (might not even finish) Nondeterministic 5 6 LL(k) (Lookahead-Left) A CFG is an LL(k) grammar if it can be parser deterministically with tokens lookahead +M M () number LL() grammar + + M M + M Look-ahead Parser Parse() { (); () { if (lookahead(, + )) { (); eat( + ); M(); else { M(); M() { if (lookahead(, * )) { M(); eat( * ); (); else { (); () { if (lookahead(0, ( )) { eat( ( ); (); eat( ) ); else { number(); +M M () number 7 8 JavaCC https://javacc.dev.java.net/ Input: Grammar specification Output: A Java program that is a recursive descent parser for the specified grammar Doesn t work for all CFGs: only for LL(k) grammars cheme Language Classes Java Python 0 n n 0 n n n LL(k) Languages Described by LL(k) Grammar 9 0 5
Next Week Monday (): Office Hours (Qi Mi in 6D) Monday (5:0): A help session uesday s class (Pieter Hooimeyer): starting to get outside the yellow circle: using grammars to solve security problems Wednesday (9:0am): Office Hours (Qi Mi in 6D) Wednesday (6pm): As Exam Review hursday: exam in class 6