CMSC 330, Fall 2018 Midterm 2

Similar documents
CMSC 330, Fall 2018 Midterm 2

CMSC330 Spring 2018 Midterm 2 9:30am/ 11:00am/ 3:30pm

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

CMSC330 Spring 2017 Midterm 2

CMSC330 Spring 2016 Midterm #2 9:30am/12:30pm/3:30pm

CMSC330 Fall 2017 Final Exam

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

CMSC 330, Fall 2018 Midterm 1

CMSC330 Fall 2017 Final Exam Solution

CMSC 330, Fall 2018 Midterm 1

CMSC330 Fall 2014 Midterm 2 Solutions

CMSC330 Spring 2014 Midterm 2 Solutions

CMSC 330: Organization of Programming Languages

Denotational Semantics; Lambda Calculus Basics Section and Practice Problems Week 4: Tue Feb 13 Fri Feb 16, 2018

Problem Score Max Score 1 Syntax directed translation & type

Midterm I (Solutions) CS164, Spring 2002

Midterm I - Solution CS164, Spring 2014

UVa ID: NAME (print): CS 4501 LDI Midterm 1

CMSC330 Spring 2012 Final Exam Solutions

CMSC 330 Exam #2 Fall 2008

CMSC330 Spring 2016 Final Exam

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

CMSC 330: Organization of Programming Languages. Operational Semantics

COP4020 Spring 2011 Midterm Exam

CMSC330 Spring 2015 Final Exam 9:30am/11:00am/12:30pm

ECE 468/573 Midterm 1 October 1, 2014

CS143 Midterm Fall 2008

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

CMSC330 Spring 2008 Final Exam

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

CS164: Midterm I. Fall 2003

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

ECE 468/573 Midterm 1 September 30, 2015

CMSC330 Fall 2009 Final Exam

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

Concepts of programming languages

CMSC 330: Organization of Programming Languages

CMSC330 Fall 2016 Final Exam

1. [5 points each] True or False. If the question is currently open, write O or Open.

CSCE 531 Spring 2009 Final Exam

CSE-321 Programming Languages 2012 Midterm

CMSC330 Fall 2015 Midterm #1 12:30pm/2:00pm/5:00pm

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

BSCS Fall Mid Term Examination December 2012

Theory Bridge Exam Example Questions Version of June 6, 2008

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

1. Consider the following program in a PCAT-like language.

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

CMSC330 Spring 2017 Midterm #1 Solution

Type Systems Winter Semester 2006

CIS 500 Software Foundations Midterm I

UNIVERSITY OF CALIFORNIA

Question Points Score

ECE251 Midterm practice questions, Fall 2010

CS154 Midterm Examination. May 4, 2010, 2:15-3:30PM

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Midterm 2. CMSC 430 Introduction to Compilers Fall 2018

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Languages and Automata

In One Slide. Outline. LR Parsing. Table Construction

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

SLR parsers. LR(0) items

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

CMSC330 Spring 2016 Midterm #1 9:30am/12:30pm/3:30pm

Question Marks 1 /12 2 /6 3 /14 4 /8 5 /5 6 /16 7 /34 8 /25 Total /120

CMSC 330, Fall 2013, Practice Problems 3

CSE 505: Concepts of Programming Languages

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

CMSC 330: Organization of Programming Languages. Context Free Grammars

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

CSE 505, Fall 2006, Final Examination 11 December Please do not turn the page until everyone is ready.

CS143 Midterm Sample Solution Fall 2010

CMSC 330: Organization of Programming Languages. Lambda Calculus

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Theory of Computations Spring 2016 Practice Final Exam Solutions

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

CMSC330 Spring 2015 Midterm #1 9:30am/11:00am/12:30pm

CSC-461 Exam #2 April 16, 2014

CMSC 330, Fall 2009, Practice Problem 3 Solutions

CMSC 330: Organization of Programming Languages

CMSC330 Spring 2017 Midterm #1 9:30am/11:00am/12:30pm/5:00pm

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

CIS 500 Software Foundations Fall September 25

1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004

CS 406/534 Compiler Construction Putting It All Together

