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.

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.
Chapter 2-2 A Simple One-Pass Compiler
CPSC 388 – Compiler Design and Construction
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Intermediate Code Generation
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.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
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.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Lecture 2 Introduction to C Programming
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
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.
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
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.
Imperative Programming
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
CPS120: Introduction to Computer Science Decision Making in Programs.
Prof. Fateman CS 164 Lecture 111 Syntax  Simple Semantics Lecture 11.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CPSC 388 – Compiler Design and Construction Parsers – Syntax Directed Translation.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
CMSC 330: Organization of Programming Languages Operational Semantics.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
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.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Lecture 9 Symbol Table and Attributed Grammars
Constructing Precedence Table
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
Corky Cartwright January 18, 2017
Semantic Analysis Chapter 6.
Mini Language Interpreter Programming Languages (CS 550)
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Semantic Analysis Chapter 6.
SYNTAX DIRECTED DEFINITION
Compiler Construction
Compiler Construction
Presentation transcript:

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 f()void  int void f(int)int  void bool aboolean char __int8 short __int16 int and long __int32

Declarations Simple grammar for declarations

SDD for Assigning Basic and Array Types addType is used to assign type to identifiers added by the Lexer

Assigning Basic Types int T.t = integer.b = integer B C int  C.t = integer.t = integer

Assigning Array Types int [2][3] T.t = integer.b = integer B C [ num ] C int C [ num ] .b = integer C.t = array( 2, ????????????? ) C.t = array( 3, ?????? ) C.t = integer.t = array( 2, array( 3, integer) ) integer array( 3, integer) ?

Checking for Unique Identifier Names Check if the id has already assigned a type in the same scope i.e. it’s already declared  error int x; int x; D  T id ; D{ t = lookup ( id.lexeme); if t = unbound then addType ( id.entry, T.t) else error( ‘identifier already defined’) } semantic action

Semantic Actions Syntax-directed translation scheme embeds program fragments called semantic actions within production bodies, as in E -+ El +T { print ' + I } By convention, semantic actions are enclosed within curly braces. (If curly braces occur as grammar symbols, we enclose them within single quotes, as in ‘{‘ and ‘ } '. The position of a semantic action in a production body determines the order in which the action is executed. In the previous production, the action occurs at the end, after all the grammar symbols; in general, semantic actions may occur at any position in a production body.

Small Example Language First-order functional language with recursive definition

Small Example Language (cont) Examples bool equals( int a, int b) = a = b int add( int a, int b) = a + b int equals (int a, int b) = if a = b then 0 else 1

The symbol table is implemented as a list with the following operations: empty: An empty symbol table is an empty list. binding: A new binding (name/object pair) is added to the front of the list. lookup: The list is searched until a matching name is found. The object paired with the name is then returned. If the end of the list is reached, an indication that this happened is returned instead. This indication can be made by raising an exception or by letting the lookup function return a type that can hold both objects and error indications, i.e., a sum-type. enter: The old list is remembered, i.e., a reference is made to it. exit: The old list is recalled, i.e., the above reference is used. Small Example Language (cont)

For terminals (variable names and numeric constants) with attributes, we assume that there are predefined functions for extracting these. Hence, id has an associated function name, that extracts the name of the identifier. Similarly, num has a function value, that returns the value of the number. For each nonterminal, we define one or more functions that take an abstract syntax subtree and inherited attributes as arguments and return the synthesised attributes. Small Example Language (cont)

Type Checking Expressions S ::= id = Exp int x; x = y;  error: undefined identifier A new look for SDD

Type Checking Expressions (cont)

Grammar rule: Exps  Exp, Exps | Exp Example: int f1(bool y) = let x = 3 in y+ f2( x, y) Type of f2 is: (int, bool)  int Exps Exp, Exps id x id y.type = bool.type = [bool].type = int.type = [int, bool] Exp ] [

Expression Type Checking (cont) ProductionSemantic Rule When to assign Exp.hasAddress = true? In case of Exp  id Also, Exp  Exp [Exp] And so on … according to the language semantics Exp  Exp 1 [Exp 2 ] Exp  * Exp 1 Exp  & Exp 1 e.g. &X &(X+Y) &3 if Exp 1.type = array(x, t) and if Exp 1.type = pointer(t) e.g. X[ Y[2] ] X[2][3] Exp.type = pointer(Exp 1.type) then Exp.type = t else error() Exp 2.type = integer then Exp.type = t else error()   if Exp 1.hasAddress = true then else error() but what if the id is constant? since an array element has an address

Type Checking of Statements ProductionSemantic Rule P  Decl Stmts if Decl.type = void then P.type = Stmts.type else error() Stmts  Stmt; Stmts 1 if Stmt.type = void then Stmts.type = Stmts 1.type Stmts  Stmt; Stmts.type = Stmt.type Stmt  id = Exp if id.type = Exp.type then Stmt.type = void else error() What if id.type is array? Stmt  if (Exp) Stmt 1 Stmt  while (Exp) Stmt 1 if Exp.type = bool then Stmt.type = Stmt 1.type else error() Stmt  ‘{‘ Stmts ‘}’ Stmt.type = Stmts.type

Function Declaration Grammar Function int add( int a, int b) = a + b int add( int a, int b) = a + b+c (Error) int add( int a, int b) = a = b (Error) int add( int a, int b, int a) = a + b (Error)

Function Declaration Checking Check Fun returns no information, it just checks for internal errors.

Function Declaration Checking (cont) e.g. int f(int x, bool y) TypeIds => int x, bool y Their should be a symbol table contains x and y with their types TypeIds TypeId, TypeIds.type = (y, bool).vtable = ((y, bool)).type = (x, int).vtable = ((y, bool), (x, int)) TypeId Int id x bool id y lookup( vtable, x) = unbound