CMSC330 Fall 2014 Midterm 1 Solution

Similar documents
CMSC330 Fall 2014 Midterm 1 Grading

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

CMSC330 Spring 2008 Final Exam

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

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

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

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

CMSC330 Spring 2017 Midterm 2

CMSC330 Fall 2014 Midterm 2 Solutions

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

CMSC 330: Organization of Programming Languages

CMSC330 Fall 2017 Final Exam

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

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

CMSC 330, Fall 2018 Midterm 1

CMSC 330, Fall 2018 Midterm 1

CMSC330 Fall 2013 Practice Problems 6 Solutions

CMSC330 Spring 2014 Midterm 2 Solutions

CMSC330 Spring 2017 Midterm #1 Solution

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

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

CS 432 Fall Mike Lam, Professor. Finite Automata Conversions and Lexing

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

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CMSC330 Spring 2012 Final Exam Solutions

CS415 Compilers. Lexical Analysis

Implementation of Lexical Analysis

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

Implementation of Lexical Analysis

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

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

CMSC330 Fall 2010 Final Exam Solutions

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

Midterm I Practice Problems

Implementation of Lexical Analysis

CMSC 330, Fall 2018 Midterm 2

Midterm I review. Reading: Chapters 1-4

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

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Introduction to Lexical Analysis

Programming Languages and Compilers (CS 421)

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

Outline. 1 Scanning Tokens. 2 Regular Expresssions. 3 Finite State Automata

Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

CMSC 330: Organization of Programming Languages. Ruby Regular Expressions

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

HKN CS 374 Midterm 1 Review. Tim Klem Noah Mathes Mahir Morshed

CMSC330 Fall 2009 Final Exam

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

Lexical Analysis 1 / 52

6 NFA and Regular Expressions

CMSC 350: COMPILER DESIGN

CMSC330 Spring 2016 Final Exam

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

CMSC330 Fall 2014 Final Exam

CMSC 330, Fall 2018 Midterm 2

Chapter Seven: Regular Expressions. Formal Language, chapter 7, slide 1

Structure of Programming Languages Lecture 3

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

Regular Languages. MACM 300 Formal Languages and Automata. Formal Languages: Recap. Regular Languages

Formal Languages and Compilers Lecture VI: Lexical Analysis

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CMSC330 Fall 2017 Final Exam Solution

Functional Programming in Haskell Part I : Basics

Lexical Analyzer Scanner

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

Chapter 3: Lexing and Parsing

Lexical Analysis. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Programming Languages and Compilers (CS 421)

UNIT -2 LEXICAL ANALYSIS

n (0 1)*1 n a*b(a*) n ((01) (10))* n You tell me n Regular expressions (equivalently, regular 10/20/ /20/16 4

CMPSCI 250: Introduction to Computation. Lecture 20: Deterministic and Nondeterministic Finite Automata David Mix Barrington 16 April 2013

Lexical Analysis. Lecture 3. January 10, 2018

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

CMSC 330: Organization of Programming Languages. Rust Basics

Sets MAT231. Fall Transition to Higher Mathematics. MAT231 (Transition to Higher Math) Sets Fall / 31

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC 330: Organization of Programming Languages. Operational Semantics

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

Compiler Construction

Lexical Analyzer Scanner

Lexical Analysis. Implementation: Finite Automata

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

ECE 468/573 Midterm 1 September 30, 2015

Finite automata. We have looked at using Lex to build a scanner on the basis of regular expressions.

CSE 401 Midterm Exam Sample Solution 2/11/15

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%

Compiler Construction

CMSC330 Fall 2016 Final Exam

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

NFAs and Myhill-Nerode. CS154 Chris Pollett Feb. 22, 2006.

Lexical Analysis. Introduction

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

Programming Languages

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

ECE 122. Engineering Problem Solving with Java

Transcription:

CMSC330 Fall 2014 Midterm 1 Solution 1. (8 pts) Programming languages (PL) et al. For the following multiple choice questions, circle the letter(s) on the right corresponding to the best answer(s) to each question. a. Which following PL feature(s) is not a part of Ruby? A B C D A) object-oriented programming B) method overloading C) implicit declarations D) dynamic types b. Which value(s) in the guard of an Ruby if statement cause A B C D the then branch to be executed? E.g., if (guard) then x else y end A) true B) false C) nil D) 0 c. Which Ruby feature(s) makes undefined variable errors more likely? A B C D A) code blocks B) regular expressions C) implicit declarations D) dynamic types d. Ruby code blocks are an example of which PL feature(s)? A B C D A) object-oriented programming B) functional programming C) higher-order functions D) imperative programming e. For which PL(s) is expression x == y not testing physical equality? A B C D A) Java B) C C) Ruby D) OCaml f. Which following operation(s) is not essential for regular expressions? A B C D A) + (e.g., a+) B) * (e.g., a*) C). (e.g., ab) D) (e.g., a b) g. Which following feature(s) is not allowed in an NFA? A B C D A) state with multiple transitions for the same label B) epsilon transitions C) multiple final states D) multiple start states h. Which following feature(s) is not allowed in an OCaml expression? A B C D A) lists with different numbers of elements B) lists with different types of elements C) tuples with different numbers of elements D) tuples with different types of elements