CMSC 330: Organization of Programming Languages. Context Free Grammars

Transcription:

CMSC 330, Fall 2018 Midterm 2 Name Teaching Assistant Kameron Aaron Danny Chris Michael P. Justin Cameron B. Derek Kyle Hasan Shriraj Cameron M. Alex Michael S. Pei-Jo Instructions Do not start this exam until you are told to do so. You have 75 minutes for this exam. This is a closed book exam. No notes or other aids are allowed. For partial credit, show all your work and clearly indicate your answers. Honor Pledge Please copy and sign the honor pledge: I pledge on my honor that I have not given or received any unauthorized assistance on this examination. Section Points Programming Language Concepts 10 Finite Automata 23 Context-Free Grammars 18 Parsing 18 Operational Semantics 11 Lambda Calculus 13 Imperative OCaml 7 Total 100 1

1 Programming Language Concepts In the following questions, circle the correct answer. 1. [1 pts] (T / F) The input to a lexer is source code and its output is an abstract syntax tree. False. 2. [1 pts] (T / F) Any language that can be expressed by a context-free grammar can be expressed by a regular expression. False. 3. [1 pts] (T / F) OCaml is Turing-complete. True. 4. [1 pts] (T / F) Converting a DFA to an NFA always requires exponential time. False. 5. [1 pts] (T / F) Recursive descent parsing requires the target grammar to be right recursive. True. 6. [1 pts] (T / F) The SmallC parser in P4A used recursive descent. True. 7. [1 pts] (T / F) The call-by-name and call-by-value reduction strategies can produce different normal forms for the same λ expression. False. 8. [1 pts] (T / F / Decline to Answer) I voted last Tuesday. (All answers are acceptable.) Any. 9. [1 pts] What language feature does the fixed-point combinator implement? (a) Booleans (b) Integers (c) Recursion (d) Closures 2

(c) 10. [1 pts] What is wrong with this definition of an NFA? type ( q, s) nfa = { qs : q list; sigma : s list; delta : ( q, s) transition list; q0 : q list; fs : q list; } (a) Allows states with multiple transitions on the same character. (b) Allows ε-transitions. (c) Allows multiple final states. (d) Allows multiple start states. (d) 3

2 Finite Automata b start 0, a 1 c b, b a 2 3 d 1. Use the NFA shown above to answer the following questions. [2 pts] ε-closure({0}) = { } ε-closure({0}) = {0, 1, 3} [2 pts] move({1}, b) = { } move(1, b) = {2, 3} 2. [1 pts] (T / F) Every NFA is also a DFA. False 3. [1 pts] (T / F) Every DFA is also an NFA. True 4. [5 pts] Draw an NFA that corresponds to the following regular expression: ((a b) (ab)) 4

start a b a b 5. [7 pts] Convert the following NFA into an equivalent DFA. a start 0 1 a a b 2 3 b 5

a start 0 a 1, 2 a b 1, 2, 3 b 1, 3 b 6. [5 pts] Circle all of the strings that will be accepted by the above NFA. (Note: Not the DFA you generated) (a) abbaa (b) aaaa (c) abbaabb (d) abbbbaab (e) aaaaa (a), (c), (e) 6

3 Context-Free Grammars 1. [6 pts] Write a CFG that is equivalent to the regular expression (wp) + g S XY X wpx wp Y gx ε 2. [6 pts] Create a CFG that generates all strings of the form a x b y a z, where y = x + z and x, y, z 0. 7

S UL U aub ε L bla ε 3. [6 pts] Given the following grammar, where S and A denote non-terminals, give a right-most and left-most derivation of ((100, 33), 30). Show all steps of your derivation. S A (S, S) A 100 33 30 Left-most, S (S, S) ((S, S), S) ((100, S), S) ((100, 33), S) ((100, 33), 30)) Right-most, S (S, S) (S, S) (S, 30)) (S, 30) ((S, S), 30) ((S, 33), 30) ((100, 33), 30) 8

4 Parsing 1. [3 pts] Convert the following to a right-recursive grammar. S S + S A A A A B B n (S) S A + S A A B A B B n (S) 2. [5 pts] What are the first sets of the non-terminals in the following grammar? S bc ca A cad B B ws ε 9

