CSE 401 Midterm Exam Sample Solution 11/4/11

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

CSE 401 Midterm Exam 11/5/10

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

CSE P 501 Exam 11/17/05 Sample Solution

CSE P 501 Exam Sample Solution 12/1/11

CSE P 501 Exam 8/5/04

CSE P 501 Exam 12/1/11

CSE 582 Autumn 2002 Exam Sample Solution

CSE 401 Final Exam. December 16, 2010

ECE251 Midterm practice questions, Fall 2010

CSE au Final Exam Sample Solution

CSE 413 Final Exam. December 13, 2012

CSE 582 Autumn 2002 Exam 11/26/02

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

Semantic Analysis. Compiler Architecture

A Simple Syntax-Directed Translator

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

Question Points Score

Chapter 4. Abstract Syntax

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

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

Downloaded from Page 1. LR Parsing

Optimizing Finite Automata

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

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

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

2068 (I) Attempt all questions.

2.2 Syntax Definition

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

CS 411 Midterm Feb 2008

Interpreter. Scanner. Parser. Tree Walker. read. request token. send token. send AST I/O. Console

CS 11 Ocaml track: lecture 6

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

Semantic Analysis. Lecture 9. February 7, 2018

LECTURE 3. Compiler Phases

CSE 413 Final Exam. June 7, 2011

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

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.

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

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Compiler Theory. (Semantic Analysis and Run-Time Environments)

CS 536 Midterm Exam Spring 2013

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

Lecture 8: Deterministic Bottom-Up Parsing

Defining syntax using CFGs

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

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.

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Lecture 7: Deterministic Bottom-Up Parsing

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

Last Time. What do we want? When do we want it? An AST. Now!

Topic 5: Syntax Analysis III

Introduction to Compiler Construction

CPS 506 Comparative Programming Languages. Syntax Specification

A simple syntax-directed

Context-Free Grammars

Parser and syntax analyzer. Context-Free Grammar Definition. Scanning and parsing. How bottom-up parsing works: Shift/Reduce tecnique.

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

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

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

(Not Quite) Minijava

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Conflicts in LR Parsing and More LR Parsing Types

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Introduction to Parsing. Lecture 5

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Building Compilers with Phoenix

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

Properties of Regular Expressions and Finite Automata

CSE 5317 Midterm Examination 4 March Solutions

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

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1


COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CMPT 379 Compilers. Parse trees

Agenda. Previously. Tentative syllabus. Fall Compiler Principles Lecture 5: Parsing part 4 12/2/2015. Roman Manevich Ben-Gurion University

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

The role of semantic analysis in a compiler

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

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

CMSC 350: COMPILER DESIGN

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

Compilers. Bottom-up Parsing. (original slides by Sam

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

Lecture 4: Syntax Specification

Outline CS4120/4121. Compilation in a Nutshell 1. Administration. Introduction to Compilers Andrew Myers. HW1 out later today due next Monday.

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

Grammars and Parsing. Paul Klint. Grammars and Parsing

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

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

Lecture 16: Static Semantics Overview 1

Transcription:

Question 1. (12 points, 2 each) The front end of a compiler consists of three parts: scanner, parser, and (static) semantics. Collectively these need to analyze the input program and decide if it is correctly formed. For each of the following properties or errors, indicate which stage of the front-end of the compiler would normally handle it. If it helps to explain your answer you can give a brief reason why, but that is not required. (a) In a variable declaration, the variable has not previously been declared in the same scope. semantics (b) += is not a legal assignment operator in MiniJava. parser (+ and = are both legal tokens, so the scanner won t detect the error) (c) A comment beginning with /* is not terminated before reaching the end of the input file (i.e., no matching */ found). scanner (e) In the method call x.m(e1,e2,,en), the type of x includes a suitable method m. semantics (e) Class declarations cannot be nested in MiniJava. parser (the grammar does not include nested class declarations) (f) The character cannot be used in an identifier in MiniJava. scanner Page 1 of 8

Question 2. Regular essions (12 points, 6 each) Give regular essions for the following sets of strings. You may only use the basic operations of concatenation, choice ( ), and repetition (*), plus the simple extensions? and +, and character sets like [a-z] and [^a-z]. You may also give names to subessions like vowels = [aeiou]. (a) All strings of 0 s and 1 s that have at least one character and start and end with the same character. (Note that the strings 0 and 1 by themselves are in this set, as well as longer strings of 0 s and 1 s that have the same character at the beginning and end.) Here are two possibilities; there are many others: 0 1 0(0 1)*0 1(0 1)*1 0((0 1)*0)? 1((0 1)*1)? (b) All strings of a s, b s and c s that (i) have at least one c, and (ii) all of the a s (if any) are to the left of all of the b s (if any). Two example solutions. For both, let acs = (a c)* and bcs = (b c)*. Solution 1: acs bcs c acs c bcs c acs bcs Solution 2: acs c acs bcs acs bcs c bcs Page 2 of 8

