Type Checking Simple Variable Declarations

Similar documents
Type Checking Binary Operators

Type Checking Binary Operators

Classes, Structs and Records. Internal and External Field Access

Type Checking While Loops

CS 536 Spring Programming Assignment 1 Identifier Cross-Reference Analysis

CSX Code Generation Routines. Part IV

CS 536 Spring Programming Assignment 5 CSX Code Generator. Due: Friday, May 6, 2016 Not accepted after Midnight, Tuesday, May 10, 2016

CA4003 Compiler Construction Assignment Language Definition

CSX Code Generation Routines. Part I I

Lexical Considerations

1 Lexical Considerations

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Lexical Considerations

Operator Precedence a+b*c b*c E + T T * P ( E )

Introduction to Programming Using Java (98-388)

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Compilers Project 3: Semantic Analyzer

Let s look at the CUP specification for CSX-lite. Recall its CFG is

The SPL Programming Language Reference Manual

CS 536 Fall CSX Code Generation Routines. Part I

TML Language Reference Manual

Compilers. Compiler Construction Tutorial The Front-end

(Not Quite) Minijava

Motivation for typed languages

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Semantic Analysis Type Checking

CS201 Some Important Definitions

The Decaf Language. 1 Lexical considerations

IC Language Specification

Mandatory Exercise 1 INF3110

Decaf Language Reference

Decaf Language Reference Manual

The Decaf language 1

Section 2.2 Your First Program in Java: Printing a Line of Text

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

Typescript on LLVM Language Reference Manual

University of Arizona, Department of Computer Science. CSc 453 Assignment 5 Due 23:59, Dec points. Christian Collberg November 19, 2002

Operators. Java operators are classified into three categories:

The PCAT Programming Language Reference Manual

Short Notes of CS201

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CMSC 330: Organization of Programming Languages. Rust Basics

Bucket Sort Puzzlers. 08 A: Sorting IV. CS1102S: Data Structures and Algorithms. Martin Henz. March 12, 2010

CS143 Handout 03 Summer 2012 June 27, 2012 Decaf Specification

Type Checking. Prof. James L. Frankel Harvard University

CS201 - Introduction to Programming Glossary By

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

ASML Language Reference Manual

Introduction to C++ with content from

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

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Java Notes. 10th ICSE. Saravanan Ganesh

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Review of the C Programming Language for Principles of Operating Systems

Inheritance - Assignment5

Racket: Modules, Contracts, Languages

Semantic analysis. Semantic analysis. What we ll cover. Type checking. Symbol tables. Symbol table entries

Section 2.2 Your First Program in Java: Printing a Line of Text

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Expressions & Flow Control

The Arithmetic Operators

CSC 1214: Object-Oriented Programming

CS 536 Spring CSX Code Generation Routines. Part I

Operational Semantics. One-Slide Summary. Lecture Outline

DM550 Introduction to Programming part 2. Jan Baumbach.

DATA AND ABSTRACTION. Today you will learn : How to work with variables How to break a program down Good program design

COMP520 - GoLite Type Checking Specification

1B1b Classes in Java Part I

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

Protection Levels and Constructors The 'const' Keyword

C Pointers. 6th April 2017 Giulio Picierro


INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

by Pearson Education, Inc. All Rights Reserved.

GAWK Language Reference Manual

