Question Points Score

Similar documents
Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS 536 Midterm Exam Spring 2013

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

A simple syntax-directed

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

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

CSE 5317 Midterm Examination 4 March Solutions

COP4020 Spring 2011 Midterm Exam

CSCI312 Principles of Programming Languages

ECE251 Midterm practice questions, Fall 2010

CSCI Compiler Design

CSE 3302 Programming Languages Lecture 2: Syntax

A Simple Syntax-Directed Translator

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

CSE 401 Midterm Exam 11/5/10

Chapter 4. Abstract Syntax

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

CSE 413 Final Exam. June 7, 2011

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

CSE 401 Midterm Exam Sample Solution 11/4/11

Class Information ANNOUCEMENTS

Midterm I - Solution CS164, Spring 2014

The procedure attempts to "match" the right hand side of some production for a nonterminal.

CS606- compiler instruction Solved MCQS From Midterm Papers

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

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

Midterm I (Solutions) CS164, Spring 2002

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Abstract Syntax Trees & Top-Down Parsing

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

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

COMP3131/9102: Programming Languages and Compilers

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Question Marks 1 /16 2 /13 3 /12 4 /15 5 /8 6 /15 7 /8 8 /5 9 /8 Total /100

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CMSC330 Spring 2014 Midterm #2. Score 1 OCaml types & type

MIDTERM EXAMINATION - CS130 - Spring 2003

Table-Driven Top-Down Parsers

A programming language requires two major definitions A simple one pass compiler

CPS 506 Comparative Programming Languages. Syntax Specification

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

Lexical and Syntax Analysis (2)

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

CS 553 Compiler Construction Fall 2009 Project #1 Adding doubles to MiniJava Due September 8, 2009

CS 314 Principles of Programming Languages

Project Compiler. CS031 TA Help Session November 28, 2011

Top down vs. bottom up parsing

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Parsing - 1. What is parsing? Shift-reduce parsing. Operator precedence parsing. Shift-reduce conflict Reduce-reduce conflict

COP4020 Programming Assignment 2 Spring 2011

COP5621 Exam 3 - Spring 2005

CSE P 501 Exam 8/5/04

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

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

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

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

Syntactic Analysis. Top-Down Parsing

Chapter 3. Parsing #1

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

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

CSE 401 Midterm Exam Sample Solution 2/11/15

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

CSE P 501 Exam 11/17/05 Sample Solution

CSX-lite Example. LL(1) Parse Tables. LL(1) Parser Driver. Example of LL(1) Parsing. An LL(1) parse table, T, is a twodimensional

Problem Score Max Score 1 Syntax directed translation & type

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

CMSC330 Fall 2014 Midterm 2 Solutions

Syntax-Directed Translation. Lecture 14

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

How do LL(1) Parsers Build Syntax Trees?

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

Context-free grammars

LL parsing Nullable, FIRST, and FOLLOW

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

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

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

Compilers. Predictive Parsing. Alex Aiken

Compiler Construction: Parsing

CSE P 501 Exam 12/1/11

CS164: Midterm I. Fall 2003

CS 3360 Design and Implementation of Programming Languages. Exam 1

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

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

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

Lecture 8: Deterministic Bottom-Up Parsing

CSE431 Translation of Computer Languages

LL(k) Compiler Construction. Choice points in EBNF grammar. Left recursive grammar

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.

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

LL(k) Compiler Construction. Top-down Parsing. LL(1) parsing engine. LL engine ID, $ S 0 E 1 T 2 3

Topic 3: Syntax Analysis I

COP4020 Programming Assignment 2 - Fall 2016

Chapter 4. Lexical and Syntax Analysis

Transcription:

CS 453 Introduction to Compilers Midterm Examination Spring 2009 March 12, 2009 75 minutes (maximum) Closed Book You may use one side of one sheet (8.5x11) of paper with any notes you like. This exam has 9 pages, including this cover page and a blank page at the end. Do all your work on these exam sheets, use the backs of the pages if needed. Be specific and clear in your answers. If there is any question about what is being asked, then indicate the assumptions you need to make to answer the question. Show all your work if you wish to be considered for partial credit. Question Points Score 1 20 2 20 3 20 4 20 5 10 6 10 Name: Email: DO NOT TURN TO NEXT PAGE TILL YOU GET PERMISSION 1

1. [20 points] Expressions, Assignments, and Printlns in the MiniJava Compiler. Given the following set of statements, { } a = 2; System.out.println( 7 + 1 * a ); (a) draw the implicit parse tree, (b) draw the AST, (c) show the output based on an interpretation of the AST, and (d) write a MIPS main routine that performs the expressed computation assuming that the function printint is available. You can also write a comment to indicate what is being done once you have shown the MIPS code for a similar set of operations. For example, once you have pushed a constant value onto the stack, later instances of that operation can show up as the comment # push 5 onto stack. 2

problem 1 cont... 3

2. [20 points] Predictive Parse Table. Assuming that NUM, EOF, and REAL are all tokens, show the nullable property and the FIRST and FOLLOW sets for all of the nonterminals in the following grammar, which is a common input format for sparse matrices: (1) matrix -> NUM entry_list EOF (2) entry_list -> entry entry_list (3) entry_list -> epsilon (4) entry -> col row val (5) col -> NUM (6) row -> NUM (7) val -> REAL Using the FIRST and FOLLOW sets, construct the predictive parse table. 4

3. [20 points] Top-Down Parsing. For the following grammar, construct the recursive-descent parser functions for the nonterminals matrix and entry list. Use panic mode error handling. (1) matrix -> NUM entry_list EOF (2) entry_list -> entry entry_list (3) entry_list -> epsilon (4) entry -> NUM NUM REAL The grammar is a simplified version of the grammar in question 2. You can assume the functions match() and panic() are available. match(tok) { if(tok=lookahead) lookahead = scan(); else throw new SyntaxException(message); } panic( nonterminal ) { print error; while ( scan() not in (FOLLOW(nonterminal)) ) { } } 5

4. [20 points] Using JavaCUP. Assume a small scripting language that manipulates integers. The scripting language includes a made up operator that performs kiddition. When performing kiddition on two integers, the result is the integer value for the string resulting from concatenating the string representations for the two integers. For example, 42 987 evaluates to the integer 42987. The scripting language also includes the made up operator, which given any two integer operands always results in the value 1. For example, 42 987 evaluates to the integer 1. Assume that is right associative, and is left associative. Also assume that has higher precedence than. (a) What does the expression 5 67 6 789 1234 evaluate to? (b) Write a grammar for this language and use the JavaCUP precedence keyword to resolve ambiguity. You do not have to create a text version of the operators. Just use the symbols and as terminals. (c) Associate actions with the grammar rules that evaluate the expression being parsed. Assume that the type for the integer literal token NUM is a Java Integer. Recall that the Integer type has a tostring() method and that there is an Integer.parseInt() method. 6

5. [10 points] Syntax-Directed Translation. Write a syntax directed definition that creates a list where the identifiers are listed in reverse order of the list of identifiers being parsed. There is more than one way to do this. Assume that the ID token is of type String and use the following grammar: (1) id_list -> id_list ID (2) id_list -> /* epsilon */ 7

6. [10 points] lvalues and rvalues. Categorize all of the subexpressions in the following C program statement as lvalues being used as rvalues, lvalues being used as lvalues, or strictly rvalues. *(p+1) = 45 * (*q) + t->f rvalue lvalue used as rvalue lvalue 8

(empty page) 9