Review Questions for Midterm I. To be discussed in class on Wednesday Feb 3. Midterm I (Friday Feb 5) will cover HW1-3, slides so far, and these questions. 0. The only Turing-Award winner from this state is: _____ He invented the __________ (something you use everyday). He graduated from _______ (choose one: OSU / UO). The only other Turing-Award winner who has lived in this state is: _____ He invented the _____ language and co-invented the ______ language. He also co-invented the BNF notation for the _____ invented by ____. Lex was co-authored by _____ who was the CEO of _____. He wrote Lex during his internship with _____ who is the author of the compiler textbook ("dragon book"). 1. Write regular expressions for (a) Python variable names (b) integers (c) simple float numbers (i.e., no scientific notation) 2. Are the following grammars ambiguous? If so, demonstrate. (a) E -> E + E | int (b) E -> E + (E) | int 3. For this grammar: E -> E + E | E * E | int (a) demonstrate two types of ambiguities (precedence and associativity). (b) write a grammar that eliminates the precedence ambiguity only. (c) write a grammar that eliminates the associativity ambiguity only, where both + and * are right-associative, i.e., a+b+c = a+(b+c). (d) write a grammar that eliminates both ambiguities, where + is left-associative, * is right-associative, and + has lower precedence than *. 4. Demonstrate shift-reduce conflict for (a) E -> E + E | int (b) E -> E + E | E -> E * E | int (c) S -> if E then S | if E then S else S | print "5" E -> True | False 5. The Ls and Rs in LL and LR mean: (a) left-to-right scanning (b) right-to-left scanning (c) left-most derivation (d) right-most derivation LL and LR are: (a) shift-reduce (b) recurive descent (c) bottom-up (d) top-down In practice, Python is parsed by ___ and C/C++/Java by ___. Why there is no "right-to-left" parsing for programming languages? 6. Reduce-reduce conflict is due to ________. Give an example using P_2. 7. If in a shift-reduce conflict, the parser always prefers reduce, the result will be left-associative or right-associative? Demonstrate. 8. Redo the Python expr to LISP example from week1 slides, using the new ast instead of the old compiler package. 9. Consider the following grammar P_0.8: module : stmt+ stmt : (assign_stmt | print_stmt) NEWLINE assign_stmt : name "=" decint print_stmt : "print" name Now you need to add a simple "while" statement, e.g.: a = 0 while a < 10: a += 1 print a For simplicity we only consider: (a) variable initialization to an integer outside of loop (but not in a loop) (b) variable increment (by an integer) or print_stmt inside loop (i.e., no nested loops) (c) single comparison in the form of "variable < integer" for the condition Now you need to: (1) Write additional CFG rules (2) Write additional code for lex (3) Write additional code for yacc (4) Write additional code to convert to C. The solution to P_0.8 (lex, yacc, translation) is in addwhile.py.