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

Similar documents
CSE 401 Midterm Exam Sample Solution 2/11/15

CSE 401 Midterm Exam 11/5/10

CSE 401 Midterm Exam Sample Solution 11/4/11

CSE P 501 Exam 12/1/11

CSE P 501 Exam 8/5/04

Midterm I - Solution CS164, Spring 2014

Question Points Score

CSE 582 Autumn 2002 Exam 11/26/02

CSE P 501 Exam 11/17/05 Sample Solution

CSE 413 Final Exam. December 13, 2012

CSE 413 Final Exam. June 7, 2011

CSE 401 Final Exam. December 16, 2010

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

CSE P 501 Exam Sample Solution 12/1/11

CSE 303 Midterm Exam

CS164: Midterm I. Fall 2003

CS131 Compilers: Programming Assignment 2 Due Tuesday, April 4, 2017 at 11:59pm

Programming Assignment III

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

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

CSE 374 Midterm Exam 11/2/15. Name Id #

Examination in Compilers, EDAN65

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

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

CMSC 330: Organization of Programming Languages

A Simple Syntax-Directed Translator

CSE au Final Exam Sample Solution

CSE 5317 Midterm Examination 4 March Solutions

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

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.

Syntax-Directed Translation. Lecture 14

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

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

Chapter 4. Abstract Syntax

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

CSE 582 Autumn 2002 Exam Sample Solution

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017

HW 3: Bottom-Up Parsing Techniques

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

Chapter 3 Parsing. time, arrow, banana, flies, like, a, an, the, fruit

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Midterm I (Solutions) CS164, Spring 2002

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

Programming Project II

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

LECTURE 3. Compiler Phases

A simple syntax-directed

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

ECE251 Midterm practice questions, Fall 2010

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Downloaded from Page 1. LR Parsing

Lecture 7: Deterministic Bottom-Up Parsing

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Context-Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08008 INFORMATICS 2A: PROCESSING FORMAL AND NATURAL LANGUAGES

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

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CPSC 411, Fall 2010 Midterm Examination

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

Compiler phases. Non-tokens

CS 314 Principles of Programming Languages

2068 (I) Attempt all questions.

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

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

Lecture 8: Deterministic Bottom-Up Parsing

CMSC 330: Organization of Programming Languages

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

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

Introduction; Parsing LL Grammars

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

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

Fall Compiler Principles Lecture 4: Parsing part 3. Roman Manevich Ben-Gurion University of the Negev

CS 164 Programming Languages and Compilers Handout 8. Midterm I

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

Parser Tools: lex and yacc-style Parsing

Context-free grammars (CFGs)

LR Parsing. The first L means the input string is processed from left to right.

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

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

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

CSE wi Midterm Exam 2/8/18. Name UW ID #

CS143 Midterm Fall 2008

CMSC 330: Organization of Programming Languages

MP 3 A Lexer for MiniJava

CSE 3302 Programming Languages Lecture 2: Syntax

CMSC 330: Organization of Programming Languages

UNIT III & IV. Bottom up parsing

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Parser Tools: lex and yacc-style Parsing

CS143 Midterm Sample Solution Fall 2010

Optimizing Finite Automata

CS 374 Fall 2014 Homework 2 Due Tuesday, September 16, 2014 at noon

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

Transcription:

Name ID # There are 7 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed books, closed notes, closed electronics. Please turn off all cell phones, personal electronics, alarm watches, and pagers, and return your tray tables and seat backs to their full upright, locked positions. Sound recording and the taking of photographs is prohibited. If you have a question during the exam, please raise your hand and someone will come to help you. Please write on the front of each page only. Those are the only pages that will be scanned for grading. There is an extra blank page at the end of the exam you can use if your answer(s) do not fit in the space provided. Please indicate on the original page(s) if your answer(s) is(are) continued on that last page. Please wait to turn the page until everyone is told to begin. Score 1 / 14 2 / 8 3 / 10 4 / 8 5 / 32 6 / 14 7 / 14 Page 1 of 11

Question 1. (14 points) Regular expressions. We would like to process strings that represent file paths in a hypothetical file system. A file path consists of a drive specification, zero or more directories separated by backslashes, and a filename with a required file extension. The drive specification consists of a single uppercase letter followed by a colon and a backslash. Directory names and file names start with a single lowercase letter, and may be followed by any number of lowercase letters, numbers, or underscores. File extensions are exactly the same, but may not contain underscores. The file name is separated from the final directory name (if any) by a backslash, and the file name is separated from the file extension by a dot. Some examples of valid file paths: C:\folder\file.txt A:\abc\temp_25\d.b4 Z:\midterm_solutions.docx D:\marketing3.pptx Some invalid file paths: B:\18au\scanner.pdf (names cannot start with numbers) M:\data.a_file (extensions cannot contain underscores) E:\\backups (directories names cannot be empty and file extensions are required) Z:\docs\README.txt (must use lower-case letters only) As with homework problems, you must restrict yourself to the basic regular expression operations covered in class and on homework assignments: r s, r s, r*, r+, r?, character classes like [a-cxy] and [^aeiou], abbreviations name=regexp, and parenthesized regular expressions. No additional operations that might be found in the regexp packages in various Unix programs, scanner generators like JFlex, or language libraries are allowed. Write your. answers on. the next page. Remove this page from the exam and do not include it when you hand in your exam. It will not be scanned or graded. Page 2 of 11

