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

Similar documents
Type Checking. Error Checking

Type systems. Static typing

Formal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table

Intermediate Code Generation Part II

Static Checking and Type Systems

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

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

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

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

Introduction to Programming Using Java (98-388)

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6

CA Compiler Construction

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

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

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

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

Type Checking. Chapter 6, Section 6.3, 6.5

The compilation process is driven by the syntactic structure of the program as discovered by the parser

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

Semantic Analysis and Type Checking

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Syntax-Directed Translation Part I

1 Lexical Considerations

There is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar

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

Programming Languages Third Edition. Chapter 7 Basic Semantics

5. Semantic Analysis!

LECTURE 3. Compiler Phases

The role of semantic analysis in a compiler

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 264

Intermediate Code Generation

Lexical Considerations

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

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

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

Semantic Analysis. Compiler Architecture

More On Syntax Directed Translation

COP5621 Exam 3 - Spring 2005

Principles of Programming Languages

UNIT IV INTERMEDIATE CODE GENERATION

Lexical Considerations

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

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

Evaluation of Semantic Actions in Predictive Non- Recursive Parsing

Type Checking. CS308 Compiler Theory 1

A simple syntax-directed

Types and Type Inference

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Types and Type Inference

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Syntax Errors; Static Semantics

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

Syntax-Directed Translation. Introduction

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Intermediate Representations

2. Reachability in garbage collection is just an approximation of garbage.

CS4215 Programming Language Implementation

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

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

CS558 Programming Languages

Crafting a Compiler with C (II) Compiler V. S. Interpreter

CS558 Programming Languages

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

CSE 307: Principles of Programming Languages

CS558 Programming Languages

CS 4201 Compilers 2014/2015 Handout: Lab 1

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

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

Types and Static Type Checking (Introducing Micro-Haskell)

CSC 467 Lecture 13-14: Semantic Analysis

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

5. Syntax-Directed Definitions & Type Analysis

Semantic Analysis Wilhelm/Seidl/Hack: Compiler Design Syntactic and Semantic Analysis, Chapter 4

Compilers CS S-05 Semantic Analysis

Computational Expression

Acknowledgement. CS Compiler Design. Semantic Processing. Alternatives for semantic processing. Intro to Semantic Analysis. V.

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Project Compiler. CS031 TA Help Session November 28, 2011

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Type Analysis. Type Checking vs. Type Inference

4. An interpreter is a program that

Chapter 6 Intermediate Code Generation

Intermediate Code Generation

Functional Programming. Pure Functional Programming

CS 6353 Compiler Construction Project Assignments

CSCE 314 Programming Languages. Type System

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Programming Languages, Summary CSC419; Odelia Schwartz

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

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

Time : 1 Hour Max Marks : 30

Formal Semantics of Programming Languages

Semantic Analysis. Lecture 9. February 7, 2018

Transcription:

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es

Introduction He am a driver might be syntactically correct but semantically wrong. Semantic analysis in the compiler s last chance to detect incorrect programs. tokens Parse tree Annotated parse tree 2

Static analysis For a program to be semantically valid: All variables, functions and classes properly defined. Expressions and variables must be used in ways that respect the type system. Access control must be respected. 3

Semantic Analysis Semantic information is associated to the programming language constructs by attaching attributes to the grammar. Values for attributes are computed by semantic rules associated with grammar constructors. This phase modifies the symbol table and is often mixed with the intermediate code generation. The method applied for the determination of syntactic and semantic correctness of a given program is called static checking. 4

Semantic Analysis Notations for associating semantic rules with productions: Syntax-directed definitions. Translation schemes. 5

Static analysis Examples Type checking errors messages are issued if an operator is applied to an incompatible operand. Flow-of-control checks for example, in C a break statement occurs inside a while, or for statements. Uniqueness checks sometimes, an object must be defined exactly one, for example, labels in a case statement must be distinct. Name-related checks sometimes, in a construct the same name must appear two or more times. 6

