========================== Study Sheet for Midterm 2 ========================== ----------------------------------- Logistics - Midterm is Thursday October 27th, in class. - Bring your ID. - Bring a TWO sided 8.5x11 cheat sheet. You will have to submit it. If you want your other one back Erman has them. ----------------------------------- Practice Problems while studying!! - You will have to work through problems by hand. - While doing examples, feel free to post examples and what answer you think it is on piazza. Give each other feedback. ----------------------------------- Concepts ----------------------------------- Terminology and concepts context-free grammars terminal, non-terminal, symbol syntax-directed translation parse trees abstract syntax trees ambiguity derivation LL(k) top-down parsing precedence associativity left recursion in a grammar left factoring pre and post order depth first traversal LL(1) Parsing - Techniques to make grammars LL(1) - Nullable, First, and Follow sets - Predictive parsing tables - recursive descent parsing functions, should be able to write them in Haskell - Show how to generate an AST while performing recursive descent parsing. How is left associativity put back in? Precedence and Associativity in Expressions - Be able to show more than one parse tree for an ambiguous expression grammar. - Know the relative precedence levels for all of the operators in PA3 MeggyJava. - Be able to provide example expressions and show the correct associativity and precedence by placing parentheses to show order of evaluation. - Show how to add precedence and associativity to a grammar. - Show how to remove left recursion from that grammar. Dangling Else problem - What is the problem? - What steps can we take to solve it for LL(1) parsing? Parse Trees and Abstract Syntax Trees - Given a grammar and an input list of tokens, show the parse tree. - Given a parse tree for MeggyJava, show the corresponding AST. Type checking - Write Haskell code for type checking a language construct. The language construct can be an expression or a statement. The language construct may or may not be in MeggyJava, but will be in Java. You will have to indicate your assumptions about the AST data structure wrt that language construct. Code Generation - Be able to show the Haskell code and/or explain how to generate code for any of the program constructs in the MeggyJava PA3 grammar. Synthesis - Given an ambiguous grammar for some little language, describe all of the steps needed to create a compiler that translates that little language into AVR assembly code. Haskell - Be able to answer any questions about what Haskell code is doing. If the construct has occurred in any of the recitation examples, then it could be in a midterm question. - Be able to write from scratch a Haskell function that can traverse a tree in pre, post, or in order. Parsing With Derivatives - Given a grammar and a token, compute the derivative of that grammar wrt the token. - What is an acceptor? - Given an input and a grammar what does an acceptor built on the above concept of a derivative do? ============================================= Example problems - Go find more in HWs and slides and make up your own. - Note that if you want extra credit for submitting a midterm question that is used in the midterm, then you need to submit the midterm question by this Friday 4pm (October 21st). - See the example questions in Friday's recitation. ------------------------------ - Assume a small scripting language that manipulates strings. To concatenate strings s1 and s2, use the + operator: s1 + s2 The power operator concatenates the same string the specified number of times. s1 ^ 2 a) Write a grammar for this language and use the JavaCUP precedence keywords to give s1 the power operator higher precedence than the concatenation operator. b) Associate actions with the grammar rules that print out the final string to standard out. Assume that the type for the string tokens is the Java String type. ------------------------------ For the following grammar, construct a recursive-descent parser. S -> 0 S 1 | 0 1 What did we have to do to make this grammar LL(1)? ---------- Fix the precedence and associativity in the below grammar. Assume ^ is right associative and of higher precedence than +. E -> E ^ E | E + E | ( E ) | NUM