Compiler (scalac, gcc) Compiler (scalac, gcc) I = 0 while (i < 10) { i = i + 1 } I = 0 while (i < 10) { i = i + 1 } source code i d 3 = 0 LF w i d 3 =

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Warm up Problem If , find .
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
CPSC 388 – Compiler Design and Construction
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
More type systems. Physical units A unit expression is defined by following grammar u, v := b | 1 | u * v | u -1 where u, v are unit expressions themselves.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Exercise: Balanced Parentheses
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Recursion in Scala. 2 Definitions A recursive method is a method that calls itself A method is indirectly recursive if it calls a method that calls a.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Exercise: Build Lexical Analyzer Part For these two tokens, using longest match, where first has the priority: binaryToken ::= ( z |1) * ternaryToken ::=
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
CSE 5317/4305 L6: Semantic Analysis1 Semantic Analysis Leonidas Fegaras.
1 Gentle Introduction to Programming Session 2: Functions.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Cse321, Programming Languages and Compilers 1 6/23/2015 Lecture #15, March. 5, 2007 Judgments for mini-Java Multiple type environments Class Hierarchy.
COS 320 Compilers David Walker. Outline Last Week –Introduction to ML Today: –Lexical Analysis –Reading: Chapter 2 of Appel.
Chapter 2 A Simple Compiler
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
Symbol Table (  ) Contents Map identifiers to the symbol with relevant information about the identifier All information is derived from syntax tree -
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
10-Sep-15 Classes. Classes and objects Scala is an Object-Oriented (O-O), functional language Object-Oriented (O-O) means it’s built around “objects”
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
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.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Compiler (scalac, gcc) Compiler (scalac, gcc) Id3 = 0 while (id3 < 10) { println(“”,id3); id3 = id3 + 1 } Id3 = 0 while (id3 < 10) { println(“”,id3); id3.
A Rule of While Language Syntax // Where things work very nicely for recursive descent! statmt ::= println ( stringConst, ident ) | ident = expr | if (
CPS 506 Comparative Programming Languages Syntax Specification.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Exercise Determine the output of the following program assuming static and dynamic scoping. Explain the difference, if there is any. object MyClass { val.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Compiler Construction FirstName1 LastName1 FirstName2 LastName2
Syntax (2).
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
JavaScript Assignment Statements Expressions Global Functions CST 200 JavaScript.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Exercise Solution for Exercise (a) {1,2} {3,4} a b {6} a {5,6,1} {6,2} {4} {3} {5,6} { } b a b a a b b a a b a,b b b a.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
UMBC CMSC 331 Case Classes in Scala Intro. Basic Properties Case classes are regular classes which export their constructor parameters and which provide.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Programming Languages 2nd edition Tucker and Noonan
Constructing Precedence Table
Parsing and Parser Parsing methods: top-down & bottom-up
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
What does it mean? Notes from Robert Sebesta Programming Languages
Overview of Compilation The Compiler BACK End
CMPE 152: Compiler Design September 13 Class Meeting
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Overview of Compilation The Compiler BACK End
Lecture 7: Introduction to Parsing (Syntax Analysis)
Abstract Syntax Prabhaker Mateti 1.
CMPE 152: Compiler Design September 27 Class Meeting
Chapter 3 Describing Syntax and Semantics.
Presentation transcript:

Compiler (scalac, gcc) Compiler (scalac, gcc) I = 0 while (i < 10) { i = i + 1 } I = 0 while (i < 10) { i = i + 1 } source code i d 3 = 0 LF w i d 3 = 0 LF w id3 = 0 while ( id3 < 10 ) id3 = 0 while ( id3 < 10 ) lexer characters words (tokens) trees parser assign while i 0 + i 1 assign < i 10 Type Checking i

Evaluating an Expression scala prompt: >def min1(x : Int, y : Int) : Int = { if (x min1(10,5) res1: Int = 6 How can we think about this evaluation? x  10 y  5 if (x < y) x else y+1

Computing types using the evaluation tree scala prompt: >def min1(x : Int, y : Int) : Int = { if (x min1(10,5) res1: Int = 6 How can we think about this evaluation? x : Int  10 y : Int  5 if (x < y) x else y+1

We can compute types without values scala prompt: >def min1(x : Int, y : Int) : Int = { if (x min1(10,5) res1: Int = 6 How can we think about this evaluation? x : Int y : Int if (x < y) x else y+1

We do not like trees upside-down

Leaves are Up type rules move from leaves to root

Type Judgements and Type Rules

if the (free) variables of e have types given by the type environment gamma, then e (correctly) type checks and has type T If e 1 type checks in gamma and has type T 1 and... and e n type checks in gamma and has type T n then e type checks in gamma and has type T type rule Type Judgements and Type Rules

Type Rules as Local Tree Constraints x : Int y : Int Type Rules for every type T, if then b has type Boolean, and...

Type Rules with Environment x : Int y : Int Type Rules

Type Checker Implementation Sketch def typeCheck(  : Map[ID, Type], e : ExprTree) : TypeTree = { e match { case Var(id) => { ?? } case If(c,e1,e2) => { ?? }... }} case Var(id) => {  (id) match case Some(t) => t case None => error(UnknownIdentifier(id,id.pos)) }

Type Checker Implementation Sketch case If(c,e1,e2) => { val tc = typeCheck( ,c) if (tc != BooleanType) error(IfExpectsBooleanCondition(e.pos)) val t1 = typeCheck( , e1); val t2 = typeCheck( , e2) if (t1 != t2) error(IfBranchesShouldHaveSameType(e.pos)) t1 }

Derivation Using Type Rules x : Int y : Int

Type Rule for Function Application

Type Rule for Function Application [Cont.] We can treat operators as variables that have function type We can replace many previous rules with application rule: ()

Computing the Environment of a Class object World { var data : Int var name : String def m(x : Int, y : Int) : Boolean {... } def n(x : Int) : Int { if (x > 0) p(x – 1) else 3 } def p(r : Int) : Int = { var k = r + 2 m(k, n(k)) } } We can type check each function m,n,p in this global environment

Extending the Environment class World { var data : Int var name : String def m(x : Int, y : Int) : Boolean {... } def n(x : Int) : Int { if (x > 0) p(x – 1) else 3 } def p(r : Int) : Int = { var k:Int = r + 2 m(k, n(k)) } }

Type Checking Expression in a Body class World { var data : Int var name : String def m(x : Int, y : Int) : Boolean {... } def n(x : Int) : Int { if (x > 0) p(x – 1) else 3 } def p(r : Int) : Boolean = { var k:Int = r + 2 m(k, n(k)) } }

Type Rule for Method Definitions Type Rule for Assignments Type Rules for Block: { var x 1 :T 1... var x n :T n ; s 1 ;... s m ; e }

Blocks with Declarations in the Middle just expression empty declaration is first statement is first

Rule for While Statement

Rule for a Method Call

Example to Type Check object World { var z : Boolean var u : Int def f(y : Boolean) : Int { z = y if (u > 0) { u = u – 1 var z : Int z = f(!y) + 3 z+z } else { 0 } }  0 = { (z, Boolean), (u, Int), (f, Boolean  Int) }  1 =  0  {(y, Boolean)} Exercise:

Solution

Overloading of Operators Not a problem for type checking from leaves to root Int x Int  Int String x String  String