semantic analysis - lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.code generation 5.optimization...

59
Semantic Analysis Lecture 9 February 7, 2018

Upload: others

Post on 27-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Semantic AnalysisLecture 9

February 7, 2018

Page 2: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Midterm 1

Compiler Stages 12 / 14COOL Programming 10 / 12Regular Languages 26 / 30Context-free Languages 17 / 21Parsing 20 / 23Extra Credit 4 / 6

Average 92

Compiler Construction 2/48

Page 3: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Formal Languages Wrap-up

É You made it!

É Takeaways from lexicalanalysis and parsingÉ Regular languages define

tokensÉ Context-free languages

define parse treesÉ Classes of languages

require different levels ofexpressive power

Compiler Construction 3/48

Page 4: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Compilers Reminder

1. Lexical AnalysisÉ Source code→ Tokens

2. Syntactic AnalysisÉ Tokens→ Abstract Syntax Tree

3. Semantic Analysis (later today)

4. Code Generation

5. Optimization

Compiler Construction 4/48

Page 5: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Compilers Reminder

É Remember, we want to support developers’effort to write softwareÉ They give our compiler source codeÉ Our job is to turn it into a program

É Automatic conversion requires various levels ofanalysisÉ On the front end, we want to extract

structure from the string of source code

Compiler Construction 5/48

Page 6: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parsing to Extract Structure

É Consider two input strings:É x = 1;É y = 1;

É And what about:É z = 42;É y = 100;

Is there a structural difference between thesestrings?

‘=’

‘x’ ‘1’ ‘;’

Compiler Construction 6/48

Page 7: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parsing to Extract Structure

É Consider two input strings:É x = 1;É y = 1;

É And what about:É z = 42;É y = 100;

Is there a structural difference between thesestrings?

‘=’

‘y’ ‘1’ ‘;’

Compiler Construction 6/48

Page 8: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parsing to Extract Structure

É Consider two input strings:É x = 1;É y = 1;

É And what about:É z = 42;É y = 100;

Is there a structural difference between thesestrings?

‘=’

‘z’ ‘42’ ‘;’

Compiler Construction 6/48

Page 9: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parsing to Extract Structure

É Consider two input strings:É x = 1;É y = 1;

É And what about:É z = 42;É y = 100;

Is there a structural difference between thesestrings?

‘=’

‘y’ ‘100’ ‘;’

Assign→ x = 1; | y = 1;| z = 42; | y = 100;| ...

Compiler Construction 6/48

Page 10: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parsing to Extract Structure

É Consider two input strings:É x = 1;É y = 1;

É And what about:É z = 42;É y = 100;

Is there a structural difference between thesestrings?

‘=’

‘y’ ‘100’ ‘;’

Assign→ x = 1; | y = 1;| z = 42; | y = 100;| ...

Compiler Construction 6/48

Page 11: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parsing to Extract Structure

É Consider two input strings:É x = 1;É y = 1;

É And what about:É z = 42;É y = 100;

Is there a structural difference between thesestrings?

Assign→ ID = VALUE ;

‘=’

ID VALUE ‘;’

Compiler Construction 6/48

Page 12: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parsing to Extract Structure

É We want a concise grammarÉ Don’t want to depend on specific values!É S→ ID = VAL ; , not S→ x = 42 ;

É Well, we need an earlier lexical analysis stepÉ Bin lexemes into tokensÉ x, y, z all the same thing: identifiersÉ 1, 42, 100 all the same thing: integer constants

Compiler Construction 7/48

Page 13: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Wrapping Up Formal Languages

É Lexical AnalysisÉ Regular Language = DFA =NFA = Regular

Expression

É Syntactic Analysis / ParsingÉ Context-free Language = Context-free Grammar=Nondeterministic Pushdown Automaton =“Parsing Automaton”

Compiler Construction 8/48

Page 14: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Formal Languages Highlights

É Regular Languages are “easier” to recognizethan CFLs!É Need a stack with CFLs

É As compiler designers, we restrict our choicesof CFLÉ LL and LR are less expressive, but easier to parse

É “Parsing DFA” is kind of a lie! Technically,CFLs are recognized with NPDAsÉ This is not a formal languages course

Compiler Construction 9/48

Page 15: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parse Tree vs. Abstract Syntax TreeAlmost the sameInput: 6 + ( ( 5 ) )

Parse Tree:

EXP

+

EXP

int

6

EXP

EXP

( )

EXP

( )

int

5Compiler Construction 10/48

Page 16: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parse Tree vs. Abstract Syntax Tree

Almost the sameInput: 6 + ( ( 5 ) )

Abstract Syntax Tree:

ADD_EXP

int:6 int:5

Compiler Construction 10/48

Page 17: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Parsing HighlightsÉ You should understand that LL and LR are

different restricted subsets of CFLsÉ Trade language expressiveness for parsing ease

É You should know top-down and bottom-upparsingÉ Real parser generators build bottom-up parsers

É We talked about LL(1), recursive descent, andLR(1) parsingÉ There are many other parsing algorithms:

É LALR (lookahead LR)É SLR (simple)É GLR (generalized)É IELR (inadequacy elimination)É Earley

Compiler Construction 11/48

Page 18: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Compiler Construction 12/48

Page 19: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

The Front End

É Lexical AnalysisÉ Detects inputs with illegal tokens

É Syntactic AnalysisÉ Detects inputs with ill-formed parse trees

É Semantic AnalysisÉ Last front end phaseÉ Detects more invalid inputs

Compiler Construction 12/48

Page 20: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Intro to Type Checking

É We have just parsed our programÉ It followed the rules of a context-free grammarÉ We know it is a structurally valid program

É Ultimately, programming languages are notcontext-freeÉ Need a rigorous analysis to tell us about semantic

validity of the programÉ Basically, a laundry list of tasks

Compiler Construction 13/48

Page 21: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Intro to Type CheckingÉ We have just parsed our program

É It followed the rules of a context-free grammarÉ We know it is a structurally valid program

É Ultimately, programming languages are notcontext-freeÉ Need a rigorous analysis to tell us about semantic

validity of the programÉ Basically, a laundry list of tasks

Compiler Construction 13/48

Page 22: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Type Checking Summary

Scoping rules match identifier uses withidentifier definitions.A type is a set of values coupled with a setof operations on those values.A type system specifies which operationsare valid for which types.Type checking can be done statically (atcompile time) or dynamically (at run time).

Compiler Construction 14/48

Page 23: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

What’s Wrong?

Example 1:1 l e t y : I n t in2 x + 3

Example 2:1 l e t y : S t r i n g <− " abc " in2 y + 3

Compiler Construction 15/48

Page 24: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Semantic Analysis: Why?

É Parsing cannot catch some errorsÉ Some language constructs are not

context-free.É All used variables must have been declared

(scoping)É All methods must be invoked with arguments of

the proper type (i.e. typing)

Compiler Construction 16/48

Page 25: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

What does semantic analysis do?Lots of things! In Cool:

É All identifiers are declaredÉ Static typesÉ Inheritance relationshipsÉ Classes defined only onceÉ Methods in a class defined only onceÉ Reserved identifiers are not misusedÉ several others...

The requirements are language-dependent.

É Which of these are checked by Python? C++?

Compiler Construction 17/48

Page 26: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Scope

Scoping rules match identifier useswith identifier declarationsÉ Critical analysis in most

languages (including Cool)

Compiler Construction 18/48

Page 27: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Scope

É The scope of an identifier is the portion of aprogram in which that identifier is accessibleÉ The same identifier may refer to different

things in different parts of the programÉ Different scopes for the same name do not overlap

É An identifier may have restricted scope

Compiler Construction 19/48

Page 28: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Static vs. Dynamic Scope

Most languages have static scopeÉ Scope depends only on the program

text, not the run-time behaviorÉ Cool is statically scoped

A few languages are dynamically scopedÉ LATEX, SNOBOL

Compiler Construction 20/48

Page 29: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Static vs. Dynamic Scope1 int x;23 int main() {4 x = 2;5 f();6 g();7 }8 void f () {9 int x = 3;

10 h();11 }12 void g () {13 int x = 4;14 h();15 }16 void h() {17 printf("%d\n", x);18 }

Compiler Construction 21/48

Page 30: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Static vs. Dynamic Scope1 int x;23 int main() {4 x = 2;5 f();6 g();7 }8 void f () {9 int x = 3;

10 h();11 }12 void g () {13 int x = 4;14 h();15 }16 void h() {17 printf("%d\n", x);18 }

Static scoping output:22

Dynamic scoping output:34

Compiler Construction 21/48

Page 31: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Static Scoping Example

1 let x : Int <- 0 in2 {3 x;4 { let x : Int <- 1 in5 x ;6 };7 x;8 }

Compiler Construction 22/48

Page 32: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Static Scoping Example

9 let x : Int <- 0 in10 {11 x;12 { let x : Int <- 1 in13 x ;14 };15 x;16 }

Uses of x refer to closest enclosing definition.

Compiler Construction 22/48

Page 33: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Scope in Cool

É Cool identifier bindings are introduced byÉ Class declarations (class names)É Method definitions (method names)É Let expressions (object id’s)É Formal parameters (object id’s)É Attribute definitions in a class (object id’s)É Case expressions (object id’s)

Compiler Construction 23/48

Page 34: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Implementing the Most-CloselyNested Rule

Much of semantic analysis can be expressed as arecursive descent of an AST!

É Process an AST node nÉ Process the children of nÉ Finish processing the node n

Compiler Construction 24/48

Page 35: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Implementing ... (2)

Example: the scope of let bindings is one subtree

1 l e t x : I n t <− 0 in expr

x can be used in subtree expr

Compiler Construction 24/48

Page 36: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Symbol TablesRecall: let x : Int <- 0 in expr

Idea:É Before processing expr, add definition of x to

current definitions (override any previousdefinition)É After processing expr, remove definition of x

and restore old definition of x (if any)

A symbol table is a data structure that tracks thecurrent bindings of identifiersÉ Part of PA4É How might we implement this?

Compiler Construction 25/48

Page 37: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Exceptions to Most-closely Nested

Not all kinds of identifiers follow the most-closelynested rule

Class definitions in Cool are globally visiblethroughout all of the program

Implication: class names can be used before it isdefined

Compiler Construction 26/48

Page 38: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Example: Use before define

1 class Foo {2 ... let y : Test in ...3 };4 class Test {5 ...6 };

Compiler Construction 27/48

Page 39: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

More Scope in Cool

Attribute names are global within the class in whichthey are defined1 class Foo {2 f() : Int { tm };3 tm : Int <- 0;4 };

Compiler Construction 28/48

Page 40: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

More Scope in Cool

Method and attribute names have complex rules

A method need not be defined in the class in whichit is used, but in some parent classÉ that’s inheritance

Methods may also be redefined (overridden)É Which languages allow this? Cool does

Compiler Construction 29/48

