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

Similar documents
Static Checking and Type Systems

Type Checking. Error Checking

Intermediate Code Generation Part II

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

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

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 systems. Static typing

Type Checking. Chapter 6, Section 6.3, 6.5

Principles of Programming Languages

CSE302: Compiler Design

COP5621 Exam 3 - Spring 2005

Intermediate Code Generation

Concepts Introduced in Chapter 6

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Concepts Introduced in Chapter 6

Intermediate Code Generation

intermediate-code Generation

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

5. Syntax-Directed Definitions & Type Analysis

Type Checking. CS308 Compiler Theory 1

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

Lexical and Syntax Analysis

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

CSE 431S Type Checking. Washington University Spring 2013

Compiler construction 2002 week 5

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

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

Intermediate Code Generation

More On Syntax Directed Translation

Types. What is a type?

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Formal Languages and Compilers Lecture X Intermediate Code Generation

Compiler construction in4020 lecture 5

Type Analysis. Type Checking vs. Type Inference

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

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

22c:111 Programming Language Concepts. Fall Types I

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Chapter 6 Intermediate Code Generation

Semantic Analysis and Type Checking

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

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

Compiler construction 2005 lecture 5

PROGRAMMING FUNDAMENTALS

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

Compilers. Compiler Construction Tutorial The Front-end

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018

Computational Expression

Java Primer 1: Types, Classes and Operators

Lecture 12: Data Types (and Some Leftover ML)

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

Intermediate Representations

Yacc. Generator of LALR(1) parsers. YACC = Yet Another Compiler Compiler symptom of two facts: Compiler. Compiler. Parser

Compilers CS S-05 Semantic Analysis

Introduction to Programming Using Java (98-388)

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

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

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

5. Semantic Analysis!

Using an LALR(1) Parser Generator

Fundamentals of Programming Languages

Lecture 02 C FUNDAMENTALS

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

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

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Type Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Lexical Considerations

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Intermediate Code Generation

COMP520 - GoLite Type Checking Specification

Chapter-8 DATA TYPES. Introduction. Variable:

CSCI312 Principles of Programming Languages!

HANDLING NONLOCAL REFERENCES

Java Notes. 10th ICSE. Saravanan Ganesh

6.096 Introduction to C++ January (IAP) 2009

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

CS558 Programming Languages

These are reserved words of the C language. For example int, float, if, else, for, while etc.

CS558 Programming Languages

3. Java - Language Constructs I

COMP520 - GoLite Type Checking Specification

1.3b Type Conversion

Chapter 2: Using Data

Expressions & Assignment Statements

CS S-06 Semantic Analysis 1

Lexical Considerations

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Symbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes

Pierce Ch. 3, 8, 11, 15. Type Systems

COMP520 - GoLite Type Checking Specification

Simple Lexical Analyzer

1 Lexical Considerations

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

Transcription:

Compilerconstructie najaar 2012 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs.nl college 5, dinsdag 9 oktober 2012 Static Type Checking 1

6.1 Variants of Syntax Trees Directed Acyclic Graphs for Expressions a+a (b c)+(b c) d Syntax tree vs DAG 2

Producing Syntax Trees or DAG s Production Semantic Rules 1) E E 1 +T E.node = new Node( +,E 1.node,T.node) 2) E E 1 T E.node = new Node(,E 1.node,T.node) 3) E T E.node = T.node 4) T ( E ) T.node = E.node 5) T id T.node = new Leaf(id,id.entry) 6) T num T.node = new Leaf(num,num.val) 3

Value-Number Method 1 id 2 num 10 3 + 1 2 4 = 1 3 5 to entry for i 4

Static Checking Type checks: Verify that type of a construct matches the expected one Flow-of-control checks: Example: break-statement must be enclosed in while-, foror switch-statement... 5

6.3 Types and Declarations Types can be used for Type checking Translation Type information useful to determine storage needed to calculate address of array element to insert explicit type conversions to choose right version of operator... 6

Type Expressions Types have structure Example: array type int[2][3] Basic types: boolean, char, integer, float, void Type names: typedefs in C, class names in C++ Type constructors: array record: data structure with named fields for function types: s t Cartesian product : s t... 7

6.5 Type Checking Type system contains information about Syntactic constructs of language Notion of types Logical rules to assign types to language constructs if both operands of + are integers, then result is integer if f has type s t and x has type s, then expression f(s) has type t Sound type system Strongly typed implementation of language 8

A Simple Type Checker A language example (Pascal-like) P D;S D D;D id : T T boolean char integer array [num ] of T ˆT S id := E if E then S while E do S S;S E true false literal num id E and E E mod E E[E] Eˆ 9

