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

Similar documents
Midterm I - Solution CS164, Spring 2014

Midterm I (Solutions) CS164, Spring 2002

CS164: Midterm I. Fall 2003

CS143 Midterm Spring 2014

CS143 Midterm Fall 2008

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

Lexical Analysis. Chapter 2

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

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

CMSC 330, Fall 2018 Midterm 2

CMSC 330, Fall 2018 Midterm 2

Lexical Analysis. Lecture 2-4

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

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

CS 164 Programming Languages and Compilers Handout 8. Midterm I

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

CS164 Second Midterm Exam Fall 2014

Introduction to Lexical Analysis

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Lexical Analysis. Lecture 3-4

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

CS143 Midterm Sample Solution Fall 2010

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

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

CSE 401 Midterm Exam Sample Solution 2/11/15

Lexical Analysis. Finite Automata

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Formal Languages and Compilers Lecture VI: Lexical Analysis

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

Lexical Analysis. Finite Automata

14.1 Encoding for different models of computation

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

ECE 468/573 Midterm 1 September 30, 2015

Cunning Plan. Informal Sketch of Lexical Analysis. Issues in Lexical Analysis. Specifying Lexers

A simple syntax-directed

Lexical Analysis. Lecture 3. January 10, 2018

CMSC 330: Organization of Programming Languages

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

Lexical Analysis. Implementation: Finite Automata

Compiler Construction

CMSC330 Fall 2014 Midterm 2 Solutions

UNIVERSITY OF CALIFORNIA

EECS 481 Software Engineering Exam #1. You have 1 hour and 20 minutes to work on the exam.

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

Implementation of Lexical Analysis

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

Question Bank. 10CS63:Compiler Design

2068 (I) Attempt all questions.

CS/ECE 374 Fall Homework 1. Due Tuesday, September 6, 2016 at 8pm

Part 5 Program Analysis Principles and Techniques

Regular Expressions. Regular Expressions. Regular Languages. Specifying Languages. Regular Expressions. Kleene Star Operation

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

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

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

Introduction to Lexing and Parsing

Semantic Analysis. Lecture 9. February 7, 2018

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

Lexical Analysis 1 / 52

CMSC330 Spring 2017 Midterm 2

Implementation of Lexical Analysis

Introduction to Lexical Analysis

CS143 Handout 20 Summer 2012 July 18 th, 2012 Practice CS143 Midterm Exam. (signed)

CS 314 Principles of Programming Languages. Lecture 3

DVA337 HT17 - LECTURE 4. Languages and regular expressions

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

Administrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis

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

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

CMSC330 Spring 2014 Midterm 2 Solutions

Introduction to Parsing Ambiguity and Syntax Errors

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

CSCE 314 Programming Languages

Chapter 2 - Programming Language Syntax. September 20, 2017

LR Parsing LALR Parser Generators

Syntax. 2.1 Terminology

Action Table for CSX-Lite. LALR Parser Driver. Example of LALR(1) Parsing. GoTo Table for CSX-Lite

Introduction to Parsing Ambiguity and Syntax Errors

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

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

QUESTIONS RELATED TO UNIT I, II And III

Syntax and Grammars 1 / 21

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

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

UNIT -2 LEXICAL ANALYSIS

Chapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

CSE 401 Midterm Exam 11/5/10

2010: Compilers REVIEW: REGULAR EXPRESSIONS HOW TO USE REGULAR EXPRESSIONS

CPS 506 Comparative Programming Languages. Syntax Specification

CMSC 330: Organization of Programming Languages

CMSC 350: COMPILER DESIGN

INSTITUTE OF AERONAUTICAL ENGINEERING

CMSC 330: Organization of Programming Languages. Context Free Grammars

Compilers and computer architecture From strings to ASTs (2): context free grammars

Introduction to Parsing. Lecture 5

Theory Bridge Exam Example Questions Version of June 6, 2008

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Transcription:

CS 4501 LDI Midterm 1 Write your name and UVa ID on the exam. Pledge the exam before turning it in. There are nine (9) pages in this exam (including this one) and six (6) questions, each with multiple parts. Some questions span multiple pages. All questions have some easy parts and some hard parts. If you get stuck on a question move on and come back to it later. You have 1 hour and 20 minutes to work on the exam. The exam is closed book, but you may refer to your two page-sides of notes. Even vaguely looking at a cellphone or similar device (e.g., tablet computer) during this exam is cheating. Please write your answers in the space provided on the exam, and clearly mark your solutions. You may use the backs of the exam pages as scratch paper. Please do not use any additional scratch paper. Solutions will be graded on correctness and clarity. Each problem has a relatively simple and straightforward solution. We might deduct points if your solution is far more complicated than necessary. Partial solutions will be graded for partial credit. Good Writing Example: Python and Ruby have implemented some Smalltalkinspired ideas with a more C-like syntax. Bad Writing Example: Im in ur class, @cing ur t3stz!1! If you leave a non-extra-credit portion of the exam blank, you will receive onethird of the points for that small portion (rounded down) for not wasting our time. If you randomly guess and throw likely words at us, we will be much less sanguine. UVa ID: KEY NAME (print): ANSWER KEY 1

