Semantic Analysis Chapter 6.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
Variables Names Bindings Type Scope. L-Value versus R-Value Not complicated Associated with assignment statements Left hand side represents an address.
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.
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)
Chapter 8 - Control II: Procedures and Environments
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.
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.
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.
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.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
Names, Scope, and Bindings Programming Languages and Paradigms.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Windows Programming Lecture 03. Pointers and Arrays.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Chapter 5 Names, Bindings, Type Checking CSCE 343.
Implementing Subprograms
Lecture 9 Symbol Table and Attributed Grammars
Data Types In Text: Chapter 6.
Type Checking and Type Inference
Functions.
Names and Attributes Names are a key programming language feature
COMPILER CONSTRUCTION
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.
Type Checking, and Scopes
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 16: Introduction to Data Types
CSE 3302 Programming Languages
Semantic Analysis Chapter 6.
Chap. 6 :: Control Flow Michael L. Scott.
Implementing Subprograms
Final Review In Text: Chapters 1-3, 5-11,
CSE 3302 Programming Languages
Midterm Review In Text: Chapters 1-3, 5-7, 15, 16.
CSE 3302 Programming Languages
Final Review In Text: Chapters 1-3, 5-10, 12,
Final Review In Text: Chapters 1-3, 5-10, 12,
Midterm Review In Text: Chapters 1-3, 5-9, 15.
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Midterm Review In Text: Chapters 1-3, 5-10, 15.
Chap. 6 :: Control Flow Michael L. Scott.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Names and Binding In Text: Chapter 5.
Compiler Construction
CSE 3302 Programming Languages
C Data Types and Variable
Midterm Review In Text: Chapters 1-3, 5-9, 15.
Midterm Review In Text: Chapters 1-3, 5-11, 15.
Compiler Construction
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Implementing Subprograms
CSE 3302 Programming Languages
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 (compile time) Value of expressions (run time) Location of variables (compile time) Object code of procedure (compile time) Number of Significant digits (design time)

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 E1  E2 + T E1.tree = mkOpNode(+, E2.tree, T.tree) E  T E.tree = T.tree F  number F.tree = mkNumNode(number.lexval)

Symbol Tables Keep track of identifiers Must deal with scope efficiently

Static vs Dynamic Scope compile time or run time 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 – each declaration is available starting with the next line C Collateral – each declaration is evaluated in the environment preceding the declaration group. Declared identifiers are available only after all finishes. scheme ML Recursive - requires the function name to be added to the symbol table before processing the body of the function. C functions and type declarations are recursive.

Example - Sequential/Colateral order is not important with in group 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. Some languages have issue with using g before g is defined. (pascal)

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

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. I.E. equivalent only if they are identical. Example typedef link = *cell link next; cell * p; Name Equivalence two expressions use the same name

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

Name Equivalence typedef int t1; typedef int t2; t2 x; t2 y; t1 z; x and y are the same type. z is not the same type.