Semantic Processing. Semantic Errors. Semantics - Part 1. Semantics - Part 1

Similar documents
CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Semantic Processing (Part 2)

CSE 431S Type Checking. Washington University Spring 2013

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

The SPL Programming Language Reference Manual

Introduction to Programming Using Java (98-388)

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

The PCAT Programming Language Reference Manual

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Semantic Processing (Part 2)

CMSC 330: Organization of Programming Languages

Informatica 3 Syntax and Semantics

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

1 Lexical Considerations

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Concepts Introduced in Chapter 6

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

The role of semantic analysis in a compiler

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Syntax Errors; Static Semantics

Semantic analysis. Semantic analysis. What we ll cover. Type checking. Symbol tables. Symbol table entries

Lexical Considerations

Lexical Considerations

Concepts Introduced in Chapter 6

Semantic Analysis. Compiler Architecture

CSC 533: Organization of Programming Languages. Spring 2005

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

CSCE 314 Programming Languages. Type System

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Lecture 16: Static Semantics Overview 1

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Java+- Language Reference Manual

CSE 431S Final Review. Washington University Spring 2013

Types. What is a type?

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Today's Topics. CISC 458 Winter J.R. Cordy

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CS558 Programming Languages. Winter 2013 Lecture 3

Review of the C Programming Language

Intermediate Code Generation

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Short Notes of CS201

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

CS201 - Introduction to Programming Glossary By

Project 3 - Parsing. Misc Details. Parser.java Recursive Descent Parser... Using Lexer.java

Decaf Language Reference Manual

Motivation for typed languages

The Decaf Language. 1 Lexical considerations

MIDTERM EXAM (Solutions)

Programming Methodology

The Structure of a Syntax-Directed Compiler

Semantic actions for declarations and expressions

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Full file at

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Semantic actions for declarations and expressions. Monday, September 28, 15

CHIL CSS HTML Integrated Language

Names, Scope, and Bindings

CS S-06 Semantic Analysis 1

Semantic actions for declarations and expressions

Semantic Analysis. Lecture 9. February 7, 2018

Computer Components. Software{ User Programs. Operating System. Hardware

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

IC Language Specification

Review of the C Programming Language for Principles of Operating Systems

3. Java - Language Constructs I

Chapter 1. Fundamentals of Higher Order Programming

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Static Checking and Type Systems

Names, Scope, and Bindings

Expressions and Assignment

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

A Simple Syntax-Directed Translator

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Language Reference Manual simplicity

Lecture 2: C Programm

C++ Programming: From Problem Analysis to Program Design, Third Edition

CS558 Programming Languages

The Decaf language 1

Writing Evaluators MIF08. Laure Gonnord

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

(Not Quite) Minijava

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Ruby: Introduction, Basics

Type Checking. Error Checking

The Arithmetic Operators

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Transcription:

Semantic Processing The Lexer and Parser Found lexical and syntax errors Built Abstract Syntax Tree Now!Find semantic errors.!build information about the program. Later!Generate IR Code!Optimize IR Code!Generate Target Code 1 Semantic Errors Undefined ID / ID is already defined Other name-related checks (e.g., can t redefine true ) Field labels Labels on loops, gotos, etc. 2

Semantic Errors Undefined ID / ID is already defined Other name-related checks (e.g., can t redefine true ) Field labels Labels on loops, gotos, etc. Type checks For operators and expressions For assignment statements Wherever expressions are used (e.g., if condition must be ean) 3 Semantic Errors Undefined ID / ID is already defined Other name-related checks (e.g., can t redefine true ) Field labels Labels on loops, gotos, etc. Type checks For operators and expressions For assignment statements Wherever expressions are used (e.g., if condition must be ean) Flow of control Return statement ( expr must / must not be included) Break/continue statement must be within a loop or switch Unreachable code? Might want to detect it. 4

Semantic Errors Undefined ID / ID is already defined Other name-related checks (e.g., can t redefine true ) Field labels Labels on loops, gotos, etc. Type checks For operators and expressions For assignment statements Wherever expressions are used (e.g., if condition must be ean) Flow of control Return statement ( expr must / must not be included) Break/continue statement must be within a loop or switch Unreachable code? Might want to detect it. Procedure calls Wrong number of arguments Type of arguments Void / non-void conflict 5 Semantic Errors Undefined ID / ID is already defined Other name-related checks (e.g., can t redefine true ) Field labels Labels on loops, gotos, etc. Type checks For operators and expressions For assignment statements Wherever expressions are used (e.g., if condition must be ean) Flow of control Return statement ( expr must / must not be included) Break/continue statement must be within a loop or switch Unreachable code? Might want to detect it. Procedure calls Wrong number of arguments Type of arguments Void / non-void conflict OOP-related checks Does this class understand this message? Is this field in this class? Is private / public access followed? 6

Contain variables May be nested May contain variable declarations { var x,y: ; { var x: double; } } Blocks Declarations of Variables Apply to the statements in the block and statements in nested blocks unless hidden by other declarations PCAT Each body is a block Outermost (main) block (at level 0) Each procedure constitutes a new block Blocks in C++ and Java: void foo { double x; for ( x = 0; ) { } } 7 Scope (Also: Lexical scope of variables ) Where is the variable visible? The scope of the variable. Scope rules are given in the language definition. begin var x 1, y begin var x 2,z Scope of x 1 Scope of x 2 and z Scope of y 8

Variations Variable X s scope exts from the beginning of the block in which it was declared, through the of the block. Variable X s scope exts from the po of its declaration through the of the block. Unless hidden by a new declaration of a variable with the same name! 9 Variations Variable X s scope exts from the beginning of the block in which it was declared, through the of the block. Variable X s scope exts from the po of its declaration through the of the block. Unless hidden by a new declaration of a variable with the same name! PCAT Variables Visible (i.e., usable) only after their declaration. Types, Procedures Visible from the beginning of the block (to allow recursion). PASS 1: Enter ID s o symbol table PASS 2: Check all uses 10

var x,y,z type T1,T2 procedure foo1 (x,a) is var y,b type T2 procedure foo2 () is var c begin ID ; begin ID ; procedure foo3 () is Level = 2 Level = 1 Level = 1 Level = 0 var begin ID ; begin ID x foo1 a y foo2 ; Static Level Lexical Level (Textual) 11 Functions as Data var f,g: function; f = function (a,b: Int) : Int is var t: Int; t = a*b; return t-1; Function; g = f; i = g(7,5); This is like a constant. (It is an expression.) Within it is a new block. Lambda Expressions Closures Nameless Functions This idea is very powerful! Programs may have more complex behavior Programmers work at higher level of abstraction 12

begin A begin B begin C begin D begin E begin F begin G Blocks are Nested A B C D E F G C A sequential scan of the program will follow a depth-first traversal of this tree! B D E F A G Level 0 Level 1 Level 2 Level 3 Level 4 13 begin A begin B begin C begin D begin E begin F begin G Blocks are Nested A B C D E F G C A sequential scan of the program will follow a depth-first traversal of this tree! B D E F A G Level 0 Level 1 Level 2 Level 3 Level 4 The symbol table will work like a stack openscope = push closescope = pop A Time B A C B A B A D B A E D B A 14

Goals of Type Checking Make sure the programmer uses data correctly. x + y must have numeric types x = a; types must match (or be compatible ) if (expr) then type of expression must be ean a[i] a must have type array, i must have type eger r.f r must have type record. foo (a,b,c) args must have the right types p* p must be a poer 15 Goals of Type Checking Make sure the programmer uses data correctly. x + y must have numeric types x = a; types must match (or be compatible ) if (expr) then type of expression must be ean a[i] a must have type array, i must have type eger r.f r must have type record. foo (a,b,c) args must have the right types p* p must be a poer Need to select the appropiate target operators. x+y Need to determine egeradd or doubleadd 16

Goals of Type Checking Make sure the programmer uses data correctly. x + y must have numeric types x = a; types must match (or be compatible ) if (expr) then type of expression must be ean a[i] a must have type array, i must have type eger r.f r must have type record. foo (a,b,c) args must have the right types p* p must be a poer Need to select the appropiate target operators. x+y Need to determine egeradd or doubleadd Need to insert coercion routines, where necessary. PCAT: i/j! 2(i)/2(j) 17 Goals of Type Checking Make sure the programmer uses data correctly. x + y must have numeric types x = a; types must match (or be compatible ) if (expr) then type of expression must be ean a[i] a must have type array, i must have type eger r.f r must have type record. foo (a,b,c) args must have the right types p* p must be a poer Need to select the appropiate target operators. x+y Need to determine egeradd or doubleadd Need to insert coercion routines, where necessary. PCAT: i/j! 2(i)/2(j) Determine how much space to allocate for each variable. Integer! 32 bits Double! 64 bits Char! 8 bits Boolean! 1 bit 18

Types Each language has its own notions of type. Basic Types (also called primitive types ) eger,, character, ean Constructed Types Built from other types Notations in other languages: array of [100] a; record { } poer to *p; function () " (* foo) () {} We must represent types within out compiler. Might want a little language of type expressions. To make explicit the universe of all possible types. 19 Each has a name eger ean char void type_error Basic Types Close correspondence with keywords in the langauge Each basic type is a set of values. Each type will have several Predefined operators on the values Void A type with zero values Used for typing functions Type_Error Used to deal with semantic errors (not ly a type) 20

Array Types PCAT array of Pascal array [1..10] of C double x [10] Java double [] Portlandish Array [Integer,Real] Element Type (or Base Type ) Can be any type Can even be other array type array of array of a[i][j] = (a[i])[j] Index Type Usually eger but other possibilities Pascal: array [Days] of Often implicit, not ly a part of the type Is the size of the array part of the type??? 21 Poer Types PCAT-style var p: ptr to eger; Pascal var p: # eger; C * p; Java MyRec p; Element Type (or Base Type ) Can be any type. Typical Operations Comparison == Copy = Dereference *p Increment p++ Convert to/from eger p = ( *) 0x0045ff00; 22

Record Types ( Structs ) PCAT var r: record value: ; count: eger; ; C struct { double value; count; } r; Java class MyRec { double value; count; } MyRec r; Each record consists of several values of different types components, fields Each component value has different type The component values are identified by names ( field names ) r.value 23 Product Types (Tuple Types) Each tuple object consists of several component values. Each component value has a different type. (Similar to record types). Component values are identified by position, not name. To specify a product type: Notation #1: var t1: eger $ ean; t2: $ $ $ ; Notation #2: var t1: (eger, ean); t2: (,,, ); To specify a tuple: t1 = <6,true>; t1 = (6,true); t1 = [6,true]; To access the component values: i = t1.1; i = first(t1); x = t2.3; x = third(t2); 24

List Types Each list object consists of zero or more values, all with the same type. To specify a list type: Notation #1: var mylist: list of eger; Notation #2: var mylist: List[eger]; To get the first element of the list: i = head(mylist); To get a new list of everything else: otherlist = tail(mylist); Add an element to the front and create a new list: newlist = cons(i,mylist) To create a list: mylist = []; mylist = [3,5,7]; Other operations: length, app, isempty i = car(mylist); i = cdr(mylist); newlist = i.mylist; mylist = null; mylist = 3.5.7.null; 25 Function Types Some languages include function types. Need to associate types with function names. Functions are first-class objects (e.g., they can be stored in arrays, etc.). To specify a function type: Notation #1: DomainType " RangeType var f: eger " ean; g: $ $ $ " void; Notation #2: function (DomainTypes) returns RangeType var f: function (eger) returns ean; g: function (,,, ); Operations: Creation and Copy f = function (a:) returns ean return ; Function Application/Invocation g (1.5, 2.5, 3.5, 4.5); Comparison is usually not allowed. 26

Assumptions: $ is associative ( $ ) $ = $ ( $ ) = $ $ $ has greater precedence than " $ " = ( $ ) " " is right associative " " = " ( " ) Working with $ and " 27 Example type Complex = $ ; var c: Complex; c = <1.2, 3.4>); <x,y> = c; function ComplexMult: Complex $ Complex " Complex Complex $ Complex " Complex = (Complex $ Complex) " Complex = (( $ ) $ ( $ )) " ( $ ) = $ $ $ " $ <x,y> = ComplexMult (c, <5.6,7.8>); 28

