CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved.

Similar documents
The PCAT Programming Language Reference Manual

The SPL Programming Language Reference Manual

CS558 Programming Languages

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Compiler Errors. Flash CS4 Professional ActionScript 3.0 Language Reference. 1 of 18 9/6/2010 9:40 PM

Syntax Errors; Static Semantics

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

CS164: Programming Assignment 5 Decaf Semantic Analysis and Code Generation

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

(Not Quite) Minijava

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

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

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Implementing a VSOP Compiler. March 20, 2018

Lecture 16: Static Semantics Overview 1

Compilers Project 3: Semantic Analyzer

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

QUIZ. What is wrong with this code that uses default arguments?

Semantic Analysis. Lecture 9. February 7, 2018

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Project 5 Due 11:59:59pm Wed, Nov 25, 2015 (no late submissions)

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

Introduction to Programming Using Java (98-388)

Reference Grammar Meta-notation: hfooi means foo is a nonterminal. foo (in bold font) means that foo is a terminal i.e., a token or a part of a token.

MP 3 A Lexer for MiniJava

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

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Principles of Programming Languages

CS558 Programming Languages

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

Lecture 15 CIS 341: COMPILERS

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

The Decaf Language. 1 Lexical considerations

Java Primer 1: Types, Classes and Operators

1 Lexical Considerations

Compilers CS S-05 Semantic Analysis

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Homework #3 CS2255 Fall 2012

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Ch. 11: References & the Copy-Constructor. - continued -

Pace University. Fundamental Concepts of CS121 1

CS558 Programming Languages

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

CSE 401 Midterm Exam Sample Solution 11/4/11

CA4003 Compiler Construction Assignment Language Definition

A Short Summary of Javali

Programming Assignment IV

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

CS S-06 Semantic Analysis 1

Fundamental Concepts and Definitions

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Overview of Semantic Analysis. Lecture 9

Type Checking. COMP 520: Compiler Design (4 credits) Alexander Krolik MWF 9:30-10:30, TR Bob, from Accounting.

Project 5 Due 11:59:59pm Tuesday, April 25, 2017

IC Language Specification

// the current object. functioninvocation expression. identifier (expressionlist ) // call of an inner function

Pierce Ch. 3, 8, 11, 15. Type Systems

COMP 105 Homework: Type Systems

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

The role of semantic analysis in a compiler

CSE 401 Midterm Exam Sample Solution 2/11/15

Project Overview. 1 Introduction. 2 A quick introduction to SOOL

CSCI 3155: Homework Assignment 4

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

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSE 431S Type Checking. Washington University Spring 2013

A Tour of the Cool Support Code

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

Programming Assignment IV Due Monday, November 8 (with an automatic extension until Friday, November 12, noon)

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Type Checking in COOL (II) Lecture 10

Project 2: Scheme Interpreter

The Decaf language 1

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Constants, References

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Semantic Analysis and Type Checking

Compiler construction 2009

Object Oriented Programming Exception Handling

MIT Semantic Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Program Correctness and Efficiency. Chapter 2

The Abstract Syntax Tree Differences Between Tolmach's and Porter's Representations

Absolute C++ Walter Savitch

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

CSE 5317 Midterm Examination 4 March Solutions

Project 2 Due 11:59:59pm Wed, Mar 7, 2012

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Project 4 Due 11:59:59pm Thu, May 10, 2012

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Assignment 4: Evaluation in the E Machine

Array. Prepared By - Rifat Shahriyar

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

CS11 Java. Fall Lecture 1

PIC 10B Lecture 1 Winter 2014 Homework Assignment #3