2. (10 pts) Ruby. What is the output (if any) of the following Ruby programs? Write FAIL if code does not execute. Output nil for puts x when x is nil (as in Ruby 1.8.7), instead of outputting a blank line (as in Ruby 1.9.3). a. a = 1 OUTPUT = 2 puts a+1 terp a = terp puts a b. a = [ ] OUTPUT = FAIL a[1][2] = 3 puts (4 + a[1][2]) c. a = [ ] OUTPUT = FAIL a[ terp ] = tesudo a[5] = 6 a.each { x puts x } d. Univ of Maryland =~ /([a-z]+)/ OUTPUT = Found niv puts Found #{$1} if $1 puts Found #{$2} if $2 puts Found #{$3} if $3 e. a = Univ of Maryland.scan(/[a-z]+/) OUTPUT = Found niv a.each { x puts Found #{x} } Found of Found aryland 3. (6 pts) Regular expressions and finite automata. a. (2 pts) Write a formal regular expression for all even integers between 10 and 999. (1 2 3 4 5 6 7 8 9)(0 1 2 3 4 5 6 7 8 9 epsilon)(0 2 4 6 8) OR (1 2 3 4 5 6 7 8 9)(0 1 2 3 4 5 6 7 8 9)(0 2 4 6 8) (1 2 3 4 5 6 7 8 9)(0 2 4 6 8) OR /^[1-9][0-9]?[02468]$/ if Ruby RE b. (4 pts) Create a DFA that accepts a string (composed of a s and b s) if and only if it is NOT accepted by the following DFA. Original Complement

4. (4 pts) RE to NFA. Create a NFA for the regular expression R (S*) given the NFAs for R & S below. You must use the method described in lecture. Note that R and S are regular expressions, not symbols in the alphabet. 5. (14 pts) NFA to DFA. Consider the NFA given on the right: a. (2 pts) Does the NFA accept the string aab? If it accepts the string, list a sequence of state transitions (e.g., 1,2,3) that leads to acceptance. Yes. 1,2,3,1,4,5,4 b. (12 pts) Reduce the NFA to a DFA using the subset construction algorithm discussed in class. Be sure to label each state in the DFA with the corresponding state(s) in the NFA.

6. (6 pts) DFA Minimization. Consider applying the Hopcroft DFA minimization algorithm discussed in class to the following DFA. a. (2 pts) What are the initial partition(s) created by the Hopcroft algorithm? P1 { 1,2,5,6 }, P2 { 3,4 } b. (2 pts) Do any partitions need to be split? If yes, what are the resulting partitions after the split? P2 needs to be split. Result = P1 { 1,2,5,6 }, P2 { 3 }, P3 { 4 } c. (2 pts) Is the DFA minimization algorithm finished at this point? Explain. Not finished, since P1 now needs to be split.