Higher-Order Functions function AddOne: " ; AddOne = function (x:) returns return x + 1.0; Function; x = AddOne(123.0); x = AddOne(AddOne(AddOne(AddOne(AddOne(123.0)))); Imagine a function which takes 2 arguments:!a function, f!an eger, N It returns a function which when applied to argument x, will apply function f, N times. function Repeat: ( " ) $ " ( " ); g = Repeat(AddOne,5); // g will add 5 x = g(123.0); x = (Repeat(AddOne,5)) (123.0); Repeat is a Higher-Order Function. At least one argument or result is another function! 29 T A Syntax of Types " " " " char " void " TypeError " array of T " list of T " ptr to T " record ID : T {, ID : T } + Record " T $ T " T " T " ( T ) Represent each type with a tree An AST 30

Using Trees To Represent Types type T1 is (ptr to ) " (array of (eger " ean)); The representation of T1 " In our PCAT compiler array of array of record ; Array ptr array Array " Record eger ean 31 Naming Types Associate a name with a type. type MyRec is record ; name type Example: type Complex is $ ; function ComplexMult (x, y: Complex) returns Complex is ; Or perhaps var ComplexMult: Complex $ Complex " Complex; " $ Complex Complex Complex Complex $ Complex " Complex 32

Naming Types Associate a name with a type. type MyRec is record ; name type Example: type Complex is $ ; function ComplexMult (x, y: Complex) returns Complex is ; Or perhaps var ComplexMult: Complex $ Complex " Complex; " $ $ $ $ $ $ $ " $ 33 Static v. Dynamic Type Checking Static Type Checking Performed by the compiler Errors detected? Pr a descriptive message and keeping checking Patch up the AST Must cope with previous errors 34

