CSE 582 Autumn 2002 Exam 11/26/02

Similar documents
CSE 582 Autumn 2002 Exam Sample Solution

CSE P 501 Exam 8/5/04

CSE P 501 Exam 11/17/05 Sample Solution

CSE P 501 Exam 12/1/11

CSE 401 Final Exam. December 16, 2010

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.

CSE 401 Midterm Exam 11/5/10

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

CSE P 501 Exam Sample Solution 12/1/11

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

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

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

CSE 413 Winter 2001 Final Exam Sample Solution

CSE 413 Final Exam. June 7, 2011

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

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

ECE468 Fall 2003, Final Exam

Midterm I - Solution CS164, Spring 2014

LL(1) predictive parsing

CSE 401 Midterm Exam Sample Solution 11/4/11

CS 406/534 Compiler Construction Putting It All Together

CSCE 531 Spring 2009 Final Exam

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

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

Optimizing Finite Automata

Lexical Analysis. Introduction

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

Midterm I (Solutions) CS164, Spring 2002

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CSE 401 Midterm Exam Sample Solution 2/11/15

Syntax. A. Bellaachia Page: 1

CS 415 Midterm Exam Spring SOLUTION

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

Lecture 8: Deterministic Bottom-Up Parsing

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

WARNING for Autumn 2004:

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

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

Lecture 7: Deterministic Bottom-Up Parsing

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

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

Compilers Crash Course

Formal Languages and Compilers Lecture VI: Lexical Analysis

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

Examination in Compilers, EDAN65

2068 (I) Attempt all questions.

CSCI Compiler Design

Part 5 Program Analysis Principles and Techniques

Introduction to Parsing. Lecture 8

CS 314 Principles of Programming Languages

Properties of Regular Expressions and Finite Automata

Compiling Regular Expressions COMP360

CS415 Compilers. Lexical Analysis

CS 314 Principles of Programming Languages

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.

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

UNIVERSITY OF CALIFORNIA

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

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

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

List of Figures. About the Authors. Acknowledgments

CS164: Midterm I. Fall 2003

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

Parsing II Top-down parsing. Comp 412

Compiler Optimisation

CSE302: Compiler Design

Problem Score Max Score 1 Syntax directed translation & type

Syntax Analysis, VI Examples from LR Parsing. Comp 412

CS 415 Midterm Exam Fall 2003

CSE 3302 Programming Languages Lecture 2: Syntax

CSE 413 Final Exam. December 13, 2012

ECE251 Midterm practice questions, Fall 2010

CSE au Final Exam Sample Solution

EECS 6083 Intro to Parsing Context Free Grammars

CSE-304 Compiler Design

CMSC 350: COMPILER DESIGN

Compiler Construction LECTURE # 1

Introduction to Parsing Ambiguity and Syntax Errors

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Chapter 2 - Programming Language Syntax. September 20, 2017

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

Part I: Multiple Choice Questions (40 points)

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Principles of Programming Languages COMP251: Syntax and Grammars

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12

MIDTERM EXAM (Solutions)

Introduction to Parsing Ambiguity and Syntax Errors

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

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

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

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

CMSC 330: Organization of Programming Languages

Introduction to Parsing. Lecture 5

Building a Parser Part III

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

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

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Transcription:

Name There are 8 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. You may refer to the following reference materials: Course lecture slides & notes Your primary compiler textbook (presumably Cooper & Torczon, Appel, or the dragon book) No other books or other materials. Please wait to turn the page until you are told to begin. Page 1 of 10

Score 1 2 3 4 5 6 7 8 (Remainder of this page intentionally left blank. Questions start on the next page.) Page 2 of 10

Question 1. (10 points) Regular expressions. Describe the set of strings that are generated by the each of the following regular expressions. a) (a (bc)* d)+ b) ((0 1)* (2 3)+) 0011 Question 2. (10 points) Draw a deterministic finite automata (DFA) that recognizes strings generated by (a (bc)* d)+. You do not need to use the formal algorithms for converting regular expressions to a NFA then a DFA (although those algorithms might provide some insight into what s needed). Just draw a suitable diagram. Page 3 of 10

