RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 2016

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

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

Chapter 4. Abstract Syntax

1 Lexical Considerations

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

CPS 506 Comparative Programming Languages. Syntax Specification

JJTree. The Compilation Task. Automated? JJTree. An easier way to create an Abstract Syntax Tree

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

CS164: Midterm I. Fall 2003

Lexical Considerations

Question Points Score

Lexical Considerations

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

Syntax-Directed Translation. Lecture 14

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

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

CSE3322 Programming Languages and Implementation

COP5621 Exam 3 - Spring 2005

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

CSE 401 Midterm Exam 11/5/10

A simple syntax-directed

Programming Lecture 3

CS 164 Programming Languages and Compilers Handout 8. Midterm I

Lexical and Syntax Analysis. Top-Down Parsing

CSE 12 Abstract Syntax Trees

Project Compiler. CS031 TA Help Session November 28, 2011

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

ECE251 Midterm practice questions, Fall 2010

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions. Monday, September 28, 15

Computational Expression

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

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

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

COMPILER DESIGN. For COMPUTER SCIENCE

Semantic actions for expressions

Programming in C++ 5. Integral data types

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

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

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

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

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

A Simple Syntax-Directed Translator

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

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Quiz 5. Exam 1 overview. Type checking basics. HW4 due. HW5 out, due in 2 Tuesdays

Semantic actions for declarations and expressions

Chapter 3. Describing Syntax and Semantics

CS 406: Syntax Directed Translation

Parsing CSCI-400. Principles of Programming Languages.

Definition of AST AST and Static Code Analysis Design Patterns. AST Features Applicability of AST JTB JJTree

Syntax and Grammars 1 / 21

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

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

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

MIDTERM EXAMINATION - CS130 - Spring 2003

List of Figures. About the Authors. Acknowledgments

Tree visitors. Context classes. CS664 Compiler Theory and Design LIU 1 of 11. Christopher League* 24 February 2016

Lexical and Syntax Analysis

CPSC 411, 2015W Term 2 Midterm Exam Date: February 25, 2016; Instructor: Ron Garcia

Compiler Compiler Tutorial

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Error Detection in LALR Parsers. LALR is More Powerful. { b + c = a; } Eof. Expr Expr + id Expr id we can first match an id:

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Decaf Language Reference

CSE 401 Midterm Exam Sample Solution 2/11/15

CS 536 Midterm Exam Spring 2013

Introduction to Parsing. Lecture 8

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

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

Introduction to Parsing. Lecture 5

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Visual C# Instructor s Manual Table of Contents

CSCI Compiler Design

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

Programming in C++ 4. The lexical basis of C++

Comp 311: Sample Midterm Examination

L-System Fractal Generator: Language Reference Manual

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

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

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

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

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

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Getting started with Java

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

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

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Question Marks 1 /11 2 /4 3 /13 4 /5 5 /11 6 /14 7 /32 8 /6 9 /11 10 /10 Total /117

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Language Reference Manual simplicity

The role of semantic analysis in a compiler

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Syntactic Analysis. Top-Down Parsing

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

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

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Transcription:

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 2016 NAME: STUDENT ID: INSTRUCTIONS Please answer directly on this exam. This exam has 4 questions, and is worth 40% of the course mark. It has 8 pages including this one NO AIDS ARE ALLOWED. Throughout this exam, non-terminals in grammars are in UPPER-CASE and terminals are in a shaded box. A - General Concepts 30 B - Parsing 20 C - Grammars 25 D - Evaluation 25

CPS 710 FINAL EXAM F2016-2 Part A - General Concepts - 30 marks A1 (2 marks) What is a self-hosting compiler or interpreter? A2 (3 marks) What is a virtual machine and why is it useful? A3 (8 marks) What would be the output of the following program fragment: integer i = 1; integer function f(integer n) { i += n; return i; } integer function g(integer n) { integer i = n; return f(n); } display g(2); display f(1); If the language is statically scoped? If the language is dynamically scoped?

CPS 710 FINAL EXAM F2016-3 A4 (4 marks) List at least 4 errors that can only be detected once the symbol table is implemented A5 (13 marks) As you know, Java integer literals are mostly strings of 1 or more digits. However, there is more to them: 1. Underscores ( _ ) can be embedded in integer literal strings as long as they are not the first or last digit of the integer literal string. (These underscores are just placeholders to make it easier to read integers with many digits) 2. Integers can be represented in four bases, which is indicated by the prefix, as follows: Binary integers start with the string 0b. The only digits allowed are 0 and 1. Hexadecimal integers start with the string 0x. The digits allowed are 0 to 9 and A to F Octal integers start with the string 0. The digits allowed are 0 to 7. Decimal integers start with a digit other than 0. The digits allowed are 0 to 9. 3. Finally, the fact that an integer is a Long is indicated by adding the suffix L to the string So for example 0b100_000_101L is the binary number 100000101 stored as a Long. Write a regular expression that can be used to scan any integer literal. You can use regular definitions if you want. You can also use the standard regular expression notational shorthands: +,?, and [a-z] for character classes.

