COMP 356 Sample Midterm Exam I

Similar documents
COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Context free grammars and predictive parsing

Bottom-Up Parsing. An Introductory Example

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

Theory of Compilation

Syntax Check of Embedded SQL in C++ with Proto

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Chapter 2: Elements of Java

Introduction to Java

Programming Languages CIS 443

Compiler Construction

CSCI 3136 Principles of Programming Languages

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Compiler I: Syntax Analysis Human Thought

Introduction to the 1st Obligatory Exercise

03 - Lexical Analysis

J a v a Quiz (Unit 3, Test 0 Practice)

Introduction to formal semantics -

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Flex/Bison Tutorial. Aaron Myles Landwehr CAPSL 2/17/2012

C++FA 5.1 PRACTICE MID-TERM EXAM

Lecture 9. Semantic Analysis Scoping and Symbol Table

The Cool Reference Manual

Compiler Construction

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Introduction to Python

Scanner. tokens scanner parser IR. source code. errors

Intel Assembler. Project administration. Non-standard project. Project administration: Repository

csce4313 Programming Languages Scanner (pass/fail)

CA Compiler Construction

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

Semantic Analysis: Types and Type Checking

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Intermediate Code. Intermediate Code Generation

The previous chapter provided a definition of the semantics of a programming

Example of a Java program

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

A Grammar for the C- Programming Language (Version S16) March 12, 2016

Pemrograman Dasar. Basic Elements Of Java

Yacc: Yet Another Compiler-Compiler

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Symbol Tables. Introduction

Programming Project 1: Lexical Analyzer (Scanner)

Compiler Design July 2004

Compilation 2012 Domain-Specific Languages and Syntax Extensions

C Compiler Targeting the Java Virtual Machine

Statements and Control Flow

Sources: On the Web: Slides will be available on:

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Computer Programming Tutorial

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

The C Programming Language course syllabus associate level

ANTLR - Introduction. Overview of Lecture 4. ANTLR Introduction (1) ANTLR Introduction (2) Introduction

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Syntaktická analýza. Ján Šturc. Zima 208

If-Then-Else Problem (a motivating example for LR grammars)

The Evolution of Programming Languages

Chapter 1 Java Program Design and Development

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Antlr ANother TutoRiaL

SML/NJ Language Processing Tools: User Guide. Aaron Turon

SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Organization of DSLE part. Overview of DSLE. Model driven software engineering. Engineering. Tooling. Topics:

7. Building Compilers with Coco/R. 7.1 Overview 7.2 Scanner Specification 7.3 Parser Specification 7.4 Error Handling 7.5 LL(1) Conflicts 7.

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Habanero Extreme Scale Software Research Project

Object-Oriented Software Specification in Programming Language Design and Implementation

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Moving from CS 61A Scheme to CS 61B Java

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

The International Journal of Digital Curation Volume 7, Issue

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Tutorial on C Language Programming

CUP User's Manual. Scott E. Hudson Graphics Visualization and Usability Center Georgia Institute of Technology. Table of Contents.

Computer Programming I

Parsing Expression Grammar as a Primitive Recursive-Descent Parser with Backtracking

Integrating Formal Models into the Programming Languages Course

Beyond the Mouse A Short Course on Programming

Approximating Context-Free Grammars for Parsing and Verification

Sample CSE8A midterm Multiple Choice (circle one)

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

CS143 Handout 18 Summer 2012 July 16 th, 2012 Semantic Analysis

Computer Programming I & II*

Anatomy of Programming Languages. William R. Cook

Part I. Multiple Choice Questions (2 points each):

Appendix B: Alloy Language Reference B.1 Lexical Issues

Transcription:

COMP 356 Sample Midterm Exam I 1. (8 points) What are the relative advantages disadvantages of implementing a programming language with an interpreter as compared with implementing the language with a compiler? Give at least two advantages for each approach. 2. Consider the following BNF definition: <S> <A><A> c<b><a> <A> a<a> <B> ǫ <B> c<b> c (a) (5 points) Give a rightmost derivation for the string: ccaa (b) (10 points) Show that the BNF definition is ambiguous. 1

3. (20 points) Consider adding Java-style switch statements to the small language for which we gave denotational semantics in class. The BNF definition of statements is then expanded to: <stmt>... switch (<expr>) {<cases>} <cases> case <expr>: <sl> break; <cases> ǫ The semantic function M stmt for switch statements is: M stmt (switch (<expr>) {<cases>}, s) = if s = then else let v = Mexpr(<expr>, s) in if v = then else Mcases(<cases>, v, s) Give the definition of the semantic function Mcases. As in Java, the values of the expressions for each case are tried in order, and when the value of one of these expressions matches the value of the selector (the selector is the expression immediately following the keyword switch), the statements in that case are executed. Because the syntax of this language requires a break after each case, no statements in following cases are executed. If none of the expressions in the cases have the same value as the selector, then none of the cases are executed. Note that the value of the selector is passed as the second argument to Mcases, so that Mcases is a function that takes three arguments (<cases>, a value and a state) and returns state. 2

4. (6 points) Write an EBNF definition the describes the set of all strings 0 s and 1 s that contain at least three 1 s. 5. (6 points) Give a regular expression that generates the set of all strings of a s, b s and c s such that all c s appear after any a. 6. (20 points) Consider the following grammar for boolean expressions, where expr is a nonterminal, and tokens LESS, GREATER, AND, OR, NOT, LPAR, RPAR and NUM have their usual meanings, i.e. LESS means < and so on, except that NUM represents only integers. expr expr LESS expr expr GREATER expr expr AND expr expr OR expr NOT expr LPAR expr RPAR NUM Write a CUP specification for a program that takes an expression formed according to this grammar as input, and prints the value of the expression as output. Boolean values are represented as integers: 0 is false, and any other integer is true. The result of applying any relational or boolean operator listed above is 0 or 1. You can assume that a lexical analyzer for this grammar is available, so you do not need to worry about where the input is coming from. Additionally, you can assume that the lexical analyzer sets the attribute value for token NUM to be the Integer lexeme for the token. The Java method to return the int from an instance of class Integer is intvalue(). You must declare the tokens listed above in your CUP specification, and you must ensure that the parser generated from your specification has no shift-reduce or reduce-reduce conflicts. The relational operators LESS and GREATER do not associate. All of the other binary operators are left associative, and NOT is right associative (at least for purposes of this problem). The precedence order from lowest to highest is: OR, then AND, then NOT and then the relational operators. That is, LESS and GREATER have equal (and highest) precedence. You do not need to provide the imports or directives sections of the CUP specification. Your CUP specification must print exactly one value as output. That is, your specification should print only the value of the entire input expression, and not the values of any subexpressions. Write your answer for this problem on the following page (which has intentionally been left blank). 3

This page intentionally left blank. 4

7. Consider the following declarations. typedef struct foo { int x; double y;} footype; typedef struct bar { int x; double y;} bartype; typedef footype baztype; typedef bartype bar2type; typedef baztype zabtype; footype a; bartype b; baztype c; zabtype d; bar2type f; (a) (5 points) List all variables whose type is compatible with the type of a under structural type compatibility. (b) (6 points) List all variables whose type is compatible with the type of a under the type compatibility rules of C++. (c) (5 points) List all variables whose type is compatible with the type of a under name type compatibility. 5