CSE 413 Final Exam. June 7, 2011

Similar documents
CSE 413 Final Exam Spring 2011 Sample Solution. Strings of alternating 0 s and 1 s that begin and end with the same character, either 0 or 1.

CSE 413 Final Exam. December 13, 2012

CSE au Final Exam Sample Solution

CSE 143 Final Exam Part 1 - August 18, 2011, 9:40 am

CSE 401 Midterm Exam 11/5/10

CSE 401/M501 18au Midterm Exam 11/2/18. Name ID #

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

CSE 582 Autumn 2002 Exam 11/26/02

CSE 401 Final Exam. December 16, 2010

Question Points Score

A Simple Syntax-Directed Translator

CSE 341, Spring 2011, Final Examination 9 June Please do not turn the page until everyone is ready.

CSE P 501 Exam 12/1/11

CSE 401 Midterm Exam Sample Solution 2/11/15

CSE 3302 Programming Languages Lecture 2: Syntax

CSE 303 Midterm Exam

CSE 401 Midterm Exam Sample Solution 11/4/11

CSE P 501 Exam 11/17/05 Sample Solution

CSE P 501 Exam 8/5/04

CSE 582 Autumn 2002 Exam Sample Solution

CSE P 501 Exam Sample Solution 12/1/11

CSCE 531 Spring 2009 Final Exam

2.2 Syntax Definition

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CSE 143 Sp03 Final Exam Sample Solution Page 1 of 13

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

The SPL Programming Language Reference Manual

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

A simple syntax-directed

CS 536 Midterm Exam Spring 2013

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

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

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

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

CS 403: Scanning and Parsing

CS164: Midterm I. Fall 2003

CSE 12 Abstract Syntax Trees

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

UNIVERSITY OF CALIFORNIA

Examination in Compilers, EDAN65

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

U N I V E R S I T Y O F W E L L I N G T O N EXAMINATIONS 2017 TRIMESTER 1 *** WITH SOLUTIONS *** COMP 261 ALGORITHMS and DATA STRUCTURES

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Introduction to Parsing. Lecture 5

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

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

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

Semantic Analysis. Lecture 9. February 7, 2018

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

CSE 374 Midterm Exam Sample Solution 2/11/13. Question 1. (8 points) What does each of the following bash commands do?

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Parser Tools: lex and yacc-style Parsing

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

ECE251 Midterm practice questions, Fall 2010

Context-Free Grammars

Pace University. Fundamental Concepts of CS121 1

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

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

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

About this exam review

The PCAT Programming Language Reference Manual

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

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

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

Principles of Programming Languages COMP251: Syntax and Grammars

Introduction to Parsing. Lecture 5

Syntax. In Text: Chapter 3

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

1 Lexical Considerations

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CMSC 330: Organization of Programming Languages

Syntax Analysis Check syntax and construct abstract syntax tree

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

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

CSE 5317 Midterm Examination 4 March Solutions

LECTURE 7. Lex and Intro to Parsing

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 303, Spring 2005, Midterm Examination 29 April Please do not turn the page until everyone is ready.

CIS 341 Midterm February 28, Name (printed): Pennkey (login id): SOLUTIONS

Chapter 4. Abstract Syntax

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

Parsing II Top-down parsing. Comp 412

CS 415 Midterm Exam Fall 2003

Parser Tools: lex and yacc-style Parsing

Chapter 2 - Programming Language Syntax. September 20, 2017

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

The Decaf Language. 1 Lexical considerations

CMSC 330: Organization of Programming Languages

CSE341, Spring 2013, Final Examination June 13, 2013

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Principles of Programming Languages COMP251: Syntax and Grammars

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Transcription:

CSE 413 Final Exam June 7, 2011 Name The exam is closed book, except that you may have a single page of hand-written notes for reference plus the page of notes you had for the midterm (although you are unlikely to need that one). Style and indenting matter, within limits. We re not overly picky about details, but we do need to be able to follow your code and understand it. Please wait to turn the page until everyone has their exam and you have been told to begin. If you have questions during the exam, raise your hand and someone will come to you. Don t leave your seat. Advice: The solutions to many of the problems are short. Don t be alarmed if there is a lot more room on the page than you actually need for your answer. More gratuitous advice: Be sure to get to all the questions. If you find you are spending a lot of time on a question, move on and try other ones, then come back to the question that was taking the time. 1 / 10 2 / 12 3 / 8 4 / 8 5 / 8 6 / 12 7 / 12 8 / 20 9 / 5 10 / 5 Total / 100 CSE 413 Final Exam, June 7, 2011 Page 1 of 14

Question 1. (10 points) Regular expressions I. Describe the set of strings generated by each of the following regular expressions. For full credit, give a description of the sets like all sets of strings made up of a s, b s, and c s with 4 a s and at least as many b s as a s. Don t just transcribe the expressions from regular expression notation into English. (a) 0(10)* 1(01)* (b) ( (0 1)* (2 3)+ )+ CSE 413 Final Exam, June 7, 2011 Page 2 of 14