Question 3. DFAs. (8 points) Draw a DFA that accepts strings generated by the regular ession (a (bc)* d)+ Note that (bc)* means empty or bc or bcbc or bcbcbc, but not an arbitrary sequence of b s and c s. That would be (b c)*. a, d a, d b d c b b Page 3 of 8

Question 4. Context-free grammars (14 points) Consider the following syntax for essions involving addition and selection: ::= + ::= ::=. id ::= id (a) (8 points) Show that this grammar is ambiguous. Here are two derivations of id+id.id: id + id. id id + id. id (b) (6 points) Give an unambiguous context-free grammar that fixes the problem(s) with the grammar in part (a) and generates essions with id, selection and addition. As in Java, selection should have higher precedence than addition and both selection and addition should be left-associative (i.e., a+b+c means (a+b)+c). The problem is in the first rule for, which creates an ambiguous precedence. Here is a reasonably simple fix. ::= + ::= ::=. id ::= id Page 4 of 8

Question 5. (26 points) The oh-no-not-again LR-parsing question. Here is a tiny grammar. 0. exp ::= exp $ 1. exp ::= id [exp ] 2. exp ::= id (a) (11 points) Draw the LR(0) state machine for this grammar. 1 2 exp' ::=. exp $ exp exp ::=. id [ exp ] exp' ::= exp. $ exp ::=. id id 3 exp ::= id. [exp ] exp ::= id. [ id 4 exp ::= id [. exp ] exp ::=. id [exp ] exp ::=. id exp 5 exp ::= id [exp. ] ] 6 exp ::= id [exp ]. (continued on next page) Page 5 of 8

Question 5. (cont.) Grammar repeated from previous page for reference. 0. exp ::= exp $ 1. exp ::= id [exp ] 2. exp ::= id (b) (9 points) Construct the LR(0) parse table for this grammar based on the state machine in your answer to part (a). State id [ ] $ exp 1 s3 g2 2 acc 3 r2 r2, s4 r2 r2 4 s3 g5 5 s6 6 r1 r1 r1 r1 (b) (3 points) Is this grammar LR(0)? Why or why not? No. There is a shift/reduce conflict in state 3 on input [. (c) (3 points) Is this grammar SLR? Why or why not? Yes. [ is not in FOLLOW(exp) so we can eliminate the conflict in state 3. Page 6 of 8

Question 6. Project (14 points) Suppose we want to add a do-while loop to MiniJava like the one found in Java, C, C++, and other languages. The syntax of do-while is do Statement while ( Expression ) The semantics of do-while is that the Statement is executed, then the Expression is evaluated, then, if the Expression is true, the process repeats again until the Expression eventually evaluates to false. (a) (3 points) What changes need to be made to the MiniJava scanner if this statement is added? You do not need to give specific JFlex or CUP code, just describe the needed changes, if any. Add DO as a new token Add a rule to JFlex to recognize do as a new keyword and return this new token. (b) (3 points) What changes or additions (if any) need to be made to the MiniJava AST classes to add the do-while statement? Again, you don t need to give specific code, but describe your changes specifically enough that anyone familiar with the project could make the changes mechanically. Add a new DoStatement AST node class extending Statement with instance variables for the ession and statement that make up the loop. (c) (8 points) Give the code that needs to be added to the grammar part of the CUP specification for Statement to successfully parse the new do-while statement, including appropriate semantic action code to create appropriate part(s) of the AST. Statement ::= DO Statement:s WHILE LPAREN Expression:e RPAREN {: RESULT = new DoStatement(s,e,sleft); :} No points were deducted if you did not include the line number. Page 7 of 8

Question 7. (14 points, 7 each) Suppose we encounter the following statement in a MiniJava program: foo = x.m(a[i]); (a) Draw a tree below showing the abstract syntax for this statement. Don t worry about whether you match the AST classes in the MiniJava project exactly (you re not expected to memorize that sort of thing), but be sure that you have the appropriate nodes and edges to include the information that belongs in the abstract syntax. (b) After you ve drawn the AST for this statement, annotate it by writing next to the appropriate nodes the checks or tests that need to be done in the static semantics/typechecking phase of the compiler to ensure that this statement does not contain any errors. = (assign) - left operand is assignable (lvalue) - type of ession is assignment compatible with type of left operand id: foo method call - type of left ession has method m with one parameter -argument type is assignment compatible with parameter type id: x id: m [] (array element) id: a id: i - identifiers are declared and are visible in current scope - type of left esson is int[] - type of subscript is int Page 8 of 8