Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.

Slides:



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

CPSC 388 – Compiler Design and Construction
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.
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.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Chapter 6 Type Checking Section 0 Overview
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
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.
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.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Runtime Environments Compiler Construction Chapter 7.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Semantic Analysis1 Checking what parsers cannot.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COMPILER CONSTRUCTION
Symbols and Type-Checking CPSC 388 Ellen Walker Hiram College.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
Compiler Principle and Technology Prof. Dongming LU Apr. 4th, 2014.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Bernd Fischer RW713: Compiler and Software Language Engineering.
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.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
6. Semantic Analysis. Semantic Analysis Phase – Purpose: compute additional information needed for compilation that is beyond the capabilities of Context-
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Names, Scope, and Bindings Programming Languages and Paradigms.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
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.
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.
Windows Programming Lecture 03. Pointers and Arrays.
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
Functions.
Names and Attributes Names are a key programming language feature
COMPILER CONSTRUCTION
CSE 3302 Programming Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
C Basics.
Semantic Analysis Chapter 6.
CSE 3302 Programming Languages
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Semantic Analysis Chapter 6.
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Compiler Construction
CSE 3302 Programming Languages
Compiler Construction
Presentation transcript:

Semantic Analysis Chapter 6

Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization

Static Semantic Analysis  Build symbol table  Keep track of declarations  Perform type checking

Static Analysis  Description –Attributes (properties)  Implementation –Attribute equations (semantic rules) –Application of rules Syntax-directed semantics

General Attribute  Property of the Language –Data type –Value of expressions –Location of variables in memory –Object code of procedure –Number of Significant digets

Specific Attributes  Parameters/Arguments type  Parameters/Arguments number  Array subscript type  Array subscript number  Continue with no place to continue to  Variable undeclared  Variable duplicately declared  Scope  Incorrect structure reference

Specific Attributes Cont.  Break inappropriate  Incorrect Return –Wrong type –Array –None when needed (void)  No main  Two main’s  Constant on left side  Expression types

Binding Time of Attributes  Static - prior to execution –Fortran  Dynamic - during execution  Combination –C –Java –Pascal

Attribute Grammars  X is grammar symbol, X a is an attribute for this symbol X  ABCD (grammar) X  ABCD (grammar) X.x = A.a B.b C.c D.d X.x = A.a B.b C.c D.d (attribute grammar) (attribute grammar)

Attribute Grammar Example  E 1  E 2 + T E 1. type = E 2.type + T.type E 1. type = E 2.type + T.type

Attribute Grammar Example  decl  type var-list var-list.dtype =type.dtype var-list.dtype =type.dtype  type  int type.dtype = integer  type  float type.dtype = float  var-list1  id, var-list2 id.dtype = var-list1.dtype id.dtype = var-list1.dtype var-list2.dtype = var-list1.dtype var-list2.dtype = var-list1.dtype  var-list  id id.dtype = var-list.dtype

Attribute Grammar Comments  Symbols may have more than one attribute  The grammar is not the master  More of a guide

Attribute Grammar Example  E 1  E 2 + T E 1. tree = E 1. tree = mkOpNode(+, E 2.tree, T.tree) mkOpNode(+, E 2.tree, T.tree)  E  T E.tree = T.tree E.tree = T.tree  F  number F.tree = mkNumNode(number.lexval) F.tree = mkNumNode(number.lexval)

Attribute Up and Down Dependency Tree  Synthesized –Point from child to parent  Inherited –Point child to child or parent to child

Symbol Tables  Lists of Lists  Hash –Collision resolving by use of buckets –Collision resolving by probing  …

Symbol Tables  Keep track of identifiers  Must deal with scope efficiently

Code Fragment int f(int size) { char i, temp; … { double j, i; { double j, i; } { char * j; { char * j; *j = i = 5; *j = i = 5; }}

Static vs Dynamic Scope int i = 1; void f(void) { printf(“%d\n”,i); } void main(void) { int i = 2; f(); f(); return; return;} What is printed?

Kinds of Declarations  sequential –C  collateral –scheme –ML  recursive –requires the function name to be added to the symbol table before processing the body of the function

Example - Sequential/Colateral int i = 1; void f(void) { int i = 2, j = i + 1; int i = 2, j = i + 1; …} Is j 2 or 3?

Example - Recursive int gcd(int n, int m) { if (m == 0) return n; else return gcd(m, n%m); else return gcd(m, n%m);} gcd must be added to the symbol table before processing the body

Example - Recursive void f(void) { … g() … } void g(void) { … f() … } Resolved by using prototype. Actually, this didn’t create a problem.

Data Types – Type Checking  Explicit datatype –int x  Implicit datatype –#define x 5

Implementation of Types  Hardware implementation –int –double –float  Software implementation –boolean –char –enum – can be integers to save space

More Complicated Types  Arrays –base(b)+i*esize –base(ar)+(i1*r2 +i2)*esize  Records –allocate memory sequentially –base+displacement

Type Checking Statements  S  id = E S.type = if id.type = E.type then void else error else error  S  if E then S 1 S.type=if E.type=boolean then S 1.type

Equivalence of type Expressions  Structural Equivalence –two expressions are either the same basic type, or are formed by applying the same constructor to structurally equivalent types. IE equivalent only if they are identical.  Name Equivalence –The following is structurally equivalent, not name typedef link = *cell typedef link = *cell link next; link next; cell * p; cell * p;

Name Equivalence typedef int t1; typedef int t2; t2 and t1 are not the same type. t2 and t1 are not the same type. int typeEqual(t1, t2) { if (t1 and t2 are simple types) return t1 == t2; return t1 == t2; if (t1 and t2 are type names) if (t1 and t2 are type names) return t1 == t2; return t1 == t2; else return 0;} in case you read the text else return 0;} in case you read the text