CSCE 531 Spring 2009 Final Exam

Similar documents
CSCE 531, Spring 2015 Final Exam Answer Key

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

Translator Design CRN Test 3 Revision 1 CMSC 4173 Spring 2012

CSE 582 Autumn 2002 Exam 11/26/02

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Syntax-Directed Translation. Lecture 14

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

Fall 2016 CSE Qualifying Exam CSCE 531, Compilers

Midterm I (Solutions) CS164, Spring 2002

CSE 401 Midterm Exam 11/5/10

CS164: Midterm I. Fall 2003

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

CSE 401 Midterm Exam Sample Solution 2/11/15

1 Lexical Considerations

2068 (I) Attempt all questions.

CMSC 330, Fall 2018 Midterm 2

CSE 413 Final Exam. June 7, 2011

Related Course Objec6ves

CSE P 501 Exam 8/5/04

Question Bank. 10CS63:Compiler Design

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

MIDTERM EXAMINATION - CS130 - Spring 2003

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

COP4020 Spring 2011 Midterm Exam

COP5621 Exam 3 - Spring 2005

S Y N T A X A N A L Y S I S LR

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

1. [5 points each] True or False. If the question is currently open, write O or Open.

The PCAT Programming Language Reference Manual

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

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

CS 44 Exam #2 February 14, 2001

Problem Score Max Score 1 Syntax directed translation & type

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

MIDTERM EXAM (Solutions)

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS 164 Handout 16. Final Examination. There are nine questions on the exam, some in multiple parts. You have 3 hours to work on the

COP4020 Programming Assignment 2 - Fall 2016

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

Principles of Programming Languages 2017W, Functional Programming

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

Chapter 4. Lexical and Syntax Analysis

CMSC 330: Organization of Programming Languages

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

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

Lexical Considerations

CS 415 Midterm Exam Spring SOLUTION

Midterm I - Solution CS164, Spring 2014

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

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 5317 Midterm Examination 4 March Solutions

Compiler Construction Assignment 3 Spring 2018

Fall 2009 CSE Qualifying Exam Core Subjects. September 19, 2009

Chapter 2 - Programming Language Syntax. September 20, 2017

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

Principles of Programming Languages COMP251: Syntax and Grammars

4. LEXICAL AND SYNTAX ANALYSIS

The SPL Programming Language Reference Manual

Language Reference Manual simplicity

Lexical Considerations

Chapter 3: Lexing and Parsing

Context-free grammars

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

CS 297 Report. By Yan Yao

CMSC 330, Fall 2018 Midterm 2

LL(1) predictive parsing

LR Parsing Techniques

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

CS 536 Midterm Exam Spring 2013

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

CSE 582 Autumn 2002 Exam Sample Solution

A simple syntax-directed

Compiler Construction: Parsing

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Test I

Programming Language Syntax and Analysis

Syntax. A. Bellaachia Page: 1

CS 415 Midterm Exam Fall 2003

Lexical Analysis (ASU Ch 3, Fig 3.1)

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

Definition: A context-free grammar (CFG) is a 4- tuple. variables = nonterminals, terminals, rules = productions,,

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

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

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Compiler Design Aug 1996

CSCI312 Principles of Programming Languages!

CSE Qualifying Exam, Spring February 2, 2008

UNIVERSITY OF CALIFORNIA

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

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

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Principles of Programming Languages COMP251: Syntax and Grammars

Theory Bridge Exam Example Questions Version of June 6, 2008

Transcription:

CSCE 531 Spring 2009 Final Exam Do all problems. Write your solutions on the paper provided. This test is open book, open notes, but no electronic devices. For your own sake, please read all problems before trying any of them the point value of a problem may not be commensurate with its difficulty. You need not show your work unless explicitly asked to do so, but if your answer is incorrect, you are more likely to get partial credit if you show your work. There are 100 points total in the exam. Any points earned beyond those limits counts as extra credit. 1. (10 points) Using the method described in the textbook or in class, convert the regular expression (ab c)* into an equivalent NFA (which can have -moves). Below is one possible answer. There are other correct answers. 3 a 5 6 b 8 2 1 4 c 7 2. (15 points total) A list is a finite sequence of things surrounded by parentheses. For example, (1 4 9 16) is a list of the first four perfect squares. (Items in the list are separated by whitespace if necessary.) Anything that is not a list is called an atom, e.g., numbers, symbols, strings, etc. The items of a list could be other lists, e.g., ((1 1) (1 2) (2 1) (2 2)). The empty list () with no items is also a list. 1

A generalized list is either an atom or a list whose items (if any) are themselves generalized lists. This is recursive definition, of course. So a generalized list could be a list of lists of lists.... (a) (5 points) Assuming three terminal symbols: (, ), and atom, give a grammar for a list of atoms suitable for bottom-up parsing. Express your grammar in abstract form, with start symbol A. A (L) L L atom L (b) (10 points) Assuming three terminal symbols: (, ), and atom, give a grammar for generalized lists suitable for bottom-up parsing. Express your grammar in abstract form, with start symbol B. B atom B (L) L LB L In both cases, you should assume that any whitespace is consumed by the lexical analyzer to separate items and is not itself a token. [Hint: Both answers are rather short.] 3. (20 points) Consider the following grammar with start symbol S for all strings of matching parentheses: S S S S(S) S Using the method described in class or the text, construct the set of states for a canonical LR(1) parser for this grammar, defining the transition function at the same time. Note that in class I denoted the transition function as trans. The textbook denotes the same function as goto. To ensure a unique correct answer, you must stick to the following rules of order, which mirror the order I used for my example in class: (a) Give each state as a list of LR(1) items, omitting the brackets. (b) Give the start state first, and denote it by s 0. Denote other states s 1, s 2,... in the order they are constructed. 2

(c) List the kernel items first in each state. List additional nonkernal items in the order that they enter the closure. (d) For each i 0, define all transitions out of s i before defining those out of s i+1. (e) When finding the transitions out of a state, or computing a closure, consider each item of the state in the order you listed it. (f) Do not list the empty set as a state. State Full form Short form s 0 (start state) S.S, $ S.S, $ S.S(S), $ S.S(S), $/( S., $ S., $/( S.S(S), ( S., ( s 1 = trans(s 0, S) S S., $ S S., $ S S.(S), $ S S.(S), $/( S S.(S), ( s 2 = trans(s 1, ( ) S S(.S), $ S S(.S), $/( S S(.S), ( S.S(S), )/( S.S(S), ) S., )/( S., ) S.S(S), ( S., ( s 3 = trans(s 2, S) S S(S.), $ S S(S.), $/( S S(S.), ( S.S(S), )/( S S.(S), ) S S.(S), ( s 4 = trans(s 3, ) ) S S(S)., $ S S(S)., $/( S S(S)., ( s 5 = trans(s 3, ( ) S S(.S), ) S S(.S), )/( S S(.S), ( S.S(S), )/( S.S(S), ) S., )/( S., ) S.S(S), ( S., ( s 6 = trans(s 5, S) S S(S.), ) S S(S.), )/( S S(S.), ( S S.(S), )/( S S.(S), ) S S.(S), ( s 7 = trans(s 6, ) ) S S(S)., ) S S(S)., )/( S S(S)., ( s 5 = trans(s 6, ( ) 3

4. (25 points total) Consider the following fragment of bison code for expressions with constants TRUE and FALSE, and boolean operators & (AND), (OR), and! (NOT): bool_expr: boolean_expr conjunction conjunction conjunction: conjunction & literal literal literal:! literal primary primary: TRUE FALSE ( bool_expr ) (a) (10 points) Give a parse tree with root bool expr for FALSE FALSE!(TRUE &!TRUE) bool_expr bool_expr conjunction bool_expr conjunction literal conjunction literal! literal literal primary primary primary FALSE ( bool_expr ) FALSE conjunction conjunction & literal literal! literal 4

primary primary TRUE TRUE (b) (15 points) Assuming each nonterminal has an integer attribute, with 0 representing FALSE and any nonzero value representing TRUE, add semantic actions to evaluate the truth value of a boolean expression. This evaluation takes place while the expression is parsed. Do not declare or use any variables besides $-variables (semantic stack items). bool_expr: boolean_expr conjunction { $$ = $1 $3 } conjunction conjunction: conjunction & literal { $$ = $1 && $3 } literal literal:! literal { $$ =!$2 } primary primary: TRUE { $$ = 1 } FALSE { $$ = 0 } ( bool_expr ) { $$ = $2 } 5. (10 points) Consider the following Pascal declaration: var a : array[3..7] of array[4..6] of Integer Assuming that a Pascal Integer is four bytes and that the base address of a is 1500, find the base address of the integer variable a[6][5]. The element type of the array a is itself an array of three integers, and so it has size 3 4 = 12. Variable Base Address a 1500 a[6] 1500 + (6 3) 12 = 1536 a[6][5] 1536 + (5 4) 4 = 1540 5

So the base address of a[6][5] is 1540. 6. (20 points total) Consider the following three-address code (line numbers added): 1 L1: i := a 2 L2: if i <= 10 then goto L4 3 i := i + 1 4 goto L2 5 L3: if j <= i then goto L5 6 L4: j := j + 1 7 goto L3 8 L5: a := a - 1 9 if a > 0 then goto L1 10 Assume that control enters at line 1 and that there are no other entry points. (a) (5 points) Describe the basic blocks B 1, B 2,... by giving an inclusive range of line numbers for each block. Block Line(s) B 1 1 B 2 2 B 3 3, 4 B 4 5 B 5 6, 7 B 6 8, 9 (b) (10 points) Draw the flow diagram as a directed graph, labeling the nodes B 1, B 2,.... Give dangling arrows both for the entry point and for the exit point of the code as a whole. (c) (5 points) Using the strict definition of a loop as defined in class and in the text, list sets of vertices that constitute loops. Label any inner loop(s) as such. Loops: {B 1, B 2, B 3, B 4, B 5, B 6 } {B 2, B 3 } (inner) {B 4, B 5 } (inner) 6