7. (40 pts) Ruby programming Implement a Ruby program in roster.rb that reads a student database file where each line contains a student UID, student name, college, and a single major. Your program must process this file and display two lists. The first list displays each college (sorted by college name). For each college, display on a separate line the college name, the number of students in the college, and each student in the college (sorted by student name). The second list displays each student (sorted by student name). For each student, display on a separate line the student s UID, student name, and majors (sorted by major name). Your program will be called with the name of the database file as an argument. For example, running your program on a file called data.txt ( ruby roster.rb data.txt ) could generate: % more data.txt 0002: Cathy,ENGR,Computer Engineering 0004: David,BSOS,Economics 0003: Bob,BSOS,Psychology 0008: Alice,CMNS,Computer Science 0003: Bob,BSO,Economics 0008: Alice,CMNS,Biology 0002: Cathy,CMNS,Math 0005: Ellen,CMNS,Computer Science 0002: Cathy,CMNS % ruby roster.rb data.txt ERROR 0003: Bob,BSO,Economics ERROR 0002: Cathy,CMNS COLLEGES BSOS,2,Bob,David CMNS,3,Alice,Cathy,Ellen ENGR,1,Cathy STUDENTS 0008,Alice,Biology,Computer Science 0003,Bob,Psychology 0002,Cathy,Computer Engineering,Math 0004,David,Economics 0005,Ellen,Computer Science Helpful Functions f = File.new(n, mode) // opens n in mode, returns File f f.eof? // is File object f at end? ln = f.readline // read single line from file f into String ln a = f.readlines // read all lines from file into array a a = str.scan( ) // finds patterns in String str, returns in array a a = h.keys // returns keys in hash h as an array a a.sort // returns sorted version of array a a.sort { x,y... } // sort using code block as comparator a.sort! // sorts elements of array a in place a.size // number of elements in the array a.join(x) // create str by joining array elements separated by str x a.each { } // apply code block to each element in array a.push / a.pop // treat array as stack s.to_i // convert string s to int value ARGV // array containing command line arguments Student UIDs must be 1 or more digits, and are separated from student names by a colon and a space. Student names must be composed of 1 or more lowercase or uppercase characters. College names must be exactly four uppercase letters. Majors must be composed of 1 or more lowercase or uppercase letters, and may include spaces. Student names, college names, and majors are separated by commas. Lines that do not follow this format should produce an error message and otherwise be ignored. Students may have an arbitrary number of majors (e.g., Alice, Cathy). Each student may only be counted once for each college, even if they have multiple majors within the college (e.g., Alice only counts as 1 student for CMNS).You may assume each student has a single unique name & UID. While reading in the file, for each invalid line found, your program should output ERROR followed by the invalid line. Next, it should output COLLEGES, followed by the information for each college (in sorted order by college name). Finally, it should output STUDENTS, followed by the information for each student (in sorted order by student name).

Example Solution: f = File.new(ARGV[0], "r") lines = f.readlines uid = { } # key = student name, value = student UID student = { } # key = student name, value = [ major1, major2, ] college = { } # key = college name, value = { student name1 => true, } lines.each { line if line =~ /^([0-9]+): ([A-Za-z]+),([A-Z]{4}),([A-Za-z ]+)$/ uid[$2] = $1 student[$2] = [ ] if!student[$2] # must allocate Array 1 st time student[$2].push $4 # can store multiple majors in Array college[$3] = { } if!college[$3] # must allocate Hash 1 st time college[$3][$2] = true # duplicate students overwritten (leaves single copy) else puts "ERROR #{line}" end } puts "COLLEGES" college.keys.sort.each { c puts "#{c},#{college[c].size},#{college[c].keys.sort.join(",")}" } puts "STUDENTS" student.keys.sort.each { s puts "#{uid[s]},#{s},#{student[s].sort.join(",")}" } 8. (12 pts) OCaml Types and Type Inference a. (4 pts) Give the type of the following OCaml expressions i. (2 pts) [ [ 2 + 3 ] ] Type = int list list ii. (2 pts) fun x -> [ 3 ] Type = a -> int list b. (4 pts) Write an OCaml expression with the following type i. (2 pts) ( (int list) * int ) list Code = [ ( [ 2 ], 2 ) ] OR [ [ 2 ], 2 ] ii. (2 pts) a list -> a -> a list Code = fun x y -> y::x OR let f x y = y::x c. (4 pts) Give the value of the following OCaml expressions. If an error exists, describe the error. i. (2 pts) let x = 3 in let x = x+2 in x+1 Value / Error = 6 ii. (2 pts) (fun a -> match a with (x::y::z) -> z ) [ 2 ; 3 ; 4 ] Value / Error = [ 4 ]