Page 41: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Class DefinitionsClass names can be used before being definedWe can’t check this property in one pass :(

SolutionÉ Pass 1: Collect all the class namesÉ Pass 2: Do the checkingÉ ?É Pass 4: Profit!

Semantic analysis requiresmultiple passes

Compiler Construction 30/48

Page 42: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Types

What is a Type?É The notion varies from language to language

ConsensusÉ A set of valuesÉ A set of operations on those values

Classes are one instantiation of the modern notionof type

Compiler Construction 31/48

Page 43: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Why do we need types?

Consider this assembly:

addi, $r1, $r2, $r3

What are the types of r1, r2, and r3?

Compiler Construction 32/48

Page 44: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Types and Operations

Cretain operations are legal or valid for values ofeach type

É It doesn’t make sense to add a function pointerand an integer in CÉ It does make sense to add two integers

É Both are the same in assembly!

Compiler Construction 33/48

Page 45: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Type Systems

A language’s type system specifies whichoperations are valid for which types

The goal of type checking is to ensure that operationsare used with the correct typesÉ Enforces intended interpretation of values,

because nothing else will!É Our last, best hope ... for victorious execution

Type systems provide a concise formalization of thesemantic checking rules

Compiler Construction 34/48

Page 46: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

What Can Types Do?

Can detect certain kinds of errors Memory errors:

É Reading from an invalid pointer, etc.

Violation of abstraction boundariesclass FileSystem{

open ( x : String) : File {...

};};

class Client {f( fs : FileSystem ) {

File fdesc <- fs.open("foo")...

}};

The f method can’t see data inside fdesc!

Compiler Construction 35/48

Page 47: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Type Checking Overview

Three kinds of languages:

É Statically typed: All or almost all checking oftypes is done as part of compilation (C, Java,Cool)É Dynamically typed: Almost all checking of

types is done during execution (Python, PHP,Ruby)É Untyped: No type checking (machine code)

Compiler Construction 36/48

Page 48: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

The Type Wars

Competing views on static vs. dynamic typing

É Static typing proponents say:É Static checking catches many programming errors at

compile timeÉ Avoids overhead of runtime type checks

É Dynamic typing proponents say:É Static type systems are restrictiveÉ Rapid prototyping is easier in a dynamic type

system

Compiler Construction 37/48

Page 49: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

The Type Wars (2)

In practice, most code is written in statically-typedlanguages with an “escape” mechanism.

É Unsafe casts in C, native methods in Java, ...

Dynamic typing (a.k.a. duck typing) is big in thescripting world

Compiler Construction 38/48

Page 50: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Cool Types

In Cool, types areÉ Class namesÉ SELF_TYPE

Technically, there are no native typesÉ Everything is boxed as an object

É Java Integer 6= int

The use declares types for all identifiers

The compiler infers types for expressionsÉ every expression

Compiler Construction 39/48

Page 51: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Type checking and Type Inference

Type Checking is the process of verifyingfully-typed programs

Type Inference is the process of filling in missingtype information

Sometimes used interchangably

Compiler Construction 40/48

Page 52: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Rules of InferenceYou have survived formal languages in specifyingparts ot he compilerÉ Regular expressions (lexer)É Context-free grammars (parser)

We uselogical rules of inferenceas a formalism for type in-ference

Compiler Construction 41/48

Page 53: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Rules of Inference?

Inference rules have the form

If Hypothesis is true, then Conclusion is true

Type checking computes via reasoning:

If E1 and E2 have certain types,then E3 has a certain type

Rules of inference are a compact notation for“If-then” statements

Compiler Construction 42/48

Page 54: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

From English to Inference Rules

Start with a simplified system and gradually addfeatures...

Building blocks

É Symbol “∧” is “and”É Symbol “⇒” is “if-then”É x:T is “x has type T”

Compiler Construction 43/48

Page 55: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

English to Inference Rules (2)

If e1 has type Int and e2 has type Int,then e1+ e2 has type Int

(e1 has type Int ∧ e2 has type Int)⇒e1+ e2 has type Int

(e1 : Int∧ e2 : Int)⇒ e1+ e2 : Int

This is an inference rule!É e1 : Int∧ e2 : Int are two hypotheses,

e1+ e2 : Int is a conclusion!

Compiler Construction 44/48

Page 56: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Notation for Inference RulesBy tradition, inference rules are written

`Hypothesis1... `Hypothesisn

`Conclusion

Cool type rules have hypotheses and conclusions ofthe form:

` e : T

`means “we can prove that...”

Compiler Construction 45/48

Page 57: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Examples

` i : Int Int

(i is an Integer)

` e1 : Int` e2 : Int` e1+ e2 : Int Add

(the result of adding to Ints is an Int)

Compiler Construction 46/48

Page 58: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Examples (2)

` e1 : Int` e2 : Int` e1+ e2 : Int Add

É These rules give templates describing how totype integers and + expressions...É We can fill in the templates, producing a

complete typing for any expression!

` false : Int` true : Int

` true+ false : IntCompiler Construction 47/48

Page 59: Semantic Analysis - Lecture 9kjleach.eecs.umich.edu/c18/l9.pdf · 4.Code Generation 5.Optimization Compiler Construction 4/48. Compilers Reminder É Remember, we want to support developers’

Example: 1+2

` 1 : Int` 2 : Int` 1+ 2 : Int

Compiler Construction 48/48