Static semantics Semantic checking which can be done at compile-time Type-compatibility –int can be assigned to double (type coercion) –double cannot be.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

Semantics Static semantics Dynamic semantics attribute grammars
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
ICE1341 Programming Languages Spring 2005 Lecture #5 Lecture #5 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
Compiler Construction
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Semantics “Semantics” has to do with the meaning of a program. We will consider two types of semantics: –Static semantics: semantics which can be enforced.
ISBN Chapter 3 Describing Syntax and Semantics.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Describing Syntax and Semantics
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Course: ICS313 Fundamentals of Programming Languages. Instructor: Abdul Wahid Wali Lecturer, College of Computer Science and Engineering.
March 5, ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341)
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
C HAPTER 3 Describing Syntax and Semantics. T OPICS Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 More data types Character and String –Non-numeric variables –Examples: char orange; String something; –orange and something are variable names –Note.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
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.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
Describing Syntax and Semantics
Chapter 7: Expressions and Assignment Statements
Semantic Analysis Type Checking
Describing Syntax and Semantics
Programming Languages
Chapter 7: Expressions and Assignment Statements
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
Describing Syntax and Semantics
Syntax Questions 6. Define a left recursive grammar rule.
Describing Syntax and Semantics
Explicit and Implicit Type Changes
Prabhaker Mateti ACK: Assembled from many sources
Relational Operators Operator Meaning < Less than > Greater than
Describing Syntax and Semantics
CSCI 3370: Principles of Programming Languages Syntax (cont.)
Describing Syntax and Semantics
Compiler Construction
Describing Syntax and Semantics
Describing Syntax and Semantics
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 (b) Semantics.
- Who must use language definitions?
Describing Syntax and Semantics
Compiler Construction
Presentation transcript:

Static semantics Semantic checking which can be done at compile-time Type-compatibility –int can be assigned to double (type coercion) –double cannot be assigned to int without explicit type cast Can be captured in grammar, but only at expense of larger, more complex grammar

Type rules in grammar Must introduce new non-terminals which encode types: Instead of a generic grammar rule for assignment: –Stmt  Var “=” Expr “;” we need multiple rules: –Stmt  doubleVar “=” intExpr | doubleExpr “;” –Stmt  intVar “=” intExpr “;” Of course, such rules need to handle all the relevant type possibilities (e.g. byte, char, short, int, long, float and double).

Attribute grammars Attribute grammars provide a neater way of encoding such information.

Attributes We can associate with each symbol X of the grammar a set of attributes A(X). Attributes are partitioned into: –synthesized attributes S(X) – pass info up tree –inherited attributes I(X) – pass info down tree

Semantic functions We can associate with each rule R of the grammar a set of semantic functions. For rule X0  X1 X2 … Xn –S(X0) = f(A(X1), A(X2), …, A(Xn)) –for 1<=j<=n, I(Xj) = f(A(X0),…,A(Xj-1))

Predicates Boolean expression involving the attributes and a set of attribute values If true, node is ok If false, node violates semantic rule

Example  =.expType .actType  [2] + [3].actType  if (var[2].actType = int) and (var[3].actType = int) then int else real.actType ==.expType .actType .actType.actType ==.expType  A | B | C.actType  lookUp(.string) Syntax rule Semantic rule Semantic predicate

A=A+B [2] [3] Suppose: A is int B is int

A=A+B [2] [3] Suppose: A is int B is int actual type = int expected type = int actual type = int actual type = int actual type = int

A=A+B [2] [3] Suppose: A is real B is int actual type = real expected type = real actual type = real actual type = int actual type = real

A=A+B [2] [3] Suppose: A is int B is real actual type = real expected type = real actual type = real actual type = int actual type = real

A=A+B [2] [3] Suppose: A is int B is real actual type = int expected type = int actual type = int actual type = real actual type = real Houston, we have a problem!