Question 2. (12 points) Regular expressions II. Write a regular expression or set of regular expressions that generate the following sets of strings. Fine print: You may use basic regular expressions (sequences rs, choice r s, and repetition r* and parentheses for subexpressions). You may also use + (one or more) and? (zero or one), and character classes like [ax-z], but you may not use additional regular expression operators that might be found in various programming languages and software tools. You also may use named abbreviations like vowels ::= *aeiou+ if these help. (a) All strings of a s, b s, and c s that have at least one character and have the property that all of the c s (if any) appear to the right of all of the a s (if any). Examples of such strings: a, b, abba, bcb, acc, ababbcb. Examples of strings that are not in the set: ca, abbacbccbbabc. (b) Ruby-like integers written as either decimal or binary integer literals. A decimal integer is any string of one or more digit characters (0-9). A binary integer begins with the characters 0b or 0B and is followed by one or more 0 s and 1 s. Examples of binary integer literals: 0b0, 0B1, 0b001011000. CSE 413 Final Exam, June 7, 2011 Page 3 of 14

Question 3. (8 points) Suppose we are writing a scanner for Java (or, for the purposes of this question, it could be C or C++). The scanner needs to recognize identifiers, reserved words, operators, punctuation, and so forth. (a) Suppose we have the following characters in the input, with spacing as shown: if thing == x = ret urn + 1 List below the tokens that would be recognized by a Java scanner as it reads this input. (Remember that the scanner does not care whether the sequence of tokens it returns makes up a sensible program.) (b) Now suppose we scan exactly the same input, but with all spaces omitted. What tokens would be returned by the scanner as it reads this altered input? ifthing==x=return+1 CSE 413 Final Exam, June 7, 2011 Page 4 of 14

