1 February 17, 2016 1 February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.

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.
CPSC 388 – Compiler Design and Construction
Semantic Analysis and Symbol Tables
Chapter 6 Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
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.
Compiler Construction
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Chapter 6 Type Checking Section 0 Overview
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.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
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.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
COP4020 Programming Languages
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 October 2, October 2, 2015October 2, 2015October 2, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
1 October 14, October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 October 16, October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1 Overview 1.1 Computer Systems 1.2 Programming and Problem Solving.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
May 31, May 31, 2016May 31, 2016May 31, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
1 November 1, November 1, 2015November 1, 2015November 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Static Checking and Type Systems Chapter 6. 2 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 January 18, January 18, 2016January 18, 2016January 18, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
More on Type Checking. Conversion and Coercion Int C; A = C;
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Data Types (3) 1 Programming Languages – Principles and Practice by Kenneth C Louden.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
March 19, March 19, 2016March 19, 2016March 19, 2016 Azusa, CA Sheldon X. Liang Ph. D. Software Engineering in CS at APU Azusa Pacific University,
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design – CSE 504 Type Checking
Semantic Analysis Type Checking
Constructing Precedence Table
Syntax-Directed Translation Part I
Semantic Analysis Chapter 6.
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Static Checking and Type Systems
Chapter 6 Intermediate-Code Generation
Compilers B V Sai Aravind (11CS10008).
Syntax-Directed Translation Part I
Compiler Construction
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Compiler Construction
Presentation transcript:

1 February 17, February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

2 Static Checking and Type Systems COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007 February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Semantic Analysis

3 The Structure of our Compiler Revisited February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

4 February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Keep in mind following questions Semantic Checking –S–Static –D–Dynamic –O–One/multi pass checking Type expressions –G–Graph representations –N–Name equivalence –S–Structural equivalence Constructing graphs by Yacc –C–Construct type nodes –S–SDD – attributed type production –Y–Yacc: auto tool for constructing

5 Static versus Dynamic Checking Static checking: the compiler enforces programming language’s static semantics –Program properties that can be checked at compile time Dynamic semantics: checked at run time –Compiler generates verification code to enforce programming language’s dynamic semantics February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

6 Static Checking Typical examples of static checking are –Type checks –Flow-of-control checks –Uniqueness checks –Name-related checks February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

7 Type Checks, Overloading, Coercion, and Polymorphism int op(int), op(float); int f(float); int a, c[10], d; d = c+d;// FAIL --> access c as an address *d = a;// FAIL --> d is not pointer a = op(d);// OK: overloading (C++) a = f(d);// OK: coersion of d to float vector v;// OK: template instantiation February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Type conversion is explicit, for example using type casts Type coercion is implicitly performed by the compiler

8 Flow-of-Control Checks myfunc() { … break; // ERROR } myfunc() { … switch (a) { case 0: … break; // OK case 1: … } myfunc() { … while (n) { … if (i>10) break; // OK } February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

9 Uniqueness Checks myfunc() { int i, j, i; // ERROR … } struct myrec { int name; }; struct myrec // ERROR { int id; }; February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction cnufym(int a, int a) // ERROR { … }

10 Name-Related Checks LoopA: for (int I = 0; I < n; I++) { … if (a[I] == 0) break LoopB; // Java labeled loop … } February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

11 One-Pass versus Multi-Pass Static Checking One-pass compiler: static checking for C, Pascal, Fortran, and many other languages is performed in one pass while intermediate code is generated –Influences design of a language: placement constraints Multi-pass compiler: static checking for Ada, Java, and C# is performed in a separate phase, sometimes by traversing the syntax tree multiple times February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

12 Type Expressions Type expressions are used in declarations and type casts to define or refer to a type –Primitive types, such as int and bool –Type constructors, such as pointer-to, array-of, records and classes, templates, and functions –Type names, such as typedefs in C and named types in Pascal, refer to type expressions February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

13 Graph Representations for Type Expressions int *f(char*,char*) fun argspointer char intpointer char pointer Tree forms fun argspointer char intpointer DAGs February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

14 Cyclic Graph Representations struct Node { int val; struct Node *next; }; struct val pointer int Cyclic graph next February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

15 Name Equivalence Each type name is a distinct type, even when the type expressions the names refer to are the same Types are identical only if names match Used by Pascal (inconsistently) type link = ^node; var next : link; last : link; p : ^node; q, r : ^node; With name equivalence in Pascal: p ≠ next p ≠ last p = q = r next = last February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

16 Structural Equivalence of Type Expressions Two types are the same if they are structurally identical Used in C, Java, C# = February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

17 Structural Equivalence of Type Expressions (cont’d) Two structurally equivalent type expressions have the same pointer address when constructing graphs by sharing nodes struct Node { int val; struct Node *next; }; struct Node s, *p; … p = &s; // OK … *p = s; // OK February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

18 Constructing Type Graphs in Yacc Type *mkint() construct int node if not already constructed Type *mkarr(Type*,int) construct array-of-type node if not already constructed Type *mkptr(Type*) construct pointer-of-type node if not already constructed February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

19 Syntax-Directed Definitions for Constructing Type Graphs in Yacc %union { Symbol *sym; int num; Type *typ; } %token INT %token ID %token NUM %type type % decl : type ID { addtype($2, $1); } | type ID ‘[’ NUM ‘]’ { addtype($2, mkarr($1, $4)); } ; type : INT { $$ = mkint(); } | type ‘*’ { $$ = mkptr($1); } | /* empty */ { $$ = mkint(); } ; February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

20 February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Got it with following questions Semantic Checking –S–Static –D–Dynamic –O–One/multi pass checking Type expressions –G–Graph representations –N–Name equivalence –S–Structural equivalence Constructing graphs by Yacc –C–Construct type nodes –S–SDD – attributed type production –Y–Yacc: auto tool for constructing

21 Thank you very much! Questions? February 17, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction