========================== Study Sheet for Final Exam ========================== ----------------------------------- Logistics - Final is Monday December 12th from 3:30 to 5:30 in G-S 906 (same room as class). - Bring your ID. - Bring a TWO sided 8.5x11 cheat sheet. You will have to submit it. ----------------------------------- 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 LR(k) bottom-up parsing receiver expression activation record 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 PA5 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 full MeggyJava 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. - Monads - Be able to describe what the behavior of a main program that is using a monad. - How might a State Monad be used to simplify the compiler implementation suggested in this class? - Lambda functions: Be able to describe what example usage of lambda functions is doing. LR Parsing Terminology bottom-up parsing Be able to do a shift-reduce parse given a grammar and an input. Symbol Tables What are the various scopes in MeggyJava? Be able to indicate when a variable is visible in a program. Memory model Be able to draw the run-time stack and heap for any MeggyJava translated to AVR program. Register allocation for expressions Using the Sethi-Ulmann algorithm, - show how many registers an expression tree requires - indicate what the order of evaluation on the expression tree needs to be for that number of registers to work ============================================= Example problems - Go find more in HWs and slides and make up your own. - Note that if you want extra credit for submitting a final question that is used in the final, then you need to submit the final question by Wednesday December 7th. - Make sure you know how to answer the questions that were on the midterm. ------------------------------ Memory Model For the below code, draw the RTS and Heap at various points in the execution. After function prologues, before function epilogues, right before calling a function, and right after a function returns. Assume that locals and parameters are being stored on the stack. Show where the frame pointer will be pointing, show where the return address and frame pointer are stored, and show where all of the parameters, locals, and member variables are stored and their current values. For the values of pointers (including the frame pointer and the stack pointer) draw arrows to indicate what address the pointer variable contains. import meggy.Meggy; class PA6fin { public static void main(String[] whatever){ new C().tst(); } } class C { V cMem; public void tst() { V tLoc; cMem = new V(); cMem.put(1,2); tLoc = new V(); tLoc.put(3,4); if ( cMem.get() < tLoc.get() ) { Meggy.setPixel((byte)1, (byte)1, Meggy.Color.GREEN); } else { Meggy.setPixel((byte)1, (byte)1, Meggy.Color.RED); } } } class V { int vMem1; int vMem2; public int get(){ return vMem1+vMem2; } public void put(int v1, int v2){ vMem1=v1; vMem2=v2; } } ------------------------------ LR Parsing Given the following grammar, (a) show the right-most derivation of the string "x + y * z" and (b) show the steps of a shift-reduce parse. E -> E + T | T T -> T * F | F F -> ID Show the ID token as ID(v), where v is the string for the identifier. ===================================== Example problems for Midterm 2, which are still valid to study for the final ------------------------------ - 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 encode the precedence and associativity giving the power operator higher precedence than the concatenation operator. b) Create an AST data structure to represent expressions in the grammar. c) Write a codegen pass that given an AST following the grammar generates a string showing the expression with parentheses showing evaluation order. ------------------------------ 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