Static v. Dynamic Type Checking Static Type Checking Performed by the compiler Errors detected? Pr a descriptive message and keeping checking Patch up the AST Must cope with previous errors Dynamic Type Checking Checking done at run-time Compiler does not know about types. var x, y, z; x = y + z; Each variable contains: A value Type information ( type tags ) Examples: Smalltalk / Squeak Lisp Integer or Floating Addition? At runtime, do y and z contain egers or s or? 35 Untyped Languages Example: Assembly Language!There may be different types of data (eger, float, poers, etc.)!the programmer says which operations to use (iadd, fadd, )!A type is not associated with each variable.!if the programmer makes mistakes, the results are wrong. 36

Untyped Languages Example: Assembly Language!There may be different types of data (eger, float, poers, etc.)!the programmer says which operations to use (iadd, fadd, )!A type is not associated with each variable.!if the programmer makes mistakes, the results are wrong. Strongly Typed Languages!Each value has an associated type.!guarantees that no type-errors can happen. Example: x = abc ; y = def ; z = x - y;!c/c++ Type errors can occur, especially with casting. It is the programmer s responsibility! Error! This operation cannot be done on this type of data. 37 Untyped Languages Example: Assembly Language!There may be different types of data (eger, float, poers, etc.)!the programmer says which operations to use (iadd, fadd, )!A type is not associated with each variable.!if the programmer makes mistakes, the results are wrong. Strongly Typed Languages!Each value has an associated type.!guarantees that no type-errors can happen. Example: x = abc ; y = def ; z = x - y;!c/c++ Type errors can occur, especially with casting. It is the programmer s responsibility! Strong, Static Type Checking!The compiler checks all types before runtime.!no type-errors can occur. Examples: Java, PCAT Error! This operation cannot be done on this type of data. 38

Basic Types: string type_of_nil Constructed Types: array record Other: typeerror Types In PCAT The type rules for nil are different myarr := nil; myrec := nil; Representation of a type: Poer to the AST for the type Type_Error We ll use null poer 39 Approach To Static Type Checking!Need to describe types A representation of types!associate a type with each variable. The variable declaration associates a type with a variable. This info is recorded (in the symbol table).!associate a type with each expression and each sub-expression.!work bottom-up The type is a synthesized attribute!check operators expr1 + expr2 Is the type of the expressions eger or?!check other places that expressions are used LValue := Expr ; Is the type of expr equal to the type of the L-Value? if (expr) Is the type of the expression ean? 40

