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 Spring 2017 Midterm 2

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

CMSC 330, Fall 2018 Midterm 1

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

CMSC 330, Fall 2018 Midterm 1

CMSC330 Fall 2017 Final Exam

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

CMSC330 Fall 2017 Final Exam Solution

Problem Score Max Score 1 Syntax directed translation & type

CMSC330 Fall 2014 Midterm 2 Solutions

CMSC 330: Organization of Programming Languages

CMSC 330 Exam #2 Fall 2008

CMSC330 Spring 2014 Midterm 2 Solutions

Midterm I - Solution CS164, Spring 2014

CS143 Midterm Fall 2008

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

CS164: Midterm I. Fall 2003

Midterm I (Solutions) CS164, Spring 2002

CMSC330 Fall 2009 Final Exam

CMSC330 Spring 2008 Final Exam

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

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

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

CMSC330 Spring 2016 Final Exam

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

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

CSCE 531 Spring 2009 Final Exam

COP4020 Spring 2011 Midterm Exam

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

CMSC 330: Organization of Programming Languages

UNIVERSITY OF CALIFORNIA

ECE251 Midterm practice questions, Fall 2010

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

CMSC 330: Organization of Programming Languages. Operational Semantics

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Languages and Automata

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

Question Points Score

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

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

BSCS Fall Mid Term Examination December 2012

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

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

CMSC330 Fall 2016 Final Exam

Theory Bridge Exam Example Questions Version of June 6, 2008

ECE 468/573 Midterm 1 October 1, 2014

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

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

CMSC330 Spring 2012 Final Exam Solutions

2068 (I) Attempt all questions.

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

CMSC330 Spring 2017 Midterm #1 Solution

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

MIDTERM EXAMINATION. CSE 130: Principles of Programming Languages. Professor Goguen. February 16, points total

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

ECE 468/573 Midterm 1 September 30, 2015

CS 44 Exam #2 February 14, 2001

CS143 Midterm Sample Solution Fall 2010

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

CMSC 330: Organization of Programming Languages

Theory of Computations Spring 2016 Practice Final Exam Solutions

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

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

UNIT -2 LEXICAL ANALYSIS

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

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

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CMSC 330: Organization of Programming Languages

CMSC 330, Fall 2009, Practice Problem 3 Solutions

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

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

CMSC 330: Organization of Programming Languages. Context Free Grammars

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

CMSC330 Fall 2014 Midterm 1 Grading

Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

CSE 582 Autumn 2002 Exam 11/26/02

Syntax-Directed Translation. Lecture 14

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

MIDTERM EXAM (Solutions)

CS 415 Midterm Exam Spring SOLUTION

SLR parsers. LR(0) items

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Structure of a Compiler: Scanner reads a source, character by character, extracting lexemes that are then represented by tokens.

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

CMSC330 Summer 2009 Midterm 1: Ruby and Re~lu[ar Lan~ua~les

Semantic Analysis. Lecture 9. February 7, 2018

In One Slide. Outline. LR Parsing. Table Construction

CSE 5317 Midterm Examination 4 March Solutions

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

Automating Construction of Lexers

Comp 311: Sample Midterm Examination

Question Bank. 10CS63:Compiler Design

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. 2. [1 pts] (T / F) Any language that can be expressed by a context-free grammar can be expressed by a regular expression. 3. [1 pts] (T / F) OCaml is Turing-complete. 4. [1 pts] (T / F) Converting a DFA to an NFA always requires exponential time. 5. [1 pts] (T / F) Recursive descent parsing requires the target grammar to be right recursive. 6. [1 pts] (T / F) The SmallC parser in P4A used recursive descent. 7. [1 pts] (T / F) The call-by-name and call-by-value reduction strategies can produce di erent normal forms for the same expression. 8. [1 pts] (T / F / Decline to Answer) I voted last Tuesday. (All answers are acceptable.) 9. [1 pts] What language feature does the fixed-point combinator implement? (a) Booleans (b) Integers (c) Recursion (d) Closures 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. 2

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}) ={ } [2 pts] move({1},b)={ } 2. [1 pts] (T / F) Every NFA is also a DFA. 3. [1 pts] (T / F) Every DFA is also an NFA. 4. [5 pts] Draw an NFA that corresponds to the following regular expression: ((a? b) (ab))? 3

5. [7 pts] Convert the following NFA into an equivalent DFA. a start 0 1 a a b 2 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 4

3 Context-Free Grammars 1. [6 pts] Write a CFG that is equivalent to the regular expression (wp) + g? 2. [6 pts] Create a CFG that generates all strings of the form a x b y a z,wherey = x + z and x, y, z 0. 5

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 6

4 Parsing 1. [3 pts] Convert the following to a right-recursive grammar. S! S + S A A! A 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 " 7

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 = 8

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 2. [5 pts] (x) ) 5 ; 5 ) 5 ; x ) 5 ; 4 ) 4 let x =5in x ) 5 A; let x =4in let x =5in x ) 5 9

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) 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)) 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. (b) [2 pts] (( x. x x)( y. y y)) (c) [2 pts] (( a. b. a b c) xy) 4. [3 pts] Write an OCaml expression that has the same semantics as the following expression. ( a. b. a b) ( x. x x) y 10

7 Imperative OCaml 1. [7 pts] Given the mut_lst variable, which is a ref list, implementtheadd 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 = 11