Transcription:

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright 2002-2012 by Andrew Tolmach. All rights reserved. Typechecking In this assignment, you will build a type-checker for the fab AST structures you built in assignment 3. To do this, you ll process declarations to extract type information about identifiers and process statements and expressions to make sure that identifiers and literals are used correctly. Your typechecker should detect all semantic errors, including: incompatible or illegal operand types in expressions, statements, or declarations; uses of undefined symbols; multiple declarations of a symbol within a single scope; attempting to assign to an identifier declared as being constant; incorrect number or types of arguments in a function call; exit statements in inappropriate contexts; return statements in inappropriate contexts, or lacking a return value when one is required or vice-versa; ill-formed record type definitions Do not attempt to check that array references are within bounds; in general, this cannot be done until runtime. As an (important!) side-effect, your typechecker must fill in the type field of all Exp and LValue nodes, and of any VarDec and ConstDec nodes where the concrete program text did not already specify a type. These type annotations will prove very useful in later stages of the compiler. They are now displayed (when present) by the AST tostring functions; a dash ( - ) is printed if the type field is null. For a modest amount of extra credit, you may choose to include a check that functions whose return type is not unit actually execute a return on all possible paths through the function body. This check will necessarily need to be conservative, i.e., you will have to reject some programs that actually always do execute a return. The more precise your check is, the more credit you get. But from the fab user s perspective, it is very important that checks of this kind be predictable, so you must include an English-language specification of how your return-checker behaves. Consult the fab Language Reference Manual for detailed rules about type compatibility. A full list of the error messages that you should be able to generate is given below. Your typechecker must be implemented within file Ast.java, as a new method in class Ast.Program, with signature 1