Operator Overloading PCAT Example: var x,y: ; x+y PCAT has two kinds of addition The + operation is overloaded Multiple meanings: iadd fadd Also multiple kinds of negation, subtraction, multiplication, comparison, Select correct operation based on argument types. We ll use the term mode INTEGER_MODE REAL_MODE Tells which form of addition will be needed. 41 Type Conversions PCAT Example: var i:, x: ; (x + i) Must convert the eger value to a value first. Real addition (fadd) will be used. The result will be a. Implicit Type Conversions (also called Coercions )!The language definition tells when they are needed.!compiler must insert special code to perform the operation. Explicit Type Conversions (also called Casting ) (i + () x)!the programmer requests a specific conversion.!the language definition tells when they allowed.!the compiler may (or may not) need to insert special code. 42

Types In PCAT: Unary Operators Given: Type of operand Determine: Type of result string array record type error not + - Blank entries indicate type error 43 Types In PCAT: Unary Operators Given: Type of operand Determine: Type of result string array Implementation Ideas: record 7 $ 3 array type error ResultType[,not]! Sequence of IF tests if (op == PLUS) or (op == MINUS) then if typeofoperand == then resulttype = ; elseif typeofoperand == then resulttype = ; else resulttype = null; If elseif (op == NOT) then not // TypeError; + - Blank entries indicate type error 44

Types In PCAT: Binary Operators Operand 1 type error (any) (other) Operand 2 (any) type error (other) + - * * * / * * * and or = <> * * < <= > >= * * div mod ** ok** * means the argument(s) must be coerced to ** means ok if the arguments are the same type := ok ok* ok ok 45 Types In PCAT: Binary Operators Operand 1 type error (any) (other) Operand 2 (any) type error (other) + - * * * / * * * and or = <> * * < <= > >= * * div mod ** ok** * means the argument(s) must be coerced to ** means ok if the arguments are the same type := ok ok* ok ok Implementation Ideas: Use a 7 $ 7 $ 15 array? Nah Switch on operator first, then on operand type. 46

Recursive Types type MyRec is record info: eger; next: MyRec; ; var x: MyRec := MyRec { info := 789; next := null }; 47 Recursive Types type MyRec is record info: eger; next: MyRec; ; var x: MyRec := MyRec { info := 789; next := null }; All records and arrays will go o the Heap. info next 123 info next 456 info next 789 null The Heap 48

Recursive Types type MyRec is record info: eger; next: MyRec; ; var x: MyRec := MyRec { info := 789; next := null }; All records and arrays will go o the Heap. info next 123 info next 456 info next 789 The Heap 49 Recursive Types type MyRec is record info: eger; next: MyRec; ; var x: MyRec := MyRec { info := 789; next := null }; All records and arrays will go o the Heap. x 32 bits info next 123 info next 456 info next 789 The Heap Our Implementation: all variables will be 32 bits 50

Recursive Types type MyRec is record info: eger; next: MyRec; ; var x: MyRec := MyRec { info := 789; next := null }; All records and arrays will go o the Heap. x 32 bits info next 123 info next 456 info next 789 Runtime Stack of Activation Records ( Stack Frames ) The Heap 51 Type Equivalence What does it mean to say type of operand 1 = type of operand 2? type T1 is record f: ; g: ; ; T2 is record f: ; g: ; ; T3 is T2; var x: T1, y: T2, z: T3; x := y; Is the type of x the same as the type of y? Is the type of y the same as the type of z? 52

Types are represented as trees. " " ptr array ptr array " " eger ean eger ean 53 Types are represented as trees. Types may be named. type T1 is ; T1 T2 T3 " " ptr array ptr array " " eger ean eger ean 54

Structural Equivalence Are the trees equivalent? Isomorphic (same shape, same nodes) Must walk the trees to check. Name Equivalence Are they the same tree? Compare poers T1 T2 T3 " " ptr array ptr array " " eger ean eger ean 55 Testing Structural Equivalence function typeequiv (s, t) returns ean if (s and t are the same basic type) then return true elseif (s = array of s1 ) and (t = array of t1 ) then return typeequiv (s1,t1) elseif (s = s1 $ s2 ) and (t = t1 $ t2 ) then return typeequiv (s1,t1) and typeequiv (s2,t2) elseif (s = ptr to s1 ) and (t = ptr to t1 ) then return typeequiv (s1,t1) elseif (s = s1 " s2 ) and (t = t1 " t2 ) then return typeequiv (s1,t1) and typeequiv (s2,t2) else return false If Function 56