CPS 710 FINAL EXAM F2016-4 Part B - Parsing ( 20 marks) B1 First and Follow (11 marks) Fill out the tables calculating the first and follow sets based on the following LL(1) grammar for structs in the C language: (1) S STRUCT STRUCTS $ First (RHS of production) (2) STRUCT struct id { MEMBERS } ; (3) STRUCTS STRUCT STRUCTS (4) STRUCTS (5) MEMBERS MEMBER MEMBERS (6) MEMBERS (7) MEMBER int id ; (8) MEMBER float id ; S STRUCT STRUCTS MEMBERS MEMBER First Follow B2 LL(1) table (9 Marks) Fill in the LL(1) table for this grammar, by entering the number of the production that is applied (e.g. (3)) in each cell where a production is applied. struct id { } ; int float $ S STRUCT STRUCTS MEMBERS MEMBER

CPS 710 FINAL EXAM F2016-5 Part C - Grammars - 25 marks C1 (6 marks) Left-factor the following set of productions fully. You can introduce new non-terminals if you wish. DEFINITION FUNCTIONDEF VARIABLEDEFS FUNCTIONDEF type id ( PARAMETERS ) VARIABLEDEFS type id (, id )* VARIABLEDEFS type id [ DIMENSIONS ] C2 (7 points) Remove the left recursion from the following set of productions. You can assume that the two logical operations are left-associative and have the same precedence. S S S S S S S true false

CPS 710 FINAL EXAM F2016-6 C3 (6 marks) In this question integer refers to an integer token and not the word integer. Why is the following set of productions ambiguous? TERM integer POLYN TERM ( ^ integer )? POLYN integer X ( ^ integer )? C4 (6 marks) Rewrite the grammar in C3 to remove the ambiguity while describing the same language.

CPS 710 FINAL EXAM F2016-7 Part D - Evaluation - 25 points Preliminary Explanation In this question you will be evaluating vector comparisons in a programming language for vectors. In this language vectors are ordered lists of integers and vectors. The elements of a vector do not have to all be the same type. For example, [[10,3], 2, -1, [[[3],-4], 5]] is a vector The section of the grammar which deals with vector comparisons is: COMPARISON EXPRVEC = EXPRVEC EXPRVEC [ EXPRLIST ] EXPRLIST EXPRESSION (, EXPRLIST)* EXPRLIST You have written a parser for this language using javacc and jjtree. This parser produces the following types of AST nodes to deal with comparison expressions: ASTcomparison has two ASTexprvec children ASTexprvec has n ASTexpression children, where n 0. (There is no ASTexprlist the parser optimized the data structures to put the list of expressions directly into the ASTexprvec.). ASTexpression can be treated like a black box in this assignment. It represents combinations of operations on vectors and integers. ASTinteger stores integer literals (constants). It has the following special method: public int getvalue() //returns the integer value of the literal ASTvector stores vector literals. An ASTvector has 0 or more children. Each child is either an ASTinteger or an ASTvector. You have written an evaluator visitor for the entire language except for the ASTcomparison node. In particular these visitors are already written: public Object visit(astexpression node, Object data) // returns either an ASTinteger or an ASTvector public Object visit(astvector node, Object data) // returns node (because no further evaluation is necessary) public Object visit(astinteger node, Object data) // returns node (because no further evaluation is necessary) Exam Question On the next page, write this visitor method in Java: public Object visit(astcomparison node, Object data) // Returns Boolean.TRUE if the two children of node evaluate to the same value // Returns Boolean.FALSE otherwise Other requirements: Your evaluator should be as efficient as possible, short-circuiting the evaluation when possible: i.e. expressions should only be evaluated if absolutely necessary. You can assume that the nodes you are evaluating are error-free. In particular, you do not need to perform any type checking or catch any exceptions. Useful AST node methods: public int getid() // Returns the ID of the node (i.e. a number representing its type) // The following two IDs will be useful: //HLTreeConstants.JJTINTEGER and HLTreeConstants.JJTVECTOR int jjtgetnumchildren(); // Return the number of children the node has. public Node jjtgetchild(int i); // Returns the ith child node. The children are numbered from zero, left to right.

CPS 710 FINAL EXAM F2016-8 STUDENT ID: public Object visit(astcomparison node, Object data) // Returns Boolean.TRUE if the two children of node evaluate to the same value // Returns Boolean.FALSE otherwise {