Computer Components. Software{ User Programs. Operating System. Hardware

The pixelman Language Reference Manual. Anthony Chan, Teresa Choe, Gabriel Kramer-Garcia, Brian Tsau

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

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

Chapter 8. Statement-Level Control Structures

JAVA OPERATORS GENERAL

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

3. Java - Language Constructs I

Concepts Introduced in Chapter 6

A Short Summary of Javali

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Variables and Operators 2/20/01 Lecture #

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Java Basic Programming Constructs

1. Every program must have at least one class declaration. (*) 2. Every class declared in a program must have a distinct identifier.

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

A First Program - Greeting.cpp

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

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

Continued from previous lecture

W3101: Programming Languages C++ Ramana Isukapalli

Programming in C++ 5. Integral data types

Transcription:

Type Checking Simple Variable Declarations vardeclnode identnode typenode 1. Check that identnode.idname is not already in the symbol table. 2. Enter identnode.idname into symbol table with type = typenode.type and kind = Variable. 358

Type Checking Initialized Variable Declarations vardeclnode identnode typenode expr tree 1. Check that identnode.idname is not already in the symbol table. 2. Type check initial value expression. 3. Check that the initial value s type is typenode.type 359

4. Check that the initial value s kind is scalar (Variable, Value or ScalarParm). 5. Enter identnode.idname into symbol table with type = typenode.type and kind = Variable. 360

Type Checking Const Decls constdeclnode identnode expr tree 1. Check that identnode.idname is not already in the symbol table. 2. Type check the const value expr. 3. Check that the const value s kind is scalar (Variable, Value or ScalarParm). 4. Enter identnode.idname into symbol table with type = constvalue.type and kind = Value. 361

Type Checking IdentNodes identnode 1. Lookup identnode.idname in the symbol table; error if absent. 2. Copy symbol table entry s type and kind information into the identnode. 3. Store a link to the symbol table entry in the identnode (in case we later need to access symbol table information). 362

Type Checking NameNodes namenode identnode expr tree 1. Type check the identnode. 2. If the subscriptval is a null node, copy the identnode s type and kind values into the namenode and return. 3. Type check the subscriptval. 4. Check that identnode s kind is an array. 363

5. Check that subscriptval s kind is scalar and type is integer or character. 6. Set the namenode s type to the identnode s type and the namenode s kind to Variable. 364

Type Checking Binary Operators binaryopnode expr tree expr tree 1. Type check left and right operands. 2. Check that left and right operands are both scalars. 3. binaryopnode.kind = Value. 365

4. If binaryopnode.operator is a plus, minus, star or slash: (a) Check that left and right operands have an arithmetic type (integer or character). (b) binaryopnode.type = Integer 5. If binaryopnode.operator is an and or is an or: (a) Check that left and right operands have a boolean type. (b) binaryopnode.type = Boolean. 6. If binaryopnode.operator is a relational operator: (a) Check that both left and right operands have an arithmetic type or both have a boolean type. (b) binaryopnode.type = Boolean. 366

Type Checking Assignments asgnode namenode expr tree 1. Type check the namenode. 2. Type check the expression tree. 3. Check that the namenode s kind is assignable (Variable, Array, ScalarParm, or ArrayParm). 4. If the namenode s kind is scalar then check the expression tree s kind is also scalar and that both have the same type. Then return. 367

5. If the namenode s and the expression tree s kinds are both arrays and both have the same type, check that the arrays have the same length. (Lengths of array parms are checked at runtime). Then return. 6. If the namenode s kind is array and its type is character and the expression tree s kind is string, check that both have the same length. (Lengths of array parms are checked at run-time). Then return. 7. Otherwise, the expression may not be assigned to the namenode. 368

Type Checking While Loops whilenode identnode stmtnode expr tree 1. Type check the condition (an expr tree). 2. Check that the condition s type is Boolean and kind is scalar. 3. If the label is a null node then type check the stmtnode (the loop body) and return. 369

4.If there is a label (an identnode) then: (a) Check that the label is not already present in the symbol table. (b) If it isn t, enter label in the symbol table with kind=visiblelabel and type= void. (c) Type check the stmtnode (the loop body). (d) Change the label s kind (in the symbol table) to HiddenLabel. 370

Type Checking Breaks and Continues breaknode identnode 1. Check that the identnode is declared in the symbol table. 2. Check that identnode s kind is VisibleLabel. If identnode s kind is HiddenLabel issue a special error message. 371

Type Checking Returns returnnode expr tree It is useful to arrange that a static field named currentmethod will always point to the methoddeclnode of the method we are currently checking. 1. If returnval is a null node, check that currentmethod.returntype is Void. 2. If returnval (an expr) is not null then check that returnval s kind is scalar and returnval s type is currentmethod.returntype. 372

Type Checking Method Declarations methoddeclnode identnode typenode args tree decls tree stmts tree 1. Create a new symbol table entry m, with type = typenode.type and kind = Method. 2. Check that identnode.idname is not already in the symbol table; if it isn t, enter m using identnode.idname. 3. Create a new scope in the symbol table. 4. Set currentmethod = this methoddeclnode. 373

5. Type check the args subtree. 6. Build a list of the symbol table nodes corresponding to the args subtree; store it in m. 7. Type check the decls subtree. 8. Type check the stmts subtree. 9. Close the current scope at the top of the symbol table. 374

Type Checking Method Calls callnode identnode args tree We consider calls of procedures in a statement. Calls of functions in an expression are very similar. 1. Check that identnode.idname is declared in the symbol table. Its type should be Void and kind should be Method. 2. Type check the args subtree. 3. Build a list of the expression nodes found in the args subtree. 375

4. Get the list of parameter symbols declared for the method (stored in the method s symbol table entry). 5. Check that the arguments list and the parameter symbols list both have the same length. 6. Compare each argument node with its corresponding parameter symbol: (a) Both should have the same type. (b) A Variable, Value, or ScalarParm kind in an argument node matches a ScalarParm parameter. An Array or ArrayParm kind in an argument node matches an ArrayParm parameter. 376