Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

Similar documents
Type Checking. Error Checking

Type systems. Static typing

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

Type Checking. CS308 Compiler Theory 1

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

Intermediate Code Generation Part II

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Static Checking and Type Systems

Type Checking. Chapter 6, Section 6.3, 6.5

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Type Analysis. Type Checking vs. Type Inference

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

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Concepts Introduced in Chapter 6

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

a data type is Types

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

COP5621 Exam 3 - Spring 2005

5. Syntax-Directed Definitions & Type Analysis

Chapter-8 DATA TYPES. Introduction. Variable:

Principles of Programming Languages

Concepts Introduced in Chapter 6

Advanced Systems Programming

Semantic Analysis and Type Checking

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

The compilation process is driven by the syntactic structure of the program as discovered by the parser

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Tokens, Expressions and Control Structures

CSC 467 Lecture 13-14: Semantic Analysis

More On Syntax Directed Translation

Motivation was to facilitate development of systems software, especially OS development.

There is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar

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

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

Computational Expression

Variables. Data Types.

Lexical and Syntax Analysis. Abstract Syntax

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

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Motivation was to facilitate development of systems software, especially OS development.

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

Francesco Nidito. Programmazione Avanzata AA 2007/08

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Proposal for Extending the switch statement

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Type Conversion. and. Statements

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Type Checking. Prof. James L. Frankel Harvard University

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

Symbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes

Semantic Analysis with Attribute Grammars Part 5

Lecture 12: Data Types (and Some Leftover ML)

1 Lexical Considerations

Lexical Considerations

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Intermediate Code Generation

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

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

Syntax-Directed Translation Part II

Compilers CS S-05 Semantic Analysis

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CSE302: Compiler Design

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Industrial Programming

CSCI 2212: Intermediate Programming / C Review, Chapters 10 and 11

12 CREATING NEW TYPES

C Language Advanced Concepts. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Pointers, Pointers, Pointers!

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

Mosig M1 - PLSCD Written exam

Data Storage. August 9, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 August 9, / 19

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

Lexical Considerations

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

CPE Summer 2015 Exam I (150 pts) June 18, 2015

Low-Level C Programming. Memory map Pointers Arrays Structures

Input And Output of C++

EL2310 Scientific Programming

CPSC 427: Object-Oriented Programming

A JSON Data Processing Language. Audrey Copeland, Walter Meyer, Taimur Samee, Rizwan Syed

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Basic Types, Variables, Literals, Constants

Tutorial 5. Call Stack and Stack Frames. Memory Addresses and Pointers. Content vs Address of Pointers. scanf() function. Perils of Pointers

Formal Languages and Compilers Lecture X Intermediate Code Generation

Introduction to Programming Using Java (98-388)

Transcription:

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E if E then S while E do S S ; S

Type checking of statements The purpose is to recurse the program to find all the expressions in the statements. The statements themselves have no special type, i.e. they are void, unless they contain some type error in which case they are error. S id := E {S.type =if lookup(id.entry) = E.type then void else error } if E then { S 1 } {S.type =if E.type = bool then S 1.type else error } while E do{s 1 } {S.type =if E.type = bool then S 1.type else error } S 1 ; S 2 {S.type =if S 1.type = void and S 2.type = void then S 1.type else error }

Exercises Extend the grammar so that it is allowed to access array elements, like a[5] := a[7] mod 42 Add type checking semantics (SDD) for the new grammar rules. Extend the grammar to handle function calls in statements, like foo(a, b+c) Add type checking semantics (SDD) for the new grammar rules.

P D ; E {P.type := E.type } D D ; D id: T {addtype(id.lexval, T.type) } T char {T.type := char } integer {T.type := integer } array [num] of T 1 {T.type := array(t 1.type) } ^T 1 {T.type := pointer(t 1.type) } T 1 -> T 2 {T.type := T 1.type -> T 2.type } E literal {E.type := char } num {E.type := integer } id {E.type := lookup(id.lexval)} E 1 mod E 2 {E.type := if E 1.type = integer and E 2.type=integer then E 1.type else error} E 1 [E 2 ] {E.type := if E 1.type = array(t) and E 2.type=integer then t else error } E 1^ {E.type := if E 1.type = pointer(t) then t else error } E 1 (E 2 ) {E.type := if E 1.type = s -> t and E 2.type = s then t else error S id := E {S.type =if lookup(id.entry) = E.type then void else error } if E then { S 1 } {S.type =if E.type = bool then S 1.type else error } while E do{s 1 } {S.type =if E.type = bool then S 1.type else error } S 1 ; S 2 {S.type =if S 1.type = void and S 2.type = void then S 1.type else error }

Equivalence between type expressions Suppose we have two language constructs for which we need to compare the types, e.g. in void foo(struct A x, int y, float z);... foo(a, b, c); we need to compare the type of function call with the type of the declaration of foo. In particular this is an issue when comparing composed types. In general two type expressions are equivalent if they are The same basic types or Are constructed from the same type constructors applied to equivalent sub type expressions If the language supports naming of constructed types, the names may have the same role as the basic types (simplifying the type checking).

Equivalence between type expressions More specific: bool equiv(s, t) if s and t are the same basic type then return true else if s=array(s') and t=array(t') then return equiv(s', t') else if s=s' x s'' and t=t' x t'' then return equiv(s', t') and equiv(s'', t'') else if s=pointer(s') and t=pointer(t') then return equiv(s', t') else if s=s' -> s'' and t=t' -> t'' then return equiv(s', t') and equiv(s'', t'') else return false

Recursively defined data structures Assume this C-code: struct RECORD {void *data; struct RECORD *next;}; struct RECORD *record; What is the type of record? With name equivalence: pointer With structural equivalence: pointer struct RECORD x x x data pointer next pointer void

Type and name equivalence C uses name equivalens, counting struct <name> as a name: struct A {int x;}; struct B {int x;}; struct X {int x;}; typedef struct X C; typedef struct X D; void foo(void) { struct A a; struct B b; C c; D d; a = b; /* Error: struct A and struct B are different type names */ c = d; /* C and D are equal */ }

Type conversions Languages may allow for type conversion. This means to make a copy of a data object, where the copy has another type than the original. unsigned int x=42; (unsigned long)x Extend with a couple 0 bits. (float)x Convert to a completely different bit pattern, preserving the meaning of the content (eventually loosing some precision) (char*)x completely different. (struct A)x Keep the bit pattern but the meaning will be There is no possible meaning with this - error

Type conversions Checking of type for a + operator allowing for real and int, and allowing for implicit conversion of int to real in the language. E E 1 + E 2 {E.type= if E 1.type=int and E 2.type=int then int else if E 1.type=real and E 2.type=real then real else if E 1.type=int and E 2.type=real then real else if E 1.type=real and E 2.type=int then real else error }...

Type conversions Explicit type conversions will appear as operators to the type checker F id num (type) F! F Syntax tree for x + ( int ) y + id Compare to x +! y + toint id id! id