Type Checking Objective: Verify that each operation executed in a program respect the type system of the program. Static type checking: Done at compile-time. Information obtained from a master symbol table. Dynamic type checking: Type information is included for each data location at runtime. 7

Designing a Type Checker Identify the types in the input language. Identify the language constructs that have types associated to them. Identify the semantic rules for the language. 8

Example of a simple language Simple language has a static type system Goal: check the types of all the expressions in the language Types in the language: Basic types char integer type_error Complex types Array[n] of T (T, 1..n) ^T pointer 9

A simple language Grammar P D ; E D D ; D id : T T char integer array [num] of T ^T E literal num id E mod E E [ E ] E^ D for declaration T for type E for expression 10

A simple language Semantic actions: P D ; E D D ; D D id : T { addtype (id.entry, T.type) } T char { T.type := char } T integer { T.type := integer } T ^T 1 { T 0.type := pointer (T 1.type) } T array [num] of T { T 0.type := array(1..num.val, T 1.type) } 11

A simple language Type checking of expressions Constants E literal { E.type := char } E num { E.type := integer } Identifiers E id { E.type := lookup (id.entry) } Operators E E mod E { if (E 1.type = integer) and (E 2.type = integer) E 0.type = integer else E 0.type = type_error } 12

A simple language Arrays E E [ E ] { if (E 1.type = integer) and (E 2.type = array(s, t)) then E 0.type = t else E 0.type = type_error } Pointers E E ^ { if (E 1.type = pointer(t)) E 0.type = t else E 0.type = type_error } 13

Type checking of statements By default, a sentence is of void type unless it is an error if and while S id := E { if (id.type = E.type) then S 0.type = void else S 0.type := type_error } S if E then S { if (E.type = boolean) then S.type = S 1.type else S 0.type := type_error } S while E do S { if (E.type = boolean) then S.type = S 1.type else S 0.type := type_error } S S ; S { if (S 1.type = void) and (S 2.type = void) then S 0.type = void else S 0.type := type_error } 14

Type checking of functions T T T { T 0.type = T 1.type T 2.type } Ej.: int f(double x, char y) {... } type de f: double x char int Rule for checking the type of a function application E E ( E ) { if (E 2.type = s) and (E 1.type = s t) then E 0.type = t else E 0.type := type_error } 15

Type Conversions Many compilers correct the simplest of type errors x + y? What if x is of type real and y is of type integer? E E op E { if (E 1.type = integer) and (E 2.type = integer) then E 0.type = integer else if (E 1.type = integer) and (E 2.type = real) then E 0.type = real else if (E 1.type = real) and (E 2.type = integer) then E 0.type = real else if (E 1.type = real) and (E 2.type = real) then E 0.type = real else E 0.type = type_error} 16

Overloading of functions and operators An overloaded symbol has different meanings depending on its context (4 + a ) and (4+ a ) in Java In Ada function * (k,j: integer) return complex function * (x,y: complex) return complex Therefore 3*5 either has type integer or complex, depending on its context 2*(3*5) integer z*(3*5) complex 17

Possible types of expressions E E { E.types=E.types} E id { E.types=lookup(id.entry)} E E ( E ) {E 0.types= {t there exists an s in E 2.types such that s t is in E 1.types} 18

Cycles in Representations of types Often data structures are defined recursively. Example.: lists Type link = ^cell; cell = record info : integer; next: link end; 19

Cycles in Representations of types Acyclic representation cell = record x x x info integer next pointer cell Cyclic representation cell = record x 20 info x x integer next pointer

Scope Checking The visibility of an identifier is constrained to some section of the program: Global and local variables. Objective: Determine if the identifier is accessible at a specific point in the program. Approaches: Use an individual symbol table for each scope (stack containing all the scopes). A single global table for all the scopes (scope-number, var-name). 21