1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.

Slides:



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

Symbol Table.
Semantics Static semantics Dynamic semantics attribute grammars
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.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
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.
Type Checking.
Compiler Construction
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Chapter 9 Imperative and object-oriented languages 1.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Encapsulation by Subprograms and Type Definitions
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
Basic Elements of C++ Chapter 2.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Compiler Principle and Technology Prof. Dongming LU Apr. 4th, 2014.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Introduction to Computer Programming
CS 423 Compiler project notes Dept. of Comp. Sci. & Eng. Geunbae Lee.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
Chapter 3 Part II Describing Syntax and Semantics.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Programming Languages Third Edition Chapter 8 Data Types.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
6. Semantic Analysis. Semantic Analysis Phase – Purpose: compute additional information needed for compilation that is beyond the capabilities of Context-
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Data Types (1) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Scalar and composite data Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Lecture 9 Symbol Table and Attributed Grammars
Chapter Topics The Basics of a C++ Program Data Types
Principles of programming languages 12: Functional programming
COMPILER CONSTRUCTION
Computing and Statistical Data Analysis Lecture 2
Lecture 16: Introduction to Data Types
Basic Elements of C++.
C Basics.
Semantic Analysis Chapter 6.
Basic Elements of C++ Chapter 2.
Syntax Questions 6. Define a left recursive grammar rule.
CMPE 152: Compiler Design September 18 Class Meeting
Introduction to Abstract Data Types
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Semantic Analysis Chapter 6.
Compiler Construction
Compiler Construction
Variables and Constants
Presentation transcript:

1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi

Outline 1. Data Types & Type Checking 1. Type Expressions & Type Constructors 2. Type Names, Type Declarations, and Recursive Types 3. Type Equivalence 4. Type Inference and Type Checking 2. Summary 2

Semantic Analysis Lecture:

Practical Work Construct symbol table for following code; int a; int function(int a, int b) { string temp; temp = “Hello”; } 4

Data Types & Type Checking One of the principal tasks of a compiler is the computation and maintenance of information on data types (type inference) Compiler uses this information to ensure that each part of the program makes sense under the type rules of the language (type checking) Data type information can occur in a program in several different forms Theoretically, a data type is a set of values, or more precisely a set of values with certain operations on those values 5

Data Types & Type Checking (Continue…) For instance, data type integer in a programming language refers to a subset of mathematical integers, together with the arithmetic operations These sets in compiler constructions are described by a type expression Type expressions can occur in several places in a program 6

Type Expressions & Type Constructors A programming language always contain a number of built-in types These predefined types correspond either to numeric data types like int or double OR they are elementary types like boolean or char Such data types are called simple types, in that their values exhibit no explicit internal structure An interesting predefined type in C language is void type  This type has no values, and so represents empty set 7

Type Expressions & Type Constructors (Continue…) In some languages it is possible to define new simple types  subrange in Pascal and enumerated types in C In Pascal, subrange of integers from 0 to 9 can be declared as type Digit = 0..9; In C, an enumerated type consisting of named values can be declared as typedef enum {red, green, blue} Color; 8

Type Expressions & Type Constructors (Continue…) Given a set of predefined types, new data types can be created using type constructors, such as array and record, or struct Such constructors can be viewed as functions that take existing types as parameters and return new types with a structure that depends on the constructor Such types are called structured types 9

Type Names, Type Declarations, and Recursive Types Languages that have a rich set of type constructors usually also have a mechanism for a programmer to assign names to type expressions Such type declarations (sometimes called type definitions) can be done in C as follows struct RealIntRec { double r; int I; }; 10

Type Names, Type Declarations, and Recursive Types (Continue…) Type declarations cause the declared type names to be entered into the symbol table just a s variable declarations cause variable names to be entered Type names are associated with attributes in the symbol table in a similar way to variable declarations These attributes include scope and type expressions corresponding to the type name Since type names can appear in type expressions, question arise about the recursive use of type names 11

Type Names, Type Declarations, and Recursive Types (Continue…) In C programming language, recursive type names cannot be declared directly because at time of declaration it is unknown that how much memory be required for the structure; struct intBST { int val; struct intBST *left, *right; }; 12

Type Equivalence Given the possible type expressions of a language, a type checker must frequently answer the question of when two type expressions represent the same type This is the question of type equivalence There are many possible ways for type equivalence to be defined by a language Type equivalence checking can be seen as a function in a compiler function typeEqual( t1, t2, TypeExp ) : Boolean 13

Type Equivalence (Continue…) The typeEqual() function takes two type expressions and returns true if they represent the same type according to the type equivalence rules of the language One issue that relates directly to the description of type equivalence algorithm is the way type expressions are represented within a compiler One straightforward method is to use a syntax tree representation 14

Type Inference & Type Checking Type checking is described in terms of semantic actions based on representation of types and a typeEqual() operation. Compiler needs symbol table as well for this purpose along with three of its basic operations insert, lookup, and delete 15

Type Inference & Type Checking (Continue…) Consider the following grammar; 16

Type Inference & Type Checking (Continue…) 17

18 Summary Any Questions?