CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.

Slides:



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

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Chapter 2-2 A Simple One-Pass Compiler
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
C Language.
Chapter 6 Intermediate Code Generation
Intermediate Code Generation
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.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
8 Intermediate code generation
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.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
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.
Type Checking.
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.
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Fall 2003CS416 Compiler Design1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks –Static – done during.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Chapter 6 Type Checking Section 0 Overview
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Type Checking  Legality checks  Operator determination  Overload resolution.
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.
1 Pertemuan 11 Semantic Analysis Matakuliah: T0522 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
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.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Semantic Analysis CS 671 February 5, CS 671 – Spring The Compiler So Far Lexical analysis Detects inputs with illegal tokens –e.g.: main$
COP4020 Programming Languages Semantics Prof. Xin Yuan.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
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.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
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.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Windows Programming Lecture 03. Pointers and Arrays.
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
Semantic Analysis Type Checking
Context-Sensitive Analysis
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CS 153: Concepts of Compiler Design October 5 Class Meeting
Chapter 6 Intermediate-Code Generation
Overview of Compilation The Compiler BACK End
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
CMPE 152: Compiler Design September 27 Class Meeting
SYNTAX DIRECTED DEFINITION
Compiler Construction
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
Compiler Construction
Presentation transcript:

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible type? flow of control - does control pass properly from one construct to another? uniqueness - are objects multiply defined? name-checks - if required, do two references to a particular object use the same name?

CS3012: Formal Languages and Compilers Type Checking A type checker verifies that the type of a construct matches that expected by its context. We can define expected types by a type signature, which displays the operator, the expected types of the operands, and the type of the result. Example: _mod_ : integer x integer -> integer

CS3012: Formal Languages and Compilers Operator Overloading The "+" operator may be used in a number of different contexts: for example _+_ : integer x integer -> integer _+_ : integer x real -> real _+_ : real x integer -> real _+_ : real x real -> real In order to add together two different types in this way, some form of type translation is required. An operator which represents different operations in different contexts is called an overloaded operator

CS3012: Formal Languages and Compilers Basic and Constructed Types In many languages, types can be either basic or constructed. Basic types have, essentially, no internal structure: e.g. integer, real, boolean Constructed types are built from a number of basic types: e.g. arrays, structures,... Every language construct has a type associated with it, represented by a type expressions.

CS3012: Formal Languages and Compilers Type Expressions A type expression is a basic type, or is a type constructor applied to some type expressions. A basic type is a type expression. type_error is a basic type void is a basic type A type name is a basic type Type constructors include: arrays products records functions

CS3012: Formal Languages and Compilers arrays and products array If T is a type expression and I is some index set, then array(I,T) is a type expression. Example: var A : array[1..10] of integer; declares an array variable A with type expression of array(1..10, integer) product If T 1 and T 2 are type expressions, then T 1 x T 2 is a type expression.

CS3012: Formal Languages and Compilers records A record is a product with a name for its fields. The type constructor will operate on both the field names and types. Example: type row = record address : integer; lexeme : array(1..15) of char; end; declares a typename called row with the type expression: record((address x integer) x (lexeme x array(1..15,char)))

CS3012: Formal Languages and Compilers functions In maths, a function maps elements from one set into another. Here, a function will map elements of one type into elements of another type. function If T 1, T 2 and T 3 are type expressions, then T 1 x T 2 -> T 3 is a type expression Example: function f(a, b: char) : integer has the type expression: char x char -> integer

CS3012: Formal Languages and Compilers Types as Syntax Trees A type expression can be represented as a syntax tree: the leaf nodes are the basic types, and the interior nodes are the type constructors (or operators). T2T2 T1T1 product: x integer array

CS3012: Formal Languages and Compilers char array lexeme integeraddress x x x char integer x -> f: record:

CS3012: Formal Languages and Compilers Type Systems A type system is a collection of rules for assigning type expressions to the different parts of a program. A type checker is an implementation of a type system. A type checker must report the location nature of a type error, and should also recover to check the remainder of the code. Note that this may require a more complex system than for simply checking types.

CS3012: Formal Languages and Compilers Example Type Checker P -> D ; E D -> D ; D D -> id : T T -> char | integer | array[num] of T E -> num | id | E mod E | E [E] | id := E D -> id : T T -> char T -> integer T 1 -> array[num] of T 2 addtype(id.entry, T.type) T.type := char T.type := integer T 1.type := array(1..num.value, T 2.type)

CS3012: Formal Languages and Compilers E -> num E -> id E 1 -> E 2 mod E 3 E 1 -> E 2 [E 3 ] E 1 -> id := E 2 E.type := integer E.type := lookup(id.entry) E 1.type := if E 2.type = integer and E 3.type = integer then integer else type_error E 1.type := if E 3.type = integer and E 2.type = array(s,t) then t else type_error E 1.type := if lookup(id.entry) = E 2.type then void else type_error