first(s) = {b, c} first(a) = {c, w, ε} first(b) = {w, ε} 3. [10 pts] Finish the definition of a recursive descent parser for the grammar below. You need not build an AST, assume all methods return unit. Note that match_tok takes a string. S Abc A A cad e let lookahead () : string = match!tok_list with [] -> raise (ParseError "no tokens") h::t -> h let match_tok (a : string) : unit = match!tok_list with h::t when a = h -> tok_list := t _ -> raise (ParseError "bad match") let rec parse_s () : unit = and parse_a () : unit = 10

let rec parse_s () : unit = let () = parse_a () in if lookahead () = "b" then let () = match_tok "b" in match_tok "c" else () and parse_a () : unit = if lookahead () = "c" then let () = match_tok "c" in let () = parse_a () in match_tok "d" else if lookahead () = "e" then match_tok "e" else raise (ParseError "parse_a") 11

5 Operational Semantics A; false false A; true true A; n n A(x) = v A; x v A; e 1 v 1 A, x : v 1 ; e 2 v 2 A; let x = e 1 in e 2 v 2 A; e 1 n 1 A; e 2 n 2 n 3 is n 1 + n 2 A; e 1 + e 2 n 3 A; e 1 true A; e 2 v A; if e 1 then e 2 else e 3 v A; e 1 false A; e 3 v A; if e 1 then e 2 else e 3 v Use the above rules to fill in the given constructions. 1. [6 pts] A; A; A; A; A; if then 10 + 3 else 5 + 2 7 A; 5 5 A; 2 2 7 is 5 + 2 A; false false A; 5 + 2 7 A; if false then 10 + 3 else 5 + 2 7 2. [5 pts] (x) 5 ; 5 5 ; x 5 ; 4 4 let x = 5 in x 5 A; let x = 4 in let x = 5 in x 5 12

A ; 4 4 A, x = 4 ; 5 5 A, x = 4, x = 5 (x) 5 A, x = 4, x = 5 ; x 5 A, x = 4 let x = 5 in x 5 A; let x = 4 in let x = 5 in x 5 13

6 Lambda Calculus 1. [2 pts] Circle all of the free variables in the following λ expression. (A variable is free if it is not bound by a λ abstraction.) x (λx. (λy. λz. x y z) y) x (λx. (λy. λz. x y z) y) 2. [2 pts] Circle all of the following where the λ expressions are α-equivalent. (a) ((λa. (λy. y a) y) and (λx. x y) (b) (λx. (λy. x y)) and (λy. (λx. y x)) The (a) expressions not α-equivalent, but the (b) expressions are. 3. Reduce each λ expression to β-normal form (to be eligible for partial credit, show each reduction step). If already in normal form, write normal form. If it reduces infinitely, write reduces infinitely. (a) [2 pts] x (λa. λb. b a) x (λy. y) Hint: application is left-associative. Normal Form (b) [2 pts] ((λx. x x)(λy. y y)) ((λx. x x)(λy. y y)) β ((λy. y y)(λy. y y)) β ((λy. y y)(λy. y y)) β... Reduces Infinitely 14

(c) [2 pts] ((λa. λb. a b c) x y) ((λa. λb. a b c) x y) β ((λb. x b c) y) β (x y c) 4. [3 pts] Write an OCaml expression that has the same semantics as the following λ expression. (λa. λb. a b) (λx. x x) y (fun a -> fun b -> a b) (fun x -> x x) y 15

7 Imperative OCaml 1. [7 pts] Given the mut_lst variable, which is a ref list, implement the add and contains functions which should add a given element to mut_lst and check if the mut_lst contains a specified element, respectively. You may add helpers and change the functions to be recursive. let mut_lst = ref [] let add (ele : a) : unit = let contains (ele : a) : bool = let add (ele : a) : unit = mut_lst := ele :: (!mut_lst) let contains (ele : a) : bool = 16

let rec helper lst = match lst with [] -> false h :: t -> if (h = ele) then true else (helper t) in helper!mut_lst 17