UVa ID: (yes, again!) Problem Max points Points 1 Compiler Stages 11 2 OCaml Functional Programming 11 3 Python Functional Programming 10 4 Regular Expressions 28 5 Ambiguity 12 Honor Pledge: 6 Earley Parsing 28 Extra Credit 0 TOTAL 100 How do you think you did? Page 2

1 Compiler Stages (11 points) Fill in each blank with the number corresponding to the best, most precise answer from the answer bank. Each answer will be used at most once. Each blank is worth one point. 5 Takes as input Regular Expressions. 12 Is produced by Compilers but not Interpreters. 11 Rejects programs that add integers to strings. 7 Rejects programs with unbalanced parentheses. 9 Is used as input by a Lexer. 2 Represents the program structure and expression types. 3 Is used as input by a Parser Generator. 10 Specifies which strings correspond to Tokens. 8 Accepts precedence and associativity annotations. 1 Is used as input by a Type Checker. 4 Produces Tokens as output. Answer bank: 1. Abstract Syntax Tree 2. Annotated Abstract Syntax Tree 3. Context-Free Grammar 4. Lexical Analyzer (Lexer) 5. Lexical Analyzer Generator 6. Optimizer 7. Parser 8. Parser Generator 9. Program (Cool) Source Code 10. Regular Expressions 11. Semantic Analyzer (Type Checker) 12. Stand-Alone Executable 13. Tokens 14. Typing Rules Page 3