Question 1. Write your answers here. Hint: it may be useful to work on the regular expression and the DFA parts simultaneously. (a) (7 points) Give a regular expression (possibly with subexpressions if it makes things clearer) that generates all valid file paths according to the above rules. (b) (7 points) Draw a DFA that accepts all valid file paths according to the above rules. Page 3 of 11

Question 2. (8 points) Tokens R Us. We ve been having so much fun testing the MiniJava scanner that we decided to see what would happen if we used it to process the following Bash shell script: # comment while [ $# -gt 0 ] do echo $* shift done Below, list in order the tokens that would be returned by a scanner for MiniJava as it reads this input. If there is a lexical error in the input, indicate where that error is encountered by writing a short explanation of the error in between the valid tokens that appear before and after the error(s) (something brief like illegal character @ where a @ was found in the file would be fine). The token list should include additional tokens found after any error(s) in the input. You may use any reasonable token names (e.g., LPAREN, ID(x), etc.) as long as your meaning is clear. A copy of the MiniJava grammar is attached as the last page of the test. You may remove it for reference while you answer this question. You should assume the scanner implements MiniJava syntax as defined in that grammar, with no extensions or changes to the language. Page 4 of 11

Question 3. (10 points, 2 each) Ambiguity I. The following grammar is ambiguous: A ::= B b C B ::= b ε C :: = b ε To demonstrate this ambiguity we can use pairs of derivations. Here are five different pairs. For each pair of derivations, circle OK if the pair correctly proves that the grammar is ambiguous. Circle WRONG if the pair does not give a correct proof. You do not need to explain your answers. (Note: Whitespace in the grammar rules and derivations is used only for clarity. It is not part of the grammar or of the language generated by it.) (a) OK WRONG A => B b C => b b C => b b b A => B b C => B b b => b b b (b) OK WRONG A => B b C => b b C => b b A => B b C => b C => b b (c) OK WRONG A => B b C => b b C => b b A => B b C => B b b => b b (d) OK WRONG A => B b C => b b C => b b A => B b C => b b C => b b b (e) OK WRONG A => B b C => B b => b b A => B b C => B b b => b b Page 5 of 11

Question 4. (8 points) Ambiguity II. The following grammar is ambiguous. (As before, whitespace is used only for clarity; it is not part of the grammar or the language generated by it.) P ::=! Q Q && Q Q Q ::= P id Give a grammar that generates exactly the same language as the one generated by this grammar but that is not ambiguous. You may resolve the ambiguities however you want there is no requirement for any particular operator precedence or associativity in the resulting grammar. Page 6 of 11

Question 5. (32 points) The you re-probably-not-surprised-to-see-it LR parsing question. Here is a small grammar, complete with the extra S'::=S$ rule to handle endof-file in the generated parser. 1. S' ::= S $ ($ represents end-of-file) 2. S ::= a K 3. S ::= J S 4. S ::= b 5. J ::= a 6. K ::= b (a) (12 points) Draw the LR(0) state machine for this grammar. (b) (8 points) Compute nullable and the FIRST and FOLLOW sets for the nonterminals S, J, and K in the above grammar: Symbol nullable FIRST FOLLOW S J K (continued on next page) Page 7 of 11

Question 5. (cont.) Grammar repeated from previous page for reference: 1. S' ::= S $ ($ represents end-of-file) 2. S ::= a K 3. S ::= J S 4. S ::= b 5. J ::= a 6. K ::= b (c) (8 points) Write the LR(0) parse table for this grammar based on the LR(0) state machine in your answer to part (a). (d) (2 points) Is this grammar LR(0)? Explain why or why not. (e) (2 points) Is this grammar SLR? Explain why or why not. Page 8 of 11

Question 6. (14 points) LL grammars. Consider the following grammar: P ::= R b R ::= P S a c S ::= P b (a) (6 points) Demonstrate that this grammar does not satisfy the LL(1) condition. Hint: you may find it useful to compute nullable and the FIRST and FOLLOW sets for some or all productions, but you are not required to do so, as long as you can give a reason the grammar is not LL(1). (b) (8 points) Rewrite the grammar so that it is LL(1). Your answer must generate the same language as the above grammar but it should not contain any conflicts that would violate the LL(1) condition. Page 9 of 11

Question 7. (14 points) Semantics. Suppose we have the following statement in a MiniJava (not full Java) program: x = a+b.f(c); (a) (6 points) Draw an abstract syntax tree (AST) for this statement in the blank space at the bottom of the page. You are not expected to remember the exact names of the classes or node types in the MiniJava AST package used in the project code, but your answer should have the appropriate level of abstraction and structural detail that is found there, and the nodes should have reasonable names. In case it is useful, remember that a copy of the MiniJava grammar is attached at the end of the exam. (b) (8 points) Annotate your AST by writing next to the appropriate nodes the checks or tests that should be done in the static semantics/type-checking phase of the compiler to ensure that this statement does not contain any errors. You do not need to specify an attribute grammar just show the necessary tests. If a particular test applies to multiple nodes you can write it once and indicate which nodes it applies to, as long as your meaning is clear and readable. Page 10 of 11

Extra space for answers, if needed. Please be sure to label which question(s) are answered here, and be sure to put a note on the question page so the grader will know to look here. Page 11 of 11