Semantic Analysis Chapter 6.

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
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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.
Compiler Construction
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Chapter 6 Type Checking Section 0 Overview
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
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.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
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.
Language Translation A programming language processor is any system that manipulates programs expressed in a PL A source program in some source language.
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.
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.
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.
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.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Implementing Subprograms
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design – CSE 504 Type Checking
Data Types In Text: Chapter 6.
Functions.
Names and Attributes Names are a key programming language feature
COMPILER CONSTRUCTION
Semantic Analysis Type Checking
CSE 3302 Programming Languages
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
C Basics.
Chap. 6 :: Control Flow Michael L. Scott.
Implementing Subprograms
CSE 3302 Programming Languages
CSE 3302 Programming Languages
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Chap. 6 :: Control Flow Michael L. Scott.
Semantic Analysis Semantic analysis includes
Semantic Analysis Chapter 6.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Names and Binding In Text: Chapter 5.
Compiler Construction
CSE 3302 Programming Languages
Compiler Construction
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
Presentation transcript:

Semantic Analysis Chapter 6

Two Flavors Static (done during compile time) 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 Implementation Attributes (properties) 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 digits

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, Xa is an attribute for this symbol X ABCD (grammar) X.x = A.a B.b C.c D.d (attribute grammar)

Attribute Grammar Example E1  E2 + T E1.type = E2.type + T.type

Attribute Grammar Example decl  type var-list 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 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 E1  E2 + T E1.tree = mkOpNode(+, E2.tree, T.tree) E  T E.tree = T.tree F  number 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; } { char * j; *j = i = 5;

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

Kinds of Declarations sequential collateral recursive C scheme ML 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; … } Is j 2 or 3?

Example - Recursive int gcd(int n, int m) { if (m == 0) return n; 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 S  if E then S1 S.type=if E.type=boolean then S1.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 link next; cell * p;

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