Midterm Review and Logistics Lukasz Ziarek lziarek@buffalo.edu Midterm Logistics • Assigned seats • To be released Thursday Morning • Multiple versions of the exam • Student ID (or other photo ID) checked when turning in the exam • Designed to be completed in 1 hour • You have 1 hour 20 minutes to do the exam Midterm Structure • Similar in style to the sample questions • Multiple choice • True / False • Short Answer • Not to your benefit to write a novel • No penalty for guessing! • Do not leave questions blank! Midterm Will Cover • Book Chapters 1,3,4,5,6, and part of 7 • Up to and including short circuit evaluation • Attribute Grammar Reference, Section 3.1 • HW1 and HW2 • HW2 solution might not be available depending on late days, no question on specific solutions • Java, SML, and Python Code • Primary focus on SML and some Python • Recitation and Lecture Material all Weeks 4 Midterm Will Not Cover • Chapter 2 • Section 3.5.2 Denotational Semantics • Code examples in languages other than Java, Python, and SML • Challenge problems 5 Office hours on Wednesday • I will be around most of the day to meet with you • 12-4pm Topics From the Book & Readings • • • • • Grammars • Terminal vs Non Terminal • Ambiguous vs Unambiguous • BNF vs EBNF vs Attribute Grammars • Derivations Lexical Analysis Parsing • Generating Parse Trees • Relationship between Grammars and Parsing Binding Scope • Dynamic vs Static • SML vs Python • Scope vs Lifetime • Referencing environments Topics From the Book & Readings • Arrays vs Lists vs Records vs Tuples • Mutable vs immutable • Design decisions of programming languages • Allocation styles and differences between them • Expressions and Assignment • Type conversions • Side-effecting operations Useful Practice Problems From the Book • Chapter 3 Problem Set 2,3,4,6,7,8,11,12,1,16,17,24,25 • Chapter 4 Problem Set: 1,2,5,6 • Chapter 5 Problem Set: 1,2,6,7,8,9,11,12 • Chapter 6 Problem Set: 1,2,9,10,11,18,21,22 • Chapter 7 Problem Set: 4,9,11,12,13,21,22 SML Topics • • • • • • • • • • Recursion Curried functions Pattern Matching References vs immutable values Functions as arguments and return values Types and type errors Foldl and Foldr Let Scoping rules for SML Free variables in functions Python Topics • Lists and iteration • List comprehension • Runtime typing / error detection • Scoping rules for Python • Global variables in Python Sample Question 1 The EBNF rule <S> ::= [ <Y> ] { <X> }+ is equivalent to the following context free grammar: (A) S Y X | Y X S (B) S X | X S | Y X | Y X S (C) S Y | X | X S | Y S (D) S T | Y T T X | XT Consider the following grammar <E> <E> % <E> <E> <E> ^ <E> <E> -<E> <E> (<E>) <E> id Which of the following sentences are valid in the language defined by the above grammar. Assume that any character string is a valid id: -(foo) ^ -(bar) (foo bar) ^ -baz –foo ^ bar ^ (-baz) Sample Question 2 Consider the following two SML functions: fun mystery1 l = foldl (fn(x,a) => a+x) 0 l fun mystery2 l = foldr (fn(x,a) => a+x) 0 l Yes or No: are these two functions equivalent (meaning given any input both will produce the same result)? What is the type of mystery1? What is the type of mystery2? Sample Question 3 Consider the following two SML functions: fun mystery1 l = foldl (fn(x,a) => a@[x]) [] l fun mystery2 l = foldr (fn(x,a) => x::a) [] l Yes or No: are these two functions equivalent (meaning given any input both will produce the same result)? What is the type of mystery1? What is the type of mystery2? Sample Question 4 Consider the following functions: fun foo(x) = (print("foo");x) fun bar(x) = (print("bar");x) fun baz(x) = (print("baz");x) Which of the following SML statements result in the same thing being printed to the screen: 1: val _ = foo bar baz 1 2: val _ = foo bar (baz 1) 3: val _ = foo bar (baz(1)) Sample Question 5 What is the type of function pair? (type inference for curried functions) fun pair x y = (x,y) A: 'a * 'b -> 'a * 'b B: 'a -> 'b -> 'a -> 'b C: 'a -> 'b -> 'a * 'b D: 'a * 'b -> 'a -> 'b