A Simple Type Checker Translation scheme for saving type of identifier P D;S D D;D D id : T {addtype(id.entry,t.type); T boolean {T.type = boolean; T char {T.type = char; T integer {T.type = integer; T ˆT 1 {T.type = pointer(t 1.type); T array [num ] of T 1 {T.type = array(1...num.val,t 1.type); 10

A Simple Type Checker Type Checking of Expressions E true {E.type = boolean; E false {E.type = boolean; E literal {E.type = char; E num {E.type = integer; E id {E.type = lookup(id.entry); E E 1 and E 2 {if (E 1.type == boolean) and (E 2.type == boolean) then E.type = boolean; else E.type = type error; E E 1 mod E 2 {if (E 1.type == integer) and (E 2.type == integer) then E.type = integer; else E.type = type error; E E[E] {if (E 2.type == integer) and (E 1.type == array(s,t)) then E.type = t; else E.type = type error; E E 1ˆ {if (E 1.type == pointer(t)) then E.type = t; else E.type = type error; 11

A Simple Type Checker Type Checking of Statements S id := E {if (id.type == E.type) then S.type = void; else S.type = type error; S if E then S 1 {if (E.type == boolean) then S.type = S 1.type; else S.type = type error; S while E do S 1 {if (E.type == boolean) then S.type = S 1.type; else S.type = type error; S S 1 ;S 2 {if (S 1.type == void) and (S 2.type == void) then S.type = void; else S.type = type error; 12

Type Equivalence S id := E {if (id.type == E.type) then S.type = void; else S.type = type error; When are type expressions equivalent? Structural equivalence Name equivalence Use graph representation of type expressions to check equivalence Leaves for basic types and type names Interior nodes for type constructors Cycles in case of recursively defined types... 13

Structural Equivalence Same basic type: integer is equivalent to integer Formed by applying same constructor to structurally equivalent types pointer(integer) is equivalent to pointer(integer) One is type name of other type link = ^cell; var next : link; last : link; p : ^cell; q, r : ^cell; 14

Type Conversions y = x+i with x float and i integer widening conversion narrowing conversion explicit conversion ( = cast) implicit conversion ( = coercion), automatically by compiler 15

Conversions in Java short byte double float long int char double float long int char short byte Widening conversions Narrowing conversions 16

Coercion (Example) Semantic action for E E 1 +E 2 uses two functions: max(t 1,t 2 ) widen(a, t, w) Addr widen(addr a, Type t, Type w) { if (t==w) return a; else if (t == integer and w == float) { temp = new Temp(); gen(temp = (float) a); return temp; else error; 17

Coercion (Example) E E 1 +E 2 { E.type = max(e 1.type,E 2.type); a 1 = widen(e 1.addr,E 1.type,E.type); a 2 = widen(e 2.addr,E 2.type,E.type); E.addr = new Temp(); gen(e.addr = a 1 + a 2 ); 18

Constructing Type Graphs in Yacc enum Types {Tint, Tfloat, Tpointer, Tarray,...; typedef struct Type { Types type; struct Type *child Type; Type *mkint() construct int node if not already constructed Type *mkfloat() construct float node if not already constructed Type *mkarray(type*, int) construct array-of-type node if not already constructed Type *mkptr(type*)construct pointer-of-type node if not already constructed 19

Yacc Specification (Example) from college 4 expr : expr + term { $$ = $1 + $3; term ; term : term + factor { $$ = $1 * $3; factor ; factor : ( expr ) { $$ = $2; DIGIT ; %% /* auxiliary functions section */ yylex() { int c; c = getchar(); if (isdigit(c)) { yylval = c- 0 ; return DIGIT; return c; 20

Constructing Type Graphs in Yacc Syntax-directed definitions %union { Symbol *sym; int num; Type *typ; %token INT %token <sym> ID %token <num> NUM %type <typ> type %% decl : type ID { addtype($2, $1); type ID [ NUM ] { addtype($2, mkarr($1,$4)); ; type : INT { $$ = mkint(); type ^ { $$ = mkptr($1); /* empty */ { $$ = mkint(); ; 21

Type Checking in Yacc Syntax-directed definitions %{ enum Types {Tint, Tfloat, Tpointer, Tarray,...; typedef struct Type { Types type; struct Type *child Type; % %union { Type *typ; %type <typ> expr %% expr : expr + expr { if ($1->type!= Tint $3->type!= Tint ) semerror("non-int operands in +"); else { $$ = mkint(); gen(int-add instruction for $1 and $3); 22

Type Coercion in Yacc Syntax-directed definitions %{... % %% expr : expr + expr { if ($1->type == Tint && $3->type == Tint) { $$ = mkint(); gen(int-add instruction for $1 and $3); else if ($1->type == Tfloat && $3->type == Tfloat) { $$ = mkfloat(); gen(float-add instruction for $1 and $3); else if ($1->type == Tfloat && $3->type == Tint) { $$ = mkfloat(); gen(int2float instruction for $3); gen(float-add instruction for $1 and $3); else if ($1->type == Tint && $3->type == Tfloat) { $$ = mkfloat(); gen(int2float instruction for $1); gen(float-add instruction for $1 and $3); else { semerror ("type error in +"); $$ = mkint(); 23

L-Values and R-Values E 1 = E 2 ; What can E 1 and E 2 be? i = i+1; i = 5; L-value: left side of assignment, location Example: identifier i, array acces a[2] R-value: right side of assignment, value Example: identifier i, array acces a[2], constant 5, addition i+1 24

L-Values and R-Values in Yacc Syntax-directed definitions (C) %{ typedef struct Node { Type *typ; int islval; Node; % %union { Node *rec; %type <rec> expr %% expr : expr + expr: { if ($1->typ->type!= Tint $3->typ->type!= Tint ) semerror ("non-int operands in +"); $$->typ = mkint(); $$->islval = FALSE; gen(...); expr = expr { if (!$1->islval $1->typ!= $3->typ ) semerror ("invalid assignment"); $$->typ = $1->typ; $$->islval = FALSE; gen(...); ID { $$->typ = lookup($1); $$->islval = TRUE; gen(...); 25

Volgende week Practicum over opdracht 2 Direct naar 302/304 Komt spoedig online Inleveren 29 oktober 26

Compiler constructie college 5 Static Type Checking Chapters for reading: 6.1, 6.3.1, 6.3.2, 6.5.1, 6.5.2 27