Question 4. (8 points) Give an unambiguous context free grammar that generates Scheme lists containing identifiers and balanced sets of parentheses and square brackets. Identifiers may only occur inside of parentheses or brackets. Examples: (a), [b c], ([(a)]), (a ((b c) d)), (a b c), (let ([x y] [p q]) (x p)). Non examples and reasons why not: a (no parentheses or brackets), (a b (c) (mismatched parentheses), (a [b c)) (improper nesting). You should assume that id is a non-terminal that stands for an arbitrary identifier. You do not need to worry about including specific identifiers like a or b. You also do not need to worry about whitespace assume that if you generate the sequence id id id there would be appropriate whitespace to avoid any ambiguities. CSE 413 Final Exam, June 7, 2011 Page 5 of 14

Question 5. (8 points) Here is a grammar for simplified Ruby method calls. A method call has a receiver object (an expression), a method name, an optional single parameter, and is optionally followed by a block. A block contains a statement and an optional parameter. expr ::= id expr. id optparm optblock optparm ::= ε ( expr ) optblock ::= ε { stmt } { id stmt } stmt ::= puts id gets The non-terminal id can derive any identifier (sequence of letters). The symbol ε is the Greek letter epsilon, denoting the empty string. Note that the vertical bars ( ) surrounding id in a block are literal symbols, not the choice or alternative symbols found elsewhere in the grammar rules. Draw the full parse tree showing the derivation of this expression: words.each { x puts x} CSE 413 Final Exam, June 7, 2011 Page 6 of 14

Question 6. (12 points) Dynamic dispatch (otherwise known as virtual function calls) Consider the following Java program: class A { void f() { g(); System.out.println("A::f"); } void g() { System.out.println("A::g"); } } class B extends A { void h() { g(); System.out.println("B::h"); } void g() { System.out.println("B::g"); } } class C extends B { void g() { System.out.println("C::g"); } void f() { h(); System.out.println("C::f"); } } class Dispatch { public static void main(string[] args) { A thing1 = new B(); B thing2 = new C(); C thing3 = new C(); /* Part (a) (see next page) Complete the diagram showing objects, vtables, and methods at this point during execution. */ } } thing1.f(); System.out.println("---"); thing2.f(); System.out.println("---"); thing3.h(); Answer questions about this program on the next page. You can remove this page for convenience while you work on the problem. CSE 413 Final Exam, June 7, 2011 Page 7 of 14

Question 6. (cont) (a) In lecture we talked about the virtual function table mechanism used to connect objects, classes, and methods in statically typed object-oriented languages like Java and C++. The variables declared in the main method, the objects they reference, the method code from the various classes, and the virtual function table (vtable) for class A are shown. You should complete this diagram to show the additional tables and connections that exist when execution of the program reaches the point shown by the comment in the middle of the main method on the previous page. A vtable A::f code main thing1 vtbl other data f g A::g code B::h code thing2 thing3 vtbl other data B::g code C::g code vtbl C::f code other data (b) What output does this program produce when it is executed? (Hint: your answer to part (a) might be useful in following the execution to get the right answer.) CSE 413 Final Exam, June 7, 2011 Page 8 of 14

Question 7. (12 points) Write a Ruby program that reads text from standard input and searches for words in the input. The first line of the input is the list of words to look for. The remaining lines should be examined to see if they contain any of the words in the given list. If any of the words are found, the line containing them should be printed out preceded by its line number. For example, if the input is how now brown cow old macdonald had a farm e-i-e-i-o that farm was on a brown dirt road and on that farm he had a cow cowabunga said old macdonald the output should be 3: that farm was on a brown dirt road 4: and on that farm he had a cow since line 3 contains the word brown and line 4 contains the word cow. Line 5 is not included in the output because none of the words in the search list match a word in that line, even though cow is a substring of cowabunga. The initial line containing the words to search for is numbered as line 0; successive lines are numbered from 1. Each input line should be printed at most once in the output, even if it contains several copies of words in the search list. To simplify things, you should read the input one line at a time with gets and use the String split method to break each line into words. The basic behavior of split is to return an array of the words, where words are defined as strings of characters separated by whitespace. Example: one two three.split => [ one, two, three ] To keep the problem simple, assume that there is no extra leading or trailing whitespace in the input lines, and assume that all words in the input contain only lower case letters, so you don t have to deal with issues of punctuation or capitalization. For full credit you should use Ruby iterators like each to process the contents of containers like arrays and hashes. Recall that if h is a hash, you can iterate through its key/value pairs with h.each { key, value... } Write your code on the next page. If you find it helpful, you can remove this page from the exam for reference while you are working. Next page please. CSE 413 Final Exam, June 7, 2011 Page 9 of 14

Question 7. (cont.) Code for the word search program: CSE 413 Final Exam, June 7, 2011 Page 10 of 14

Question 8. (20 points) One of our clients would like us to extend the calculator language and interpreter to add a function to compute the maximum value of one or more expressions. For instance, max(1+2) would evaluate to 3, and max(17, 3-42, 5) would evaluate to 17. Of course variables could also appear in expressions, as in the original calculator language. For reference, here is the original calculator grammar (although you may not need to use it): program ::= statement program statement statement ::= exp id = exp unset id list quit exit exp ::= term exp + term exp - term term ::= power term * power term / power power ::= factor factor ** power factor ::= id number ( exp ) sqrt ( exp ) One of those pesky summer interns proposes adding this new max function to the calculator language by adding an alternative to the rule for factor and adding an additional rule to handle the max function parameter lists: factor ::=... max ( explist ) explist ::= explist, explist exp (a) Show that the original grammar with these additions is now ambiguous. (Hint: you only need to show ambiguity for a part of a grammar that has a problem, not necessarily for all of the grammar rules.) (continued next page) CSE 413 Final Exam, June 7, 2011 Page 11 of 14

Question 8. (cont.) (b) Give a different grammar rule or rules that add the max function to the rule for factor and handle its parameter list, but that are not ambiguous. (c) We would like to add this new max function to our calculator interpreter. What changes, if any, need to be made to the original calculator scanner? Describe any necessary changes to the scanner, the set of tokens, or anything else that must be modified. You do not need to give the implementation, just describe the needed changes. (d) On the next page, write the additional Ruby code needed in method factor and any other necessary code needed to parse and evaluate this new max function and its parameter list. Your answer should be guided by your solutions to parts (b) and (c), but, as usual, adjusted as needed for use in a recursive-descent parser. You should make the following assumptions: The scanner has been modified as describe above in your solution to part (c). You can call the scanner s next_token method whenever you need to get the next token from the input. If for some reason you need it, the kind method of a Token object returns strings containing the token text like list, sqrt, +. There is a global variable current_token that contains the next unprocessed Token in the input at all times. Your code needs to update this variable appropriately as it parses the input. Functions like exp exist to parse each grammar nonterminal and return its value, if any. You should assume that there are no syntax errors, missing or extra tokens, or other problems in the calculator input. Write your solution on the next page. CSE 413 Final Exam, June 7, 2011 Page 12 of 14

Question 8. (cont.) (d) Below, write your modifications to method factor and any other code needed to parse and evaluate the new max function expression. CSE 413 Final Exam, June 7, 2011 Page 13 of 14

A couple of short-answer questions to wrap up. Please keep your answers brief and to the point (and legible!!). Your readers thank you. Question 9. (5 points) When we looked at automatic storage management, one of the algorithms we discussed was Generational Garbage Collection. In the basic version of this scheme, the heap is divided into two parts, a nursery, which is the space where new objects are allocated, and the rest of the heap where long-lived objects are moved after a number of garbage collections. What is the rationale for splitting the heap this way? What advantage does it provide, if any? Question 10. (5 points) Java includes interfaces that can be used to specify a collection of methods and constants. Ruby doesn t contain anything similar. Would it make any sense to add interfaces to Ruby as a new language feature? Why or why not? CSE 413 Final Exam, June 7, 2011 Page 14 of 14