void check() throws CheckError where CheckError is a new class within Ast, defined as follows: public static class CheckError extends Exception { CheckError(int line, String text) { super("error at line " + line + ": " + text); If check encounters any type type error in the program, it should throw a CheckError exception with the line number at which the error occurs and a suitable explanatory message. Otherwise it should simply return silently. Here s the driver that will be used to test your checker: class CheckDriver { public static void main(string argv[]) throws Exception { try { Parser parser_obj = new Parser(new Scanner(System.in)); Ast.Program prog = (Ast.Program) parser_obj.parse().value; prog.check(); System.out.println(prog); catch (ParseError exn) { System.err.println(exn.getMessage()); catch (Ast.CheckError exn) { System.err.println(exn.getMessage()); As usual, the correct behavior of the the typechecker is more precisely specified by the behavior of the reference checker embedded in the WorkingChecker.jar file available on the web page. (To run this checker code, you should include WorkingChecker.jar in your classpath.) The type-annotated AST produced by your checker, and printed by the driver above, should be the same as the reference checker. Your checker should issue errors whenever (and only when!) the reference checker does so. The text of your error messages and the associated line numbers do not have to match the reference checker exactly, but they should convey essentially the same information. The reference parser includes a simple version of the extra-credit check for return statements, which considers all control paths through the program assuming that nothing is known about the value of any boolean expression. 2

Errors Generated Here is a list of all the error messages the reference parser generates. Most are self-evident; explanatory comments are given for a few. Identifier name is not defined This applies to type names in declarations and record and array constructors; l-values; and for -loop indices. Identifier name is already defined (at line linenum) This applies to record type definitions, variables, parameters, and function names. No identifier can be defined more than once at a given scope level. Identifier name is a type name and cannot be redefined Identifier name is not a type name This applies to type names in declarations and array constructors. Identifier name is a built-in name and cannot be redefined Variable initialized to nil must have explicit type constraint Constant initialized to nil must have explicit type constraint Type of initializing expression (type 1 ) does not match declared type (type 2 ) Duplicate field name name in record type declaration Cycle in record inheritance hierarchy Assignment LHS type (type 1 ) does not match RHS type (type 2 ) Function called in statement context must not return a value Applies to function call statements. Function called in expression context must return a value Applies to function call expressions. Function with return type type might not execute return statement Apply to function call expressions. (Generate these for extra credit.) Call to expression of non-function type Wrong number of arguments provided 3

Argument type (type 1 ) does not match declared type (type 2 ) for parameter name read statement argument has type type; must be integer or real write statement argument has type type; must be integer, real, boolean, or string Expression after if or elsif has type type; must be boolean Expression after while has type type; must be boolean Index of for statement has type type; must be integer Expression in for statement has type type; must be integer exit statement is not inside a while, loop, or for statement return statement not allowed in Main program body return expression type (type) does not match declared procedure return type (type) return missing result expression of type type return from function with result type unit does not allow result expression Operand has type type; must be integer or real Applies to various arithmetic operators. Operand types (type 1,type 2 ) do not match Functions cannot be compared for equality Apply to = and <> operators. Operand has type type; must be integer Applies to various arithmetic operators. Operand has type type; must be boolean Applies to various boolean operators. Array initializer count has type type; must be integer Type of array initializer value (type 1 ) does not match declared array element type (type 2 ) 4

Identifier name is not a record type name Applies to record constructors and extends clauses in record type definitions. Record initializer expression has missing field(s) Repeated field name in record initializer Type of expression (type 1 ) does not match type of field name (type 2 ) Undefined field name in record initializer Identifier name is not accessible in nested function Applies to non-const parameters and locals declared in an outer enclosing function. Identifier name cannot be assigned to Identifier name is not a variable Apply to l-values being assigned into. Identifier name is not a value Applies to l-values being dereferenced for their contents. Array subscript expression has type type; must be integer Subscripted expression is not an array Field name does not appear in this record type Dereferenced expression does not have record type Implementation and Program Submission The file Ast0.java on the course web page section for this assignment defines Ast classes very similar to the Ast.java file provided for homework 3, but also contains the skeleton of a typechecker that supports partial checking of fab. You are strongly urged (though not required) to use this code as the basis of your own checker. The skeleton checker omits support for the following language features: all aspects of boolean types; all aspects of arrays; most aspects of function declarations; read, if, while, and exit statements; all operators except for PLUS, MINUS, TIMES, and SLASH; and call expressions. To implement the missing features requires about 100 lines of new code. Most (but not all) places where you need to add code are marked with a comment NOT IMPLEMENTED. You may want to change the signatures of some of the existing routines too. In its present form, the skeleton checker will process the whole fab language without throwing any uncaught exceptions, but not correctly! In most cases, it will err by not issuing an error message where one is needed. In some cases it will issue inappropriate error messages (e.g., when the 5

program mentions true or false, or when the skeleton checker incorrectly returns the type of an expression as integer t) or assumes that every function type has an empty argument list. Here are few notes on the design of this checker; The checker maintains an environment of all the identifiers currently in scope, implemented as a linked list of the Binding objects; TypeBindings describe record types and the built-in types, and ValBindings describe variables, parameters, functions, and built-in constants. Each identifier in the environment has an associated level number, which indicates how deeply its declaration is nested. Built-in identifiers are at level 0; record types are at level 1; top-level identifiers are at level 2; identifiers defined within a top-level function are at level 3; identifiers defined within a level 3 function are at level 4; etc. Since fab primarily uses structural type equivalence, we need a structural representation of types. The checker uses the existing TypeExp AST class for this purpose; objects of this class must be synthesized by the checker at the few places where they don t appear in the source program already. Named types can be compared by doing string compares on the names. In addition to the user-visible built-in types (e.g., integer ), it is convenient to define a few internal built-in types (e.g., for strings). To avoid possible clashes with user-defined type names, these internal names begin with a character (?) that is illegal in concrete syntax identifiers. The TypeExp member functions equiv to and subtype of answer whether the receiving object is structurally equivalent to, or a subtype of, the argument object. In most cases, subtype of is the correct test to apply. Your revised Ast.java file should be submitted as a plain text attachment to a mail message sent to cs321-03@cecs.pdx.edu with subject line prfix [hw4]. Be sure to include your name in a comment within your submitted code file. Your code must work correctly with the provided (assignment 4) versions of CheckDriver.java, ParseError.java, Symbol.java, Scanner.java, Yylex.class, Parser.class, CUP$Parser$actions.class, and SymKinds.class. 6