CS164 Second Midterm Exam Fall 2014

Similar documents
Midterm I - Solution CS164, Spring 2014

Midterm I (Solutions) CS164, Spring 2002

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

CS164: Midterm I. Fall 2003

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

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

CS143 Midterm Fall 2008

CSE 401 Midterm Exam 11/5/10

CS164 First Midterm Exam Fall 2014

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

CS 164 Programming Languages and Compilers Handout 8. Midterm I

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

ECE251 Midterm practice questions, Fall 2010

Question Points Score

BSCS Fall Mid Term Examination December 2012

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

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

Lecture 3: Lexical Analysis

CSCI Compiler Design

CSE 5317 Midterm Examination 4 March Solutions

CS143 Midterm Spring 2014

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

CT32 COMPUTER NETWORKS DEC 2015

Part I: Multiple Choice Questions (40 points)

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

Introduction to Lexing and Parsing

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

Theory Bridge Exam Example Questions Version of June 6, 2008

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

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

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

CS143 Midterm Sample Solution Fall 2010

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

1 Parsing (25 pts, 5 each)

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

Midterm Solutions COMS W4115 Programming Languages and Translators Wednesday, March 25, :10-5:25pm, 309 Havemeyer

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

ECS 120 Lesson 7 Regular Expressions, Pt. 1

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

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

ECE468 Fall 2003, Final Exam

CS164: Programming Assignment 2 Dlex Lexer Generator and Decaf Lexer

CSE P 501 Exam 8/5/04

Converting a DFA to a Regular Expression JP

6 NFA and Regular Expressions

CS164: Final Exam. Fall 2003

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

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

Chapter 2 - Programming Language Syntax. September 20, 2017

Midterm Exam. CS169 Fall November 17, 2009 LOGIN: NAME: SID: Problem Max Points Points TOTAL 100

ECE 468/573 Midterm 1 September 30, 2015

CSE344 Midterm Exam Winter 2017

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

Optimizing Finite Automata

Course Project 2 Regular Expressions

COP 3402 Systems Software Syntax Analysis (Parser)

Lexical Analysis 1 / 52

CS 115 Exam 1, Fall 2015 Thu. 09/24/2015

Midterm II CS164, Spring 2006

Lecture 4: Syntax Specification

Formal Languages. Formal Languages

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

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

CMSC 330, Fall 2018 Midterm 2

Specifying Syntax COMP360

Final Exam CS164, Fall 2007 Dec 18, 2007

Decision Properties of RLs & Automaton Minimization

Languages and Automata

UNIVERSITY OF CALIFORNIA

Automating Construction of Lexers

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

2068 (I) Attempt all questions.

This book is licensed under a Creative Commons Attribution 3.0 License

UNIT -2 LEXICAL ANALYSIS

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

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

CS 406/534 Compiler Construction Putting It All Together

CS308 Compiler Principles Lexical Analyzer Li Jiang

Compiler Construction Using

CS143 Final Fall 2009

COMP Logic for Computer Scientists. Lecture 25

MIDTERM EXAMINATION - CS130 - Spring 2003

Intro To Parsing. Step By Step

CSE 401 Midterm Exam Sample Solution 2/11/15

Lexical Analysis - 2

CSE 582 Autumn 2002 Exam 11/26/02

CS606- compiler instruction Solved MCQS From Midterm Papers

CS 415 Midterm Exam Spring SOLUTION

Formal Languages and Compilers Lecture VI: Lexical Analysis

CMSC330 Spring 2017 Midterm 2

(Refer Slide Time: 0:19)

Syntax Analysis Part I

Languages and Compilers

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

CSE 401 Midterm Exam Sample Solution 11/4/11

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

CMSC 330, Fall 2009, Practice Problem 3 Solutions

CS 536 Midterm Exam Spring 2013

Transcription:

CS164 Second Midterm Exam Fall 2014 November 24 th, 2014 Please read all instructions (including these) carefully. This is a closed-book exam. You are allowed a one-page, one-sided handwritten cheat sheet. Write your name and login on this first sheet, and your login at the top of each sheet. No electronic devices are allowed, including cell phones used merely as watches. Silence your cell phones and place them in your bag. Solutions will be graded on correctness and clarity. Each problem has a relatively simple and straightforward solution. Partial solutions will be graded for partial credit. There are todo pages in this exam and 4 questions, each with multiple 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. 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. Do not use any additional scratch paper. LOGIN: NAME: Problem Max points Points 1 2 3 4 Sub Total 100 1

Question 1: Adding Constructs [15 points] This question will test your understanding of how to add new language constructs. Imagine you want to add a crazy new construct to the cs164 language. This construct will be called untilnull, and it will evaluate each expression in a sequence of 3 expressions until it reaches an expression that evaluates to null. It will not execute any expressions after that null valued expression. If no expression in the list evaluates to null, this construct will evaluate all expressions. For the purposes of this question, we do not care what untilnull itself evaluates to. Informal Syntax Example 1 Output 1 Example 2 Output 2 untilnull(e, E, E) deftimes(x, y){ defa=x*y printa if(a==2){null else{a untilnull( times(1,1), times(1,2), times(1,3) ) 1 2 defx=0 defplus(num){ x=x+num null untilnull( plus(1), plus(2), plus(3) ) printx 1 Part A [5 points] Can the untilnullconstruct be implemented by extending the core cs164 language that is, by adding an untilnullast node with e1, e2, and e3attributes and adding a new untilnullcase to your interpreter s evalexpressionfunction? If yes, write code for the new untilnullcase. If no, write a one to two sentence description of why this design strategy is impossible. Write in only one box! Yes functionevalexpression(node, env){ switch(node.type){ case'untilnull': if(evalexpression(node.e1, env)!==null){ if(evalexpression(node.e2, env)!==null){ evalexpression(node.e3, env) returnnull//wedon tcareaboutthereturnval No 2

Part B [5 points] Can the untilnullconstruct be implemented by desugaring the untilnullconstruct into constructs already present in the cs164 language that is, by adding an untilnullast node with e1, e2, and e3attributes and adding a new desugaring rule? If yes, write the desugaring rule. If no, write a one to two sentence description of why this design strategy is impossible. Write in only one box! Yes vardesugar_ast_raw={ 'untilnull':'lambda(){\ if(%e1!=null){\ if(%e2!=null){\ %e3\ \ ()' No Part C [5 points] Can the untilnullconstruct be implemented as a library function that is, by letting the parser produce a callnode, and adding a function untilnullwith arguments e1, e2, and e3defined in library.164? If yes, write code for the library function. If no, write a one to two sentence description of why this design strategy is impossible. Write in only one box! Yes defuntilnull(e1, e2, e3){ No No. If we implement untilnullas a function, all three expressions will be evaluated every time the construct is used. Our spec requires that only the 3

expressions up until and including the first null valued expression be evaluated. 4

Question 2: Regular Expressions and Automata [30 points] This question will test your understanding of regular expressions and automata. Part A [2 points] Write a regular expression that accepts the same strings as the nondeterministic finite automaton (NFA) pictured below. Your regular expression may use concatenation, alternation ( ), Kleene stars (*), and parentheses. NFA Regular Expression (cs164)* Part B [4 points] Draw an NFA that accepts the same strings as the following regular expression. Regular Expression NFA (a bc)*d 5

Part C [6 points] Draw a state transition table (as seen in discussion section) for the following NFA, then a state transition table for a deterministic finite automaton (DFA) that accepts the same strings, then a DFA that accepts the same strings. NFA NFA state transition table DFA state transition table a b 1 {2,3 { 2 { {3 3 {2 { a b _1 _2_3 D _2_3 _2 _3 _2 D _3 _3 2 D D D D DFA Part D [6 points] The following rule for converting the regular expression A* into an automaton is not correct. The oval labeled A represents an NFA equivalent to the regular expression element A. Provide an example regular expression for which this rule will yield an NFA that is not equivalent to the regular expression also provide a string that is accepted by either the regular expression or the NFA, but not the other. 6

Regular expression for which this rule fails: (a(b)*)* String accepted by either the above regular expression or its rule generated automaton, but not both: b Part E [6 points] A question mark in a regular expression indicates that the regular expression accepts exactly 0 or 1 instances of the element that precedes the question mark. For instance, the regular expression yay? accepts the strings ya and yay. If our only other translation rules are the rules seen in class, is the following rule for converting A? to an automaton correct for all regular expressions? If yes, justify your answer. If no, provide an example regular expression on which it fails also provide a string that is accepted by either the regular expression or the automaton, but not the other. Yes, this rule works. Justification: As long as we re using correct rules for all other constructs in regular expressions, this is ok, because there are never backward edges leaving the final state. If we re using incorrect rules, like the Kleene star rule above, then this rule can fail, basically in the same way that the above Kleene star rule fails. No, this rule doesn t work. Regular expression for which this rule fails: String accepted by either the above regular expression or its generated automaton, but not both: Part F [6 points] Propose a rule for converting regular expressions with Kleene plus (+) into automata. Your rule should be presented as a drawing of the automaton to which A+ should be converted, where A is any regular expression element. Label an oval with A to indicate that it is an NFA equivalent to the regular expression element A. A Kleene plus is like a Kleene star except that it requires at least one repetition of the preceding regular expression element. 7

8

Question 3: Grammars, SDT and Parsing [35 points] This question will test your understanding of grammars, syntax directed translation and parsing. We provide a small grammar to express automata below. You will be fixing and adding onto this grammar. %ignore /[\t\v\f\r\n]+/ %% S->SSepS E E->NodeTransitionNode Transition-> -> Input Node->Id Sep-> Input-> ( Character ) _ Character->/[a-zA-Z_0-9]/ Id->/[a-zA-Z_][a-zA-Z_0-9]+/ 9

Part A [10 points] The grammar allows simple automata to be easily specified. We provide an example of a string in the language, and the corresponding automata below. Input: n1->(h)n2 Draw the completed edges created by the Earley algorithm using the above grammar on the input n1->(h)n2. We provide the final completed edge (S > E.) as an example of the labelling we expect. Part B [4 points] The grammar above is ambiguous. Modify the grammar to remove ambiguity. You only need to write production(s) that are new or modified. S->SSepE E Part C [2 points] What is the worst case running time of parsing an input string using the original (ambiguous) grammar? O(n^3) 10

Part D [3 points] Write an input which exhibits the worst case running time for the original grammar. n1->(h)n2n2->(i)n3n3->(j)n4 n100->(a)n101 Part E [3 points] Now we want to modify the grammar to allow for paths. A path allows multiple adjacent transitions to be specified together. For example, the two following strings specify the same automaton. n1->(h)n2n2->(i)n3 n1->(h)n2->(i)n3 Again, you only need to write production(s) that are new or modified. Ensure that the new grammar is still unambiguous. E->ETransitionNode NodeTransitionNode Part F [3 points] Now we want to modify the grammar to specify start states and accepting states. We give a few examples of the syntax below. n1[start]->(h)n2->(i)n3[accept] n1[start,accept]->(a)n1 Modify your grammar so that it accepts this new language. As before, make sure the grammar is unambiguous. Node->Id Id [ Modifier_list ] Modifier_list->Modifier_list, Modifier Modifier Modifier-> start accept 11

Part G [10 points] Complete the grammar below so that strings in the language are translated into the following function calls. Function name addedge(nodeid,nodeid,input) addstart(nodeid) addaccept(nodeid) Description Add a transition edge between two states. input should either be a string of length 1, specifying the input character or null, specifying an epsilon transition. Specifies that nodeidis a start state. Specifies that nodeidis an accepting state. We provide an example of translation below. Input: n1[start,accept]->(c)n2->(s)n3->(1)n4->(6)n5->(4)n6->n1 Function Call Trace Automata addstart( n1 ) addaccept( n1 ) addedge( n1, n2, c ) addedge( n2, n3, s ) addedge( n3, n4, 1 ) addedge( n4, n5, 6 ) addedge( n5, n6, 4 ) addedge( n6, n1,null) Note that the output represents a trace of functions called during the execution of the parser, not a textual program returned by parsing. Also, functions may be called in any order. %ignore /[\t\v\f\r\n]+/ %% S->SSepE %{return% E %{return% 12

E->ETransitionNode %{addedge(n1.val,n3.val,n2.val)returnn3.val% NodeTransitionNode %{addedge(n1.val,n3.val,n2.val)returnn3.val% Transition->'->'Input%{returnn2.val% Node->Id %{returnn1.val% Id'['Modifier_list']' %{if('start'inn3.val) addstart(n1.val) if('accept'inn3.val) addaccept(n1.val) returnn1.val % Modifier_list->Modifier_list','Modifier %{vart=n1.val t[n3.val]=true returnt % Modifier %{vart={ t[n1.val]=true returnt % Modifier->'start' 'accept' Input->'('Character')' %{returnn2.val% _ %{returnnull% Sep->'' Character->/[a-zA-Z_0-9]/ Id->/[a-zA-Z_][a-zA-Z_0-9]+/ 13

14

Question 4: Bytecode translation [20 points] In PA 4, you implemented compilation to bytecode of cs164 statements. In this question, we will consider an alternative protocol for bytecode generation, i.e. we are going to change how the recursive functions in the AST to bytecode compiler exchange information. We will adapt the compilation rules accordingly. Recall the interface for the btcnode function, that takes a node to translate, a target register, and the array of bytecode generated so far: functionbtcnode(node,target,btc){ We would like to modify this interface such that btcnode only takes one argument, the node to translate. functionbtcnode(node){ We also require that btcnode cannot access any global variables. Part A [2 points] Give a data structure (a JavaScript object) that the new btcnode function needs to return with this new protocol? Give a one line description for each field you define. The new btcnode will need to return a data structure with: target: the identifier of a fresh target node that it creates btc: the bytecode array that the node translates to 15

Part B [8 points] Complete the bytecode translation code below for addition. The input to btcnode for an addition is an AST whose nodes include the fields: type: the node type, i.e. + operand1: the AST for the first operand operand2: the AST for the second operand The bytecode instruction for addition is an object with the following fields: type: the instruction type, + operand1: the register for the first operand operand2: the register for the second operand target: the target register that should receive the addition result You may use the following library functions: concat(xs1, xs2) returns the concatenation of the two sequences xs1 and xs2. append(xs, x) returns a new sequence that appends element x to the sequence xs. uniquegen() generates generate a unique name. functionbtcnode(node){ switch(node.type){ case + : varres1=btcnode(node.operand1) varres2=btcnode(node.operand2) vartarget=uniquegen() varaddbtc={ type : +, operand1 :res1.target, operand2 :res2.target, target :target return{ btc :append(concat(res1.btc,res2.btc),addbtc), target :target break 16

Part C [10 points] Complete the bytecode translation below for function calls. The input to btcnode for a call is an AST which defines: type: the node type, i.e. call. function: the AST for the function arguments: the array of ASTs for the call arguments. The bytecode instruction for the call is an object with the following fields: type: the instruction type, call function: the register that holds the function to call arguments: the array of registers that hold each argument value target: the target register that should receive the value returned by the call You may use the library functions given for Part B. Problem continued on the next page. 17

functionbtcnode(node){ switch(node.type){ case call : varbtcs=[] vartarget=uniquegen() varfnres=btcnode(node.function) varargtargets=[] varargbtcs=[] node.arguments.foreach(function(arg){ varargres=btcnode(arg) argtargets=append(argtargets,argres.target) btcs=concat(btcs,argres.btc) ) varcallbtc={ type : call, function :fnres.target, arguments :argtargets, target :target return{ btc :append(concat(fnres.btc,argbtcs),callbtc), target :target break 18