Question 3. (10 points) A new programming language is being designed that includes decimal numbers with no exponent. A decimal number has a period to separate the integer and fractional parts of the number and must have at least one digit both before and after the decimal point. Furthermore, a decimal number must not have any superfluous leading or trailing 0 s. Some examples of legal decimal numbers are 3.14, 0.01, 17.0, 0.0, 123.00321. Some examples of illegal decimal numbers are 00.0,.5, 17, 17., 003.01, 0.0012300 (this last example is illegal because of the trailing 0 s; the 0 before the decimal point is required). Write a regular expression (or collection of regular expressions) to generate legal decimal numbers. Question 4. (10 points) Suppose we have the following fragment of a Java program (including some language operators that are not included in JFlat) while (xyzzy >= thing) // repeat until thing is big enough { x += y; thing++; } List in order the tokens that would be produced by a scanner when reading this input. (Use any reasonable set of token names; just be sure they are clear and descriptive.) Page 4 of 10

Question 5. (12 points) The standard programming language grammar for statements is ambiguous because of the dangling else problem. stmt ::= if ( expr ) stmt if ( expr ) stmt else stmt while ( expr ) stmt S Give an unambiguous grammar that defines all of the above statements and correctly handles the dangling else problem in the grammar. (Hint: introduce multiple productions for statements and divide the productions into two categories: those that might end in an if statement with no else clause a short if and those that definitely do not.) Page 5 of 10

Question 6. (20 points) Grammars and LR parsing. Consider the following grammar s ::= expr $ expr ::= a a subs subs ::= [ expr ] [ expr ] subs (In the first production, $ represents the end of file) a) (3 points) What are the terminals and non-terminals of this grammar? Terminals: Non-terminals: b) (3 points) Describe in English the set of strings generated by this grammar (continued next page) Page 6 of 10

Question 6. (Cont) LR parsing. CSE 582 Autumn 2002 Exam 11/26/02 (Grammar repeated for reference) s ::= expr $ expr ::= a a subs subs ::= [ expr ] [ expr ] subs d) (12 points) Construct (draw) the LR(0) state machine for this grammar. You do not need to write out the parser GOTO and ACTION tables, or compute FIRST and FOLLOW sets. Just draw the state machine. Be sure to show the sets of items in each state. e) (2 points) Is this grammar LR(0)? Why or why not? Page 7 of 10

Question 7. (18 points) x86 hacking. Consider the following C main program and function definition: int avg(int a, int b) { int ans; if (a == b) { return a/2; } else { ans = (a + b) / 2; return ans; } } void main() { int x; x = avg(17,42); } (a) (5 points) Draw a picture showing the layouts of the stack frames for methods avg and main. This should show the stack layout immediately after the prologue code of each function is executed, just before execution of the first statement of the function body (i.e., after the stack frame has been allocated). Be sure to show where registers ebp and esp point, and the location and offsets from ebp of each parameter and local variable. Page 8 of 10

Question 7. (cont) CSE 582 Autumn 2002 Exam 11/26/02 (b) (13 points) Translate both functions avg and main into x86 assembly language. You do not need to slavishly imitate the code shapes described in class straightforward x86 code is fine as long as it is correct, uses the registers properly, and obeys the x86 C language conventions for stack frame layout, function calls, and so on. It will help us grade your answer if you include the source code as comments near the corresponding x86 instructions. The C code is repeated here to save some page flipping. int avg(int a, int b) { void main() { int ans; int x; if (a == b) { x = avg(17,42); return a/2; } } else { ans = (a + b) / 2; return ans; } } Page 9 of 10

Question 8. (10 points) Code Shape Several languages in the ALGOL family had a loop statement designed strictly for counting loops. The syntax for a specific example is: for var := expr1 to expr2 by expr3 do statement The semantics of the for statement are to repeatedly execute the statement that is the loop body with var taking on values starting at expr1, continuing while the value in var is less than or equal to expr2, and incrementing var by expr3 after each execution of the loop body. All three expressions (expr1, expr2, and expr3) are evaluated only once before the first iteration of the loop. If expr1 is initially greater than expr2 then the loop body is not executed. The final by expr3 clause is optional; if it is omitted, by 1 is assumed instead. Describe the code shape that could be used to implement this statement on the x86 processor, in the same style that we ve used to show code shapes for other programming language constructs. Include the instructions and labels needed to implement the loop and be sure it is clear where the compiled code for the various expressions and the loop body would appear. If you introduce any temporary variables, be sure it is clear where they are stored (where in registers and/or memory). Page 10 of 10