Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Semantic Analysis and Symbol Tables
Symbol Table.
Chapter 6 Intermediate Code Generation
Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
Compiler Construction
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Fall 2003CS416 Compiler Design1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks –Static – done during.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Chapter 6 Type Checking Section 0 Overview
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Types Type = Why? a set of values
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
Imperative Programming
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
ISBN Chapter 7 Expressions and Assignment Statements.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design – CSE 504 Type Checking
Context-Sensitive Analysis
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS 326 Programming Languages, Concepts and Implementation
Programming Languages and Paradigms
Programming Paradigms
CMPE 152: Compiler Design September 18 Class Meeting
Chapter 6 Intermediate-Code Generation
CMPE 152: Compiler Design October 4 Class Meeting
SYNTAX DIRECTED DEFINITION
The Data Element.
Compiler Construction
The Data Element.
Programming Languages and Paradigms
Compiler Construction
Presentation transcript:

Chapter 6 Type Checking

The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without running the program. It is a static check as opposed to a dynamic check which is performed during run time. Examples: Flow-of-Control: if control leaves a construct is there a place for it to go to? An example is a break statement in C not enclosed within a while, for, or, switch statement. Uniqueness: Each variable should only be declared once. A function or procedure should only be defined once. Labels in a case statement should be unique.

Basic types are atomic types with no internal structure visible to the programmer. Pascal examples are: boolean, character, integer and real. Subrange types like 1….10 and enumerated types, like (violet, indigo, blue, green, yellow, orange, red) are also basic types. Constructed types built from other types. Pascal examples are arrays, records, sets and pointers. The type of a language construct is denoted by a type expression. Type expressions are basic types or constructions of type expressions. Basic type expressions: Includes the basic types of the language like boolean, character, integer and real. A special basic type expression is type_error to signal the occurrence of a type check error.

Statements have no values and are assigned the basic type expression of void. Type names are basic type expressions. Array(I, T) is a type expression denoting the type of an array with elements of type T and index set I. Cartesian product: If T1 and T2 are type expressions then T1 * T2 is a type expression. The type of a list is the Cartesian product of the types of its elements. E.g. if a and b are of type real then a, b is of real * real type.

Records: The type expression for a record with fields F1, F2, …, Fn of types T1, T2, …, Tn resply is : record ((F1 * T1) * (F2 * T2)* … * (Fn * Tn)). Pointers: If T is a type expression then pointer(T) is the type expression for a pointer to an object of type T. Functions: The type expression for a function is D  R where D is the type expression for the domain of the function and R is the type expression for the range of the function. If the function has no arguments then the domain type is void. Procedures: A procedure does not return any value so it can be treated like a function where the type expression for the range is void.

A Simple Type Checker The grammar is : prog  decls; expr decls  decls; decls | id : type type  char | integer | array[num] of type | type expr  literal | num | id | expr mod expr | expr [expr] | expr A prog has decls written before expr so the types of all declared identifiers can be saved in the symbol table before the expression is checked. A translation scheme to save the types of identifiers is:

prog  decls; expr decls  decls; decls decls  id : type {addtype(id.entry, T.type)} type  char {T.type := char} type  integer {T.type := integer} type  type1 {T.type := pointer(T1.type)} type  array [num] of type1 {T.type := array (1..num.val, T1.type)}

Type checking expressions We call a function lookup (e) to fetch the type of entry e in the symbol table. A translation scheme to type check expressions is : expr  literal {E.type := char} expr  num {E.type := integer} expr  id {E.type := lookup (id.entry)} expr  expr1 mod expr2 {E.type := if E1.type = integer and E2.type = integer then integer else type_error} expr  expr1 [ expr2 ] {E.type := if E2.type = integer and E1.type = array (s,t) then t else type_error} expr  expr1 {E.type := if E1.type = pointer (t) then t else type_error}

Type Checking Statements The type expression of a statement is void if it is correct or type_error if a type error is found. E.g. prog  decls; stmt stmt  id := expr {S.type := if lookup (id.entry) = E.type then void else type_error} stmt  if expr then stmt1 {S.type := if E.type = boolean then S1.type else type_error} stmt  while expr do stmt1 {S.type := if E.type = boolean then S1.type else type_error} stmt  stmt1 ; stmt2 {S.type := if S1.type = void then S2.type else type_error}

Type checking of calls to no-argument functions. The statement a := b sets a to the value returned by function b if b is a function or to the value of b if b is a variable. expr  id {E.type := if lookup(id.entry) = (void  t) or lookup (id.entry) = t then t else type_error} Type checking of procedure statements stmt  id (expr) {S.type := if E.type = e and lookup(id.entry) = (e  void) then void else type_error} stmt  id {S.type := if lookup (id.entry) = (void  void) then void else type_error}

Two standard types : integer and real Four types of variables: integer, real, integer array and real array The index sets of all arrays are integers. We won’t keep track of the limits in type checking. Factors, terms and expressions maybe: integer, real or boolean Each argument of a functinon or a procedure maybe: integer, real, integer array or real array Functions return either integers or reals The symbol table contains the names of variables, functions and procedures

Encoding of type expressions: the alphabet has 8 symbols representing integer, real, integer_array, real_array, boolen, void, type_error and . The type expression of an expression_list or a parameter_list is a string of symbols with one symbol for each element of the list. The  symbol is used to encode functions and procedures: The type expression for a real function of an integer and a real is: integer real  real The type expression for an integer function with no arguments is: void  integer The type expression for a procedure with an integer and a real argument is: integer real  void The type expression for a procedure with no arguments is: void  void The type expressions for functions and procedures in the symbol table could be stored in the name array