2 OCaml Functional Programming (11 points) Consider the following OCaml functions. The functions are correct and behave as specified; these are all direct copies of standard library functions. (You may use others too!) (* map f [a1;...; an] applies function f to a1,..., an, and builds the list [ f a1;...; f an] with the results returned by f. *) let rec map f lst = match lst with [] -> [] hd :: tl -> (f hd) :: ( map f tl) (* filter p l returns all the elements of the list l that satisfy the predicate p. *) let rec filter p l = match l with [] -> [] hd :: tl - > if p hd then hd :: ( filter p tl) else ( filter p tl) (* fold_left f a [b1;...; bn ]] is f (... (f (f a b1) b2)...) bn. *) let rec fold_left f accu lst = match lst with [] -> accu hd :: tl - > fold_ left f ( f accu hd) tl (* mem a lst is true if and only if a is equal to an element of lst. *) let rec mem a lst = match lst with [] -> false hd :: tl -> (hd = a) ( mem a tl) let is_ odd n = ( n mod 2) = 1 (* returns true if n is odd *) Complete each of the following functions by filling in each blank with a single identifier, keyword, operator or constant. You must write well-typed functional programs. (* count_odds returns the # of odd elements in its input list. *) let count_ odds x = _ LENGTH_ ( _ FILTER_ is_ odd x) (* intersect takes two lists as arguments and returns a list of all elements they both contain. *) let intersect a b = filter ( fun x - > _ MEM X B_ ) a (* reaches takes a list string - string pairs representing the directed edges in a graph and returns the set of all nodes reachable from node "A" ( including "A" itself ). *) let reaches edges = let rec helper reached edges = match edges with [] -> reached ( a, b) :: tl - > if mem _A_ reached && not ( mem _B_ reached ) then ( helper ( _B_ :: reached ) edges ) else helper _ REACHED TL_ in helper [_"A"_] edges Page 4

3 Python Functional Programming (10 points) Consider a function is dfa for determining if a finite state machine description corresponds to a DFA. Recall that a DFA has no epsilon transitions. Recall that a DFA never has two edges leaving the same state with the same label going to different destination states. For example, consider the DFA accepting the regular language denoted by the regular expression c a(aa) b below: edges = [ ("q0", "a", "q1"), # in state q0, on a, goto q1 ("q0", "c", "q2"), # in state q0, on c, goto q2 ("q1", "b", "q2"), # in state q1, on b, goto q2 ("q1", "a", "q0") ] # in state q1, on a, goto q0 final = [ "q2" ] start = "q0" That DFA accepts ab, c and aaab but neither a nor aab. However, if we were to add either or both of the following edges: ("q0", "", "q1"), # in state q0 epsilon transition to q2 ("q1", "b", "q0"), # in state q1, on b, goto q0 the result would not be a DFA. As a reminder, here is the list comprehension syntax: print [ x* x for x in range ( 10) if x > 5] # [36, 49, 64, 81] Complete the following function by filling in each blank with a single identifier, keyword, operator or constant. You must write a correct Python program. def is_dfa ( edges, final, start ): any_epsilon = _LEN_ ([ b for (a,b,c) in edges \ if _B_ == _""_ ]) > 0 # Helper function to determine if another edge # is leaving the same state on the same character # but reaching a different destination. def another (a,b,c): return len ([ x for (x,y,z) in _EDGES_ \\ if x == _A_ and y == _B_ and \\ z _!= C_ ]) > 0 edge_problem = len ([ a for (a,b,c) in _EDGES_ \\ if _ANOTHER_ (a,b,c) ]) > 0 return not ( any_epsilon or edge_problem ) Page 5

4 Regular Expressions and Automata (28 points) For this question, the regular expressions are single character (a), epsilon (ɛ), concatenation (r 1 r 2 ), disjunction (r 1 r 2 ), Kleene star r, plus r+ and option r?. (a) (7 pts.) Write a regular expression (over the alphabet Σ = {a, b, c}) for the language of strings that have all of the letters in order and an odd number of occurrences of c. Use at most 20 symbols in your answer. a* b* c(cc)* (b) (7 pts.) Draw an NFA (or DFA) that accepts the language from the above problem. Use at most four states in your answer. /-\ e /-\ e /-\ c /===\ -> ---> ---> ---> \-/ \-/ \-/ \===/ ^ ^ ^ \-------/ \-/ \-/ c a b (c) (2 pt.) NEVER. The set of all strings over Σ = {a, b} that contain more occurrences of a than b is regular. (d) (2 pt.) ALWAYS. A finite language is both regular and context free. (e) (2 pt.) SOMETIMES. An infinite language is regular. (f) (2 pt.) SOMETIMES. Given a DFA d, there exists a regular expression r containing neither + nor such that L(d) = L(r). (g) (2 pt.) NEVER. The set of valid Cool programs is context free. (h) (2 pt.) SOMETIMES. Given an NFA n, there is a DFA d such that L(d) = {ss s L(n)}. (i) (2 pt.) ALWAYS. Given a DFA d, there is an NFA n such that L(n) = {rs r L(d) s L(d)}. Page 6

5 Ambiguity (12 points) Consider the following grammar G 1. S E E int E E + E E E E (E) (a) (3 pts.) Show that this grammar is ambiguous using the string int - int - int. (int - int) - int vs. int - (int - int) (b) (9 pts.) Rewrite the grammar to eliminate left recursion. That is, provide a grammar G 2 such that L(G 1 ) = L(G 2 ) but G 2 admits no derivation X Xα. S E E int F E (E) F F + E E ɛ Page 7

6 Earley Parsing (28 points) (a) (23 pts.) Complete the Earley parsing chart (parsing table) on the next page. (b) (2 pts.) Give one disadvantage of an LALR(1) parser generator. Give one disadvantage of an Earley parser. Do not exceed four sentences. LALR(1) only accept a subset of all context-free grammars, may require you to deal with shift/reduce and reduce/reduce conflicts, may require you to rewrite the grammar in an unnatural manner. Earley can require up to cubic time to parse. (c) (1 point if not blank.) What s your favorite thing about this class? (If you re in Compilers, answer for Compilers as well.) (d) (1 point if not blank.) What s your least favorite thing about this class? (If you re in Compilers, answer for Compilers as well.) (e) (1 point if not blank.) Which trivia topics would you like to see discussed during breaks in class? (f) Extra Credit (at most 2 points). Cultural literacy. Below are the English titles of ten important works of world literature or oral tradition. Each work is associated with one of the ten most common languages (by current number of first-language speakers; Ethnologue 2015 estimate). For each work, give the associated language. Be specific. ENGLISH Jayne Eyre. HINDI (SANSKRIT) Lake of the Deeds of Rama. SPANISH One Hundred Years of Solitude. ARABIC One Thousands and One Nights. RUSSIAN The Brothers Karamazov. PORTUGUESE The Crime of Father Amaro. MANDARIN (CHINESE) The Dream of the Red Chamber. JAPANESE The Tale of Genji. JAVANESE (MALAYSIAN) Wayang Kulit. BENGALI Where the Mind is Without Fear. Page 8

T E + E + E Grammar E int ( A A int A ) ε Input int + ( int ) + int int + ( int ) + chart [0] chart [1] chart [2] chart [3] chart [4] chart [5] chart [6] chart [7] int T E+E+E,0 E int,0 T E+ E+E,0 E ( A,2 A int A,3 A ),4 T E+E+ E,0 E int,6 E int,0 T E +E+E,0 E int,2 A int A,3 A int A,4 A int A,3 E int,6 T E+E+E,0 E ( A,0 E ( A,2 A ),3 A ),4 E (A,2 E ( A,6 A,3 A,4 T E+E +E,0 E (A,2 A int A,3 T E+E +E,0 E ( A,2 T E+E +E,0 Page 9