Writing Evaluators MIF08. Laure Gonnord

Save this PDF as:
 WORD  PNG  TXT  JPG

Size: px
Start display at page:

Download "Writing Evaluators MIF08. Laure Gonnord"

Transcription

1 Writing Evaluators MIF08 Laure Gonnord

2 Evaluators, what for? Outline 1 Evaluators, what for? 2 Implementation Laure Gonnord (Lyon1/FST) Writing Evaluators 2 / 21

3 Evaluators, what for? Analysis Phases source code lexical analysis sequence of lexems (tokens) syntactic analysis (Parsing) abstract syntax tree (AST ) semantic analysis abstract syntax (+ symbol table) Laure Gonnord (Lyon1/FST) Writing Evaluators 3 / 21

4 Evaluators, what for? Until now We have parsed, and evaluate in semantic actions. But we want: more structure. an easier way to perform actions (not in the.g4 file). Laure Gonnord (Lyon1/FST) Writing Evaluators 4 / 21

5 Evaluators, what for? Notion of Abstract Syntax Tree = int y + 12 * 4 x int AST: memory representation of a program; Node: a language construct; Sub-nodes: parameters of the construct; Leaves: usually constants or variables. Laure Gonnord (Lyon1/FST) Writing Evaluators 5 / 21

6 Evaluators, what for? Separation of concerns The semantics of the program could be defined in the semantic actions (of the grammar). Usually though: Syntax analyzer only produces the AST; The rest of the compiler directly works with this AST. Why? Manipulating a tree (AST) is easy (recursive style); Separate language syntax from language semantics; During later compiler phases, we can assume that the AST is syntactically correct simplifies the rest of the compilation. Laure Gonnord (Lyon1/FST) Writing Evaluators 6 / 21

7 Evaluators, what for? Running example : Numerical expressions This is an abstract syntax (no more parenthesis,... ): Let us construct an AST to: e ::= c constant x variable e + e add e e mult... Evaluate this expression (by tree traversal) Later: generate code for these expressions (by tree traversal) Laure Gonnord (Lyon1/FST) Writing Evaluators 7 / 21

8 Implementation Outline 1 Evaluators, what for? 2 Implementation Old-school way Evaluators with visitors Laure Gonnord (Lyon1/FST) Writing Evaluators 8 / 21

9 Implementation Old-school way Outline 1 Evaluators, what for? 2 Implementation Old-school way Evaluators with visitors Laure Gonnord (Lyon1/FST) Writing Evaluators 9 / 21

10 Implementation Old-school way Explicit construction of the AST Declare a type for the abstract syntax. Construct instances of these types during parsing (trees). Evaluate with tree traversal. Laure Gonnord (Lyon1/FST) Writing Evaluators 10 / 21

11 Implementation Old-school way Example in Java 1/3 AST definition in Java: one class per language construct. public class APlus extends AExpr { AExpr e1, e2 ; public APlus ( AExpr e1, AExpr e2 ) { this. e1=e1 ; this. e2=e2 ; } } public class AMinus extends AExpr {... Laure Gonnord (Lyon1/FST) Writing Evaluators 11 / 21

12 Implementation Old-school way Example in Java 2/3 The parser builds an AST instance using AST classes defined previously. ArithExprASTParser.g4 parser grammar A r i t h E x p r A S T P a r s e r ; options { tokenvocab = ArithExprASTLexer; } prog returns [ AExpr e ] : expr EOF { $e= $expr. e; } ; // We create an AExpr instead of computing a value expr returns [ AExpr e ] : LPAR x= expr RPAR { $e=$x. e; } INT { $e = new AInt ( $INT. int ) ; } e1 = expr PLUS e2 = expr { $e = new APlus ( $e1. e,$e2. e ) ; } e1 = expr MINUS e2 = expr { $e = new AMinus ( $e1. e,$e2. e ) ; } ; Laure Gonnord (Lyon1/FST) Writing Evaluators 12 / 21

13 Implementation Old-school way Example in Java 3/3 Evaluation is an eval function per class: AExpr.java p u b l i c a b s t r a c t class AExpr { a b s t r a c t i n t eval ( ) ; / / need to provide semantics } APlus.java public class APlus extends AExpr { AExpr e1, e2 ; public APlus ( AExpr e1, AExpr e2 ) { this. e1=e1 ; this. e2=e2 ; } / / semantics below i n t eval ( ) { return ( e1. eval ( ) + e2. eval ( ) ) ; } } Laure Gonnord (Lyon1/FST) Writing Evaluators 13 / 21

14 Implementation Evaluators with visitors Outline 1 Evaluators, what for? 2 Implementation Old-school way Evaluators with visitors Laure Gonnord (Lyon1/FST) Writing Evaluators 14 / 21

15 Implementation Evaluators with visitors Principle - OO programming The visitor design pattern is a way of separating an algorithm from an object structure on which it operates.[...] In essence, the visitor allows one to add new virtual functions to a family of classes without modifying the classes themselves; instead, one creates a visitor class that implements all of the appropriate specializations of the virtual function. Laure Gonnord (Lyon1/FST) Writing Evaluators 15 / 21

16 Implementation Evaluators with visitors Application Designing evaluators / tree traversal in ANTLR-Python The ANTLR compiler generates a Visitor class. We override this class to traverse the parsed instance. Laure Gonnord (Lyon1/FST) Writing Evaluators 16 / 21

17 Implementation Evaluators with visitors Example with ANTLR/Python 1/3 AritParser.g4 expr: expr mdop expr # m u l t i p l i c a t i o n E x p r expr pmop expr # a d d i t i v e E x p r atom # atomexpr ; atom : INT # int ID # id '( ' expr ') ' # parens compilation with -Dlanguage=Python3 -visitor Laure Gonnord (Lyon1/FST) Writing Evaluators 17 / 21

18 Implementation Evaluators with visitors Example with ANTLR/Python 2/3 -generated file class A r i t V i s i t o r ( P a r s e T r e e V i s i t o r ) :... # V i s i t a parse t r e e produced by A r i t P a r s e r # m u l t i p l i c a t i o n E x p r. def v i s i t M u l t i p l i c a t i o n E x p r ( s e l f, c t x ) : r e t u r n s e l f. v i s i t C h i l d r e n ( c t x ).. # V i s i t a parse t r e e produced by A r i t P a r s e r #atomexpr. def visitatomexpr ( s e l f, c t x ) : r e t u r n s e l f. v i s i t C h i l d r e n ( c t x ) Laure Gonnord (Lyon1/FST) Writing Evaluators 18 / 21

19 Implementation Evaluators with visitors Example with ANTLR/Python 3/3 Visitor class overriding to write the evaluator: MyAritVisitor.py class M y A r i t V i s i t o r ( A r i t V i s i t o r ) : # V i s i t a parse t r e e produced by A r i t P a r s e r # i n t. def v i s i t I n t ( s e l f, c t x ) : value = i n t ( c t x. gettext ( ) ) ; r e t u r n value ; def v i s i t M u l t i p l i c a t i o n E x p r ( s e l f, c t x ) : l e f t v a l = s e l f. v i s i t ( c t x. expr ( 0 ) ) r i g h t v a l = s e l f. v i s i t ( c t x. expr ( 1 ) ) myop = s e l f. v i s i t ( c t x. mdop ( ) ) i f ( myop == ) : r e t u r n l e f t v a l r i g h t v a l else : r e t u r n l e f t v a l / r i g h t v a l Laure Gonnord (Lyon1/FST) Writing Evaluators 19 / 21

20 Implementation Evaluators with visitors Nice Picture (Lab#3) Arit.g4 Tree.py antlr -visitor inherits from AritParser.py AritVisitor.py inherits from MyAritVisitor.py Laure Gonnord (Lyon1/FST) Writing Evaluators 20 / 21

21 Implementation Evaluators with visitors From grammars to evaluators - summary The meaning of each operation/grammar rule is now given by the implementation of the associated function in the visitor. The visitor performs a tree traversal on the structure of the parse tree. Laure Gonnord (Lyon1/FST) Writing Evaluators 21 / 21

22 Types, Typing MIF08 Laure Gonnord

23 Typing + Laure Gonnord (Lyon1/FST) Typing (simple) programs 2 / 18

24 Typing / Laure Gonnord (Lyon1/FST) Typing (simple) programs 3 / 18

25 Typing If you write: "5" + 37 what do you want to obtain a compilation error? (OCaml) an exec error? (Python) the int 42? (Visual Basic, PHP) the string "537"? (Java) anything else? and what about 37 / "5"? Typing: an analysis that gives a type to each subexpression, and reject incoherent programs. Laure Gonnord (Lyon1/FST) Typing (simple) programs 4 / 18

26 When Dynamic typing (during exec): Lisp, PHP, Python Static typing (at compile time): C, Java, OCaml Here: the second one. Laure Gonnord (Lyon1/FST) Typing (simple) programs 5 / 18

27 Typing objectives well typed programs do not go wrong Should be decidable. It should reject programs like (1 2) in OCaml, or 1+"toto" in C before an actual arror in the evaluation of the expression: this is safety. The type system should be expressive enough and not reject too many programs. (expressivity) Laure Gonnord (Lyon1/FST) Typing (simple) programs 6 / 18

28 Several solutions All sub-expressions are anotated by a type fun (x : int) let (y : int) = (+ :)(((x : int), (1 : int)) : int int) in easy to verify, but tedious for the programmer Annotate only variable declarations (Pascal, C, Java,... ) fun (x : int) let (y : int) = +(x, 1) in y Only annotate function parameters fun (x : int) let y = +(x, 1) in y Do nothing : complete inference : Ocaml, Haskell,... Laure Gonnord (Lyon1/FST) Typing (simple) programs 7 / 18

29 Simple Type Checking for mini-while, theory Outline 1 Simple Type Checking for mini-while, theory 2 A bit of implementation (for expr) Laure Gonnord (Lyon1/FST) Typing (simple) programs 8 / 18

30 Simple Type Checking for mini-while, theory Mini-While Syntax Expressions: Mini-while: e ::= c constant x variable e + e addition e e multiplication... S(Smt) ::= x := expr assign skip do nothing S 1 ; S 2 sequence if b then S 1 else S 2 test while b do S done loop Laure Gonnord (Lyon1/FST) Typing (simple) programs 9 / 18

31 Simple Type Checking for mini-while, theory Typing judgement We will define how to compute typing judgements denoted by: Γ e : τ and means in environment Γ, expression e has type τ Γ associates a type Γ(x) to all free variables x in e (in this course, computed from the variable declarations). Here types are basic types: Int String Bool Laure Gonnord (Lyon1/FST) Typing (simple) programs 10 / 18

32 Simple Type Checking for mini-while, theory Typing rules for expr (or bool,... ) Γ x : Γ(x) Γ n : int Γ e 1 : int Γ e 2 : int Γ e 1 + e 2 : int An expression is well typed if there is a proof tree for it with regular applications of the rules, and whose leaves are axioms. Laure Gonnord (Lyon1/FST) Typing (simple) programs 11 / 18

33 Simple Type Checking for mini-while, theory Hybrid expressions What if we have ? reject? compute a float! This is type coercion. Laure Gonnord (Lyon1/FST) Typing (simple) programs 12 / 18

34 Simple Type Checking for mini-while, theory More complex expressions What if we have types pointer of bool or array of int? We might want to check equivalence (for addition... ). This is called structural equivalence (see Dragon Book, type equivalence ). This is solved by a basic graph traversal. Laure Gonnord (Lyon1/FST) Typing (simple) programs 13 / 18

35 Simple Type Checking for mini-while, theory Typing rules for statements - See TD2 Idea: the type is void otherwise typing error Γ e : t Γ(x) : t t {int, bool} Γ x := e : void Γ b : bool Γ S : void Γ while b do S done : void A program is well typed if there is a proof tree with regular applications of the rules whose leaves are axioms. Laure Gonnord (Lyon1/FST) Typing (simple) programs 14 / 18

36 A bit of implementation (for expr) Outline 1 Simple Type Checking for mini-while, theory 2 A bit of implementation (for expr) Laure Gonnord (Lyon1/FST) Typing (simple) programs 15 / 18

37 A bit of implementation (for expr) Principle of type checking Gamma is constructed with lexing information or parsing (variable declaration with types). Rules are semantic actions. The semantic actions are responsible for the evaluation order, as well as typing errors. Laure Gonnord (Lyon1/FST) Typing (simple) programs 16 / 18

38 A bit of implementation (for expr) Type Checking (here): visitor (Lab3) MyMuTypingVisitor.py def v i s i t A d d i t i v e E x p r ( s e l f, c t x ) : l v a l t y p e = s e l f. v i s i t ( c t x. expr ( 0 ) ) r v a l t y p e = s e l f. v i s i t ( c t x. expr ( 1 ) ) op = s e l f. v i s i t ( c t x. oplus ( ) ) i f l v a l t y p e == r v a l t y p e : r e t u r n l v a l t y p e e l i f { l v a l t y p e, r v a l t y p e } == { BaseType. Integer, BaseType. F l o a t } : r e t u r n BaseType. F l o a t e l i f op == u + and any ( v t == BaseType. S t r i n g f o r v t i n ( r v a l t y p e, l v a l t y p e ) ) : r e t u r n BaseType. S t r i n g else : r a i s e SyntaxError ( " I n v a l i d type f o r a d d i t i v e operand " ) Laure Gonnord (Lyon1/FST) Typing (simple) programs 17 / 18

39 A bit of implementation (for expr) Typing is more than type checking. Sometimes we want this information during code generation AST decorated with types (but not in this course) And we want informative errors: Type error at line 42 is not sufficient! Laure Gonnord (Lyon1/FST) Typing (simple) programs 18 / 18

40 Code Generation MIF08 Laure Gonnord oct 2017

41 Big picture source code lexical+syntactic analysis + typing decorated AST code production (numerous phases) assembly language Laure Gonnord (Lyon1/FST) Code Generation / 21

42 Rules of the Game here For this code generation: Still no functions and no non-basic types. (mini-while) Syntax-directed: one grammar rule a set of instructions. Code redundancy. No register reuse: everything will be stored on the stack. The Target Machine : LEIA (course #1) Laure Gonnord (Lyon1/FST) Code Generation / 21

43 3-address syntax-directed Code Generation Outline 1 3-address syntax-directed Code Generation Rules 2 Memory allocation 3 Toward a more efficient Code Generation Laure Gonnord (Lyon1/FST) Code Generation / 21

44 3-address syntax-directed Code Generation A first example (1/4) How do we translate: var x;y:int; x=4; y=12+x; Variable decl s visitor gives a place to each variable: x place0, y place1. Compute 4, store somewhere, then copy in x s place. Compute 12 + x : 12 in place1, copy the value of x in place2, then add, store in place3, then copy into y s place. the code generator will use a place generator called newtmp() Laure Gonnord (Lyon1/FST) Code Generation / 21

45 3-address syntax-directed Code Generation A first example: (2/4) Compute 4 and store in x (temp0) :.let temp2 4 copy temp0 temp2 Laure Gonnord (Lyon1/FST) Code Generation / 21

46 3-address syntax-directed Code Generation Objective 3-address LEIA Code Generation for the Mini-While language: All variables are int/bool. All variables are global. No functions with syntax-directed translation. Implementation in Lab. This is called three-adress code generation Laure Gonnord (Lyon1/FST) Code Generation / 21

47 3-address syntax-directed Code Generation Rules Outline 1 3-address syntax-directed Code Generation Rules 2 Memory allocation 3 Toward a more efficient Code Generation Laure Gonnord (Lyon1/FST) Code Generation / 21

48 3-address syntax-directed Code Generation Rules Code generation utility functions We will use: A new (fresh) temporary can be created with a newtemp() function. A new fresh label can be created with a newlabel() function. The generated instructions are closed to the LEIA ones (except for snif) Laure Gonnord (Lyon1/FST) Code Generation / 21

49 3-address syntax-directed Code Generation Rules Abstract Syntax Expressions: and statements: e ::= c constant x variable e + e addition e or e boolean or e < e less than... S(Smt) ::= x := expr assign skip do nothing S 1 ; S 2 sequence if b then S 1 else S 2 test while b do S done loop Laure Gonnord (Lyon1/FST) Code Generation / 21

50 3-address syntax-directed Code Generation Rules Code generation for expressions, example e ::= c (cte expr) dr <-newtemp() code.add(instructionlet(dr, c)) return dr this rule gives a way to generate code for any constant. Laure Gonnord (Lyon1/FST) Code Generation / 21

51 3-address syntax-directed Code Generation Rules Code generation for a boolean expression, example e ::= e 1 < e 2 dr <-newtemp() t1 <- GenCodeExpr(e1) t2 <- GenCodeExpr(e2) dr <- newtemp() endrel <- newlabel() code.add(instructionlet(dr, 0)) #if t1>=t2 jump to endrel code.add(instructioncondjump(endrel, t1, ">=", t2) code.add(instructionlet(dr, 1)) code.addlabel(endrel) return dr integer value 0 or 1. Laure Gonnord (Lyon1/FST) Code Generation / 21

52 3-address syntax-directed Code Generation Rules Code generation for commands, example if b then S1 else S2 lelse,lendif <-newlabels() t1 <- GenCodeExpr(b) #if the condition is false, jump to else code.add(instructioncondjump(lelse, t1, "=", 0)) GenCodeSmt(S1) #then code.add(instructionjump(lendif)) code.addlabel(lelse) GenCodeSmt(S2) #else code.addlabel(lendif) Laure Gonnord (Lyon1/FST) Code Generation / 21

53 Memory allocation Outline 1 3-address syntax-directed Code Generation 2 Memory allocation 3 Toward a more efficient Code Generation Laure Gonnord (Lyon1/FST) Code Generation / 21

54 Memory allocation A first example: from code to valid LC-3 (3/5) is not valid LEIA code! 3 kinds of allocation : All in registers (but?) place i register All in memory (here!) place i memory Something in the middle (later!) Laure Gonnord (Lyon1/FST) Code Generation / 21

55 Memory allocation A stack, why? Store constants, strings,... Provide an easy way to communicate arguments values (see later) Give place to store intermediate values (here) Laure Gonnord (Lyon1/FST) Code Generation / 21

56 Memory allocation LEIA stack emulation - from the archi course r 6 is initialised to the stack address. addresses will be computed from this base. The stack grows in the dir. of decreasing addresses!. R0 R1 ins1 ins2 x0000 R7 R8 x30fe x3000 x3001 x30ff stackend: x xffff Nice picture by N. Louvet Laure Gonnord (Lyon1/FST) Code Generation / 21

57 Memory allocation A first example: prelude/postlude 4/5 Here store r 1 on the stack! [init r6].let r1 4 sub r0 r6 1 ; first dec from r6 (and store some info!) wmem r1 [r0] ; now r1 can be recycled Laure Gonnord (Lyon1/FST) Code Generation / 21

58 Memory allocation A first example: prelude/postlude 5/5 The rest of the code generation:.set r6 stack [...] jump 0.align16 stackend:.reserve 42 stack: This is valid LEIA code that can be assembled and executed Laure Gonnord (Lyon1/FST) Code Generation / 21

59 Toward a more efficient Code Generation Outline 1 3-address syntax-directed Code Generation 2 Memory allocation 3 Toward a more efficient Code Generation Laure Gonnord (Lyon1/FST) Code Generation / 21

60 Toward a more efficient Code Generation Drawbacks of the former translation Drawbacks: redundancies (constants recomputations,... ) memory intensive loads and stores. we need a more efficient data structure to reason on: the control flow graph (CFG). (see next course) Laure Gonnord (Lyon1/FST) Code Generation / 21

Syntax Analysis MIF08. Laure Gonnord

Syntax Analysis MIF08. Laure Gonnord Syntax Analysis MIF08 Laure Gonnord Laure.Gonnord@univ-lyon1.fr Goal of this chapter Understand the syntaxic structure of a language; Separate the different steps of syntax analysis; Be able to write a

More information

Lexing, Parsing. Laure Gonnord sept Master 1, ENS de Lyon

Lexing, Parsing. Laure Gonnord  sept Master 1, ENS de Lyon Lexing, Parsing Laure Gonnord http://laure.gonnord.org/pro/teaching/capm1.html Laure.Gonnord@ens-lyon.fr Master 1, ENS de Lyon sept 2017 Analysis Phase source code lexical analysis sequence of lexems (tokens)

More information

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

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Static Semantics Lecture 15 (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1 Current Status Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing

More information

Partial Exam Compilation and Program Analysis (CAP) October, 24th, 2016 Duration: 2 Hours

Partial Exam Compilation and Program Analysis (CAP) October, 24th, 2016 Duration: 2 Hours http://laure.gonnord.org/pro/ CAP, ENSL, 2016/2017 Partial Exam Compilation and Program Analysis (CAP) October, 24th, 2016 Duration: 2 Hours Only one a4 sheet (10pt, recto/verso) is autorized. Instructions

More information

Time : 1 Hour Max Marks : 30

Time : 1 Hour Max Marks : 30 Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with

More information

Compilers. Compiler Construction Tutorial The Front-end

Compilers. Compiler Construction Tutorial The Front-end Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers

More information

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

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules. Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

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

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL 6.035 Project 3: Unoptimized Code Generation Jason Ansel MIT - CSAIL Quiz Monday 50 minute quiz Monday Covers everything up to yesterdays lecture Lexical Analysis (REs, DFAs, NFAs) Syntax Analysis (CFGs,

More information

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

More information

LECTURE 3. Compiler Phases

LECTURE 3. Compiler Phases LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent

More information

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

Syntax and Grammars 1 / 21

Syntax and Grammars 1 / 21 Syntax and Grammars 1 / 21 Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types What is a language? 2 / 21 What is a language?

More information

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

Crafting a Compiler with C (II) Compiler V. S. Interpreter Crafting a Compiler with C (II) 資科系 林偉川 Compiler V S Interpreter Compilation - Translate high-level program to machine code Lexical Analyzer, Syntax Analyzer, Intermediate code generator(semantics Analyzer),

More information

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev Fall 2015-2016 Compiler Principles Lecture 6: Intermediate Representation Roman Manevich Ben-Gurion University of the Negev Tentative syllabus Front End Intermediate Representation Optimizations Code Generation

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy

More information

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

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review

More information

5. Syntax-Directed Definitions & Type Analysis

5. Syntax-Directed Definitions & Type Analysis 5. Syntax-Directed Definitions & Type Analysis Eva Rose Kristoffer Rose NYU Courant Institute Compiler Construction (CSCI-GA.2130-001) http://cs.nyu.edu/courses/spring15/csci-ga.2130-001/lecture-5.pdf

More information

Compiler Code Generation COMP360

Compiler Code Generation COMP360 Compiler Code Generation COMP360 Students who acquire large debts putting themselves through school are unlikely to think about changing society. When you trap people in a system of debt, they can t afford

More information

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming The Big Picture Again Syntactic Analysis source code Scanner Parser Opt1 Opt2... Optn Instruction Selection Register Allocation Instruction Scheduling machine code ICS312 Machine-Level and Systems Programming

More information

Compiler Optimisation

Compiler Optimisation Compiler Optimisation 1 Introductory Lecture Hugh Leather IF 1.18a hleather@inf.ed.ac.uk Institute for Computing Systems Architecture School of Informatics University of Edinburgh 2018 Textbooks Engineering

More information

Chapter 3 (part 3) Describing Syntax and Semantics

Chapter 3 (part 3) Describing Syntax and Semantics Chapter 3 (part 3) Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax

More information

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

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. SEMANTIC ANALYSIS: Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. Parsing only verifies that the program consists of tokens

More information

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far Lecture Outline Operational Semantics of Cool Lecture 13 COOL operational semantics Motivation Notation The rules Prof. Aiken CS 143 Lecture 13 1 Prof. Aiken CS 143 Lecture 13 2 Motivation We must specify

More information

Operational Semantics. One-Slide Summary. Lecture Outline

Operational Semantics. One-Slide Summary. Lecture Outline Operational Semantics #1 One-Slide Summary Operational semantics are a precise way of specifying how to evaluate a program. A formal semantics tells you what each expression means. Meaning depends on context:

More information

Lecture 16: Static Semantics Overview 1

Lecture 16: Static Semantics Overview 1 Lecture 16: Static Semantics Overview 1 Lexical analysis Produces tokens Detects & eliminates illegal tokens Parsing Produces trees Detects & eliminates ill-formed parse trees Static semantic analysis

More information

Type Checking. Chapter 6, Section 6.3, 6.5

Type Checking. Chapter 6, Section 6.3, 6.5 Type Checking Chapter 6, Section 6.3, 6.5 Inside the Compiler: Front End Lexical analyzer (aka scanner) Converts ASCII or Unicode to a stream of tokens Syntax analyzer (aka parser) Creates a parse tree

More information

Compiler Theory Introduction and Course Outline Sandro Spina Department of Computer Science

Compiler Theory Introduction and Course Outline Sandro Spina Department of Computer Science Compiler Theory 001 - Introduction and Course Outline Sandro Spina Department of Computer Science ( course Books (needed during this My slides are based on the three books: Compilers: Principles, techniques

More information

Formal Languages and Compilers Lecture X Intermediate Code Generation

Formal Languages and Compilers Lecture X Intermediate Code Generation Formal Languages and Compilers Lecture X Intermediate Code Generation Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

CS 432 Fall Mike Lam, Professor. Code Generation

CS 432 Fall Mike Lam, Professor. Code Generation CS 432 Fall 2015 Mike Lam, Professor Code Generation Compilers "Back end" Source code Tokens Syntax tree Machine code char data[20]; int main() { float x = 42.0; return 7; } 7f 45 4c 46 01 01 01 00 00

More information

CS 415 Midterm Exam Spring 2002

CS 415 Midterm Exam Spring 2002 CS 415 Midterm Exam Spring 2002 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Good Luck! Score Fortran Algol 60 Compilation Names, Bindings, Scope Functional Programming

More information

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block Interpreters Prof. Clarkson Fall 2017 Today s music: Step by Step by New Kids on the Block Review Previously in 3110: functional programming modular programming data structures Today: new unit of course:

More information

Begin at the beginning

Begin at the beginning Begin at the beginning Expressions (Syntax) Exec-time Dynamic Values (Semantics) Compile-time Static Types 1. Programmer enters expression 2. ML checks if expression is well-typed Using a precise set of

More information

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

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis? Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator

More information

CS 11 Ocaml track: lecture 6

CS 11 Ocaml track: lecture 6 CS 11 Ocaml track: lecture 6 n Today: n Writing a computer language n Parser generators n lexers (ocamllex) n parsers (ocamlyacc) n Abstract syntax trees Problem (1) n We want to implement a computer language

More information

Decaf Language Reference

Decaf Language Reference Decaf Language Reference Mike Lam, James Madison University Fall 2016 1 Introduction Decaf is an imperative language similar to Java or C, but is greatly simplified compared to those languages. It will

More information

Compil M1 : Front-End

Compil M1 : Front-End Compil M1 : Front-End TD1 : Introduction à Flex/Bison Laure Gonnord (groupe B) http://laure.gonnord.org/pro/teaching/ Laure.Gonnord@univ-lyon1.fr Master 1 - Université Lyon 1 - FST Plan 1 Lexical Analysis

More information

Lecture Compiler Middle-End

Lecture Compiler Middle-End Lecture 16-18 18 Compiler Middle-End Jianwen Zhu Electrical and Computer Engineering University of Toronto Jianwen Zhu 2009 - P. 1 What We Have Done A lot! Compiler Frontend Defining language Generating

More information

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages Topics Covered Thus Far CMSC 330: Organization of Programming Languages Names & Binding, Type Systems Programming languages Ruby Ocaml Lambda calculus Syntax specification Regular expressions Context free

More information

Chapter 4 - Semantic Analysis. June 2, 2015

Chapter 4 - Semantic Analysis. June 2, 2015 Chapter 4 - Semantic Analysis June 2, 2015 The role of the semantic analyzer Compilers use semantic analysis to enforce the static semantic rules of a language It is hard to generalize the exact boundaries

More information

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far. Lecture Outline Operational Semantics of Cool COOL operational semantics Motivation Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Notation The rules CS781(Prasad) L24CG 1 CS781(Prasad)

More information

7. Introduction to Denotational Semantics. Oscar Nierstrasz

7. Introduction to Denotational Semantics. Oscar Nierstrasz 7. Introduction to Denotational Semantics Oscar Nierstrasz Roadmap > Syntax and Semantics > Semantics of Expressions > Semantics of Assignment > Other Issues References > D. A. Schmidt, Denotational Semantics,

More information

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999 Chapter 3: Syntax and Semantics Matt Evett Dept. Computer Science Eastern Michigan University 1999 Syntax and Semantics Syntax - the form or structure of the expressions, statements, and program units

More information

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions Semantic actions for declarations and expressions Semantic actions Semantic actions are routines called as productions (or parts of productions) are recognized Actions work together to build up intermediate

More information

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = Hello; Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";

More information

Type Checking and Type Inference

Type Checking and Type Inference Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

More information

IR Optimization. May 15th, Tuesday, May 14, 13

IR Optimization. May 15th, Tuesday, May 14, 13 IR Optimization May 15th, 2013 Tuesday, May 14, 13 But before we talk about IR optimization... Wrapping up IR generation Tuesday, May 14, 13 Three-Address Code Or TAC The IR that you will be using for

More information

Summary: Direct Code Generation

Summary: Direct Code Generation Summary: Direct Code Generation 1 Direct Code Generation Code generation involves the generation of the target representation (object code) from the annotated parse tree (or Abstract Syntactic Tree, AST)

More information

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

CD Assignment I. 1. Explain the various phases of the compiler with a simple example. CD Assignment I 1. Explain the various phases of the compiler with a simple example. The compilation process is a sequence of various phases. Each phase takes input from the previous, and passes the output

More information

COMPILERS BASIC COMPILER FUNCTIONS

COMPILERS BASIC COMPILER FUNCTIONS COMPILERS BASIC COMPILER FUNCTIONS A compiler accepts a program written in a high level language as input and produces its machine language equivalent as output. For the purpose of compiler construction,

More information

Introduction to Compiler Design

Introduction to Compiler Design Introduction to Compiler Design Lecture 1 Chapters 1 and 2 Robb T. Koether Hampden-Sydney College Wed, Jan 14, 2015 Robb T. Koether (Hampden-Sydney College) Introduction to Compiler Design Wed, Jan 14,

More information

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units Syntax - the form or structure of the expressions, statements, and program units Semantics - the meaning of the expressions, statements, and program units Who must use language definitions? 1. Other language

More information

TDDD55 - Compilers and Interpreters Lesson 3

TDDD55 - Compilers and Interpreters Lesson 3 TDDD55 - Compilers and Interpreters Lesson 3 November 22 2011 Kristian Stavåker (kristian.stavaker@liu.se) Department of Computer and Information Science Linköping University LESSON SCHEDULE November 1,

More information

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

COMPILERS AND INTERPRETERS Lesson 4 TDDD16 COMPILERS AND INTERPRETERS Lesson 4 TDDD16 Kristian Stavåker (kristian.stavaker@liu.se) Department of Computer and Information Science Linköping University TODAY Introduction to the Bison parser generator

More information

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

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

The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic Analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure

More information

Principles of Compiler Design

Principles of Compiler Design Principles of Compiler Design Intermediate Representation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Unambiguous Program representation

More information

GBIL: Generic Binary Instrumentation Language. Language Reference Manual. By: Andrew Calvano. COMS W4115 Fall 2015 CVN

GBIL: Generic Binary Instrumentation Language. Language Reference Manual. By: Andrew Calvano. COMS W4115 Fall 2015 CVN GBIL: Generic Binary Instrumentation Language Language Reference Manual By: Andrew Calvano COMS W4115 Fall 2015 CVN Table of Contents 1) Introduction 2) Lexical Conventions 1. Tokens 2. Whitespace 3. Comments

More information

FUNCTIONAL AND LOGIC PROGRAMS

FUNCTIONAL AND LOGIC PROGRAMS FUNCTIONAL AND LOGIC PROGRAMS Contents Language Specific Compilation Context Handling Identification Scope Overloading Imported Scope Type Checking Type table Type equivalence Coercions Casts and conversions

More information

Abstract Syntax Trees

Abstract Syntax Trees Abstract Syntax Trees COMS W4115 Prof. Stephen A. Edwards Fall 2007 Columbia University Department of Computer Science Parsing and Syntax Trees Parsing decides if the program is part of the language. Not

More information

Intermediate Representations

Intermediate Representations COMP 506 Rice University Spring 2018 Intermediate Representations source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

More information

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573 What is a compiler? Xiaokang Qiu Purdue University ECE 573 August 21, 2017 What is a compiler? What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g.,

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 09 IR (ackpatching) THEORY OF COMPILATION Eran Yahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec09.pptx Reference: Dragon 6.2,6.3,6.4,6.6 1 Recap Lexical analysis regular expressions identify

More information

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Error Handling Syntax-Directed Translation Recursive Descent Parsing Error Handling Syntax-Directed Translation Recursive Descent Parsing Lecture 6 by Professor Vijay Ganesh) 1 Outline Recursive descent Extensions of CFG for parsing Precedence declarations Error handling

More information

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler. Name: Midterm Exam PID: This is a closed-book exam; you may not use any tools besides a pen. You have 75 minutes to answer all questions. There are a total of 75 points available. Please write legibly;

More information

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

First Midterm Exam CS164, Fall 2007 Oct 2, 2007 P a g e 1 First Midterm Exam CS164, Fall 2007 Oct 2, 2007 Please read all instructions (including these) carefully. Write your name, login, and SID. No electronic devices are allowed, including cell phones

More information

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing Review of Parsing Abstract Syntax Trees & Top-Down Parsing Given a language L(G), a parser consumes a sequence of tokens s and produces a parse tree Issues: How do we recognize that s L(G)? A parse tree

More information

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

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper)

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper) Gradual Typing for Functional Languages Jeremy Siek and Walid Taha (presented by Lindsey Kuper) 1 Introduction 2 What we want Static and dynamic typing: both are useful! (If you re here, I assume you agree.)

More information

Project 1: Scheme Pretty-Printer

Project 1: Scheme Pretty-Printer Project 1: Scheme Pretty-Printer CSC 4101, Fall 2017 Due: 7 October 2017 For this programming assignment, you will implement a pretty-printer for a subset of Scheme in either C++ or Java. The code should

More information

Overview of Semantic Analysis. Lecture 9

Overview of Semantic Analysis. Lecture 9 Overview of Semantic Analysis Lecture 9 1 Midterm Thursday In class SCPD students come to campus for the exam Material through lecture 8 Open note Laptops OK, but no internet or computation 2 Outline The

More information

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

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem: Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd

More information

Scala, Your Next Programming Language

Scala, Your Next Programming Language Scala, Your Next Programming Language (or if it is good enough for Twitter, it is good enough for me) WORLDCOMP 2011 By Dr. Mark C. Lewis Trinity University Disclaimer I am writing a Scala textbook that

More information

Compilers Crash Course

Compilers Crash Course Compilers Crash Course Prof. Michael Clarkson CSci 6907.85 Spring 2014 Slides Acknowledgment: Prof. Andrew Myers (Cornell) What are Compilers? Translators from one representation of program code to another

More information

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013 COMP455: COMPILER AND LANGUAGE DESIGN Dr. Alaa Aljanaby University of Nizwa Spring 2013 Chapter 1: Introduction Compilers draw together all of the theory and techniques that you ve learned about in most

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS Objective PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS Explain what is meant by compiler. Explain how the compiler works. Describe various analysis of the source program. Describe the

More information

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4 CS412/413 Introduction to Compilers and Translators Spring 00 Outline Typechecking Symbol tables Using symbol tables for analysis Lecture 8: Semantic Analysis and Symbol Tables Lecture 8 CS 412/413 Spring

More information

TDDD55- Compilers and Interpreters Lesson 3

TDDD55- Compilers and Interpreters Lesson 3 TDDD55- Compilers and Interpreters Lesson 3 Zeinab Ganjei (zeinab.ganjei@liu.se) Department of Computer and Information Science Linköping University 1. Grammars and Top-Down Parsing Some grammar rules

More information

Building Compilers with Phoenix

Building Compilers with Phoenix Building Compilers with Phoenix Parser Generators: ANTLR History of ANTLR ANother Tool for Language Recognition Terence Parr's dissertation: Obtaining Practical Variants of LL(k) and LR(k) for k > 1 PCCTS:

More information

Syntactic Directed Translation

Syntactic Directed Translation Syntactic Directed Translation Translation Schemes Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

Syntax Intro and Overview. Syntax

Syntax Intro and Overview. Syntax Syntax Intro and Overview CS331 Syntax Syntax defines what is grammatically valid in a programming language Set of grammatical rules E.g. in English, a sentence cannot begin with a period Must be formal

More information

Introduction to Functional Programming in Haskell 1 / 56

Introduction to Functional Programming in Haskell 1 / 56 Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order

More information

Syntax-Directed Translation

Syntax-Directed Translation Syntax-Directed Translation ALSU Textbook Chapter 5.1 5.4, 4.8, 4.9 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 What is syntax-directed translation? Definition: The compilation

More information

MIDTERM EXAM (Solutions)

MIDTERM EXAM (Solutions) MIDTERM EXAM (Solutions) Total Score: 100, Max. Score: 83, Min. Score: 26, Avg. Score: 57.3 1. (10 pts.) List all major categories of programming languages, outline their definitive characteristics and

More information

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto Data Structures and Algorithms in Compiler Optimization Comp314 Lecture Dave Peixotto 1 What is a compiler Compilers translate between program representations Interpreters evaluate their input to produce

More information

CPSC 411, Fall 2010 Midterm Examination

CPSC 411, Fall 2010 Midterm Examination CPSC 411, Fall 2010 Midterm Examination. Page 1 of 11 CPSC 411, Fall 2010 Midterm Examination Name: Q1: 10 Q2: 15 Q3: 15 Q4: 10 Q5: 10 60 Please do not open this exam until you are told to do so. But please

More information

Intermediate Code, Object Representation, Type-Based Optimization

Intermediate Code, Object Representation, Type-Based Optimization CS 301 Spring 2016 Meetings March 14 Intermediate Code, Object Representation, Type-Based Optimization Plan Source Program Lexical Syntax Semantic Intermediate Code Generation Machine- Independent Optimization

More information

Lecture 12: Conditional Expressions and Local Binding

Lecture 12: Conditional Expressions and Local Binding Lecture 12: Conditional Expressions and Local Binding Introduction Corresponds to EOPL 3.3-3.4 Please review Version-1 interpreter to make sure that you understand how it works Now we will extend the basic

More information

Denotational Semantics. Domain Theory

Denotational Semantics. Domain Theory Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning

More information

Compilers and computer architecture: A realistic compiler to MIPS

Compilers and computer architecture: A realistic compiler to MIPS 1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

CS 536 Midterm Exam Spring 2013

CS 536 Midterm Exam Spring 2013 CS 536 Midterm Exam Spring 2013 ID: Exam Instructions: Write your student ID (not your name) in the space provided at the top of each page of the exam. Write all your answers on the exam itself. Feel free

More information

Compiler construction in4020 lecture 5

Compiler construction in4020 lecture 5 Compiler construction in4020 lecture 5 Semantic analysis Assignment #1 Chapter 6.1 Overview semantic analysis identification symbol tables type checking CS assignment yacc LLgen language grammar parser

More information

Programs as Data 6 Imperative languages, environment and store, micro-c

Programs as Data 6 Imperative languages, environment and store, micro-c Programs as Data 6 Imperative languages, environment and store, micro-c Peter Sestoft Monday 2012-10-01* www.itu.dk 1 Course overview Today A naïve imperative language C concepts Pointers and pointer arithmetics,

More information

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline Grade Weights Language Design and Overview of COOL CS143 Lecture 2 Project 0% I, II 10% each III, IV 1% each Midterm 1% Final 2% Written Assignments 10% 2.% each Prof. Aiken CS 143 Lecture 2 1 Prof. Aiken

More information

Ambiguity and Errors Syntax-Directed Translation

Ambiguity and Errors Syntax-Directed Translation Outline Ambiguity (revisited) Ambiguity and rrors Syntax-Directed Translation xtensions of CFG for parsing Precedence declarations rror handling Semantic actions Constructing a parse tree CS780(Prasad)

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 4a Andrew Tolmach Portland State University 1994-2016 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

Introduction to Compiler Construction

Introduction to Compiler Construction Introduction to Compiler Construction ASU Textbook Chapter 1 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 What is a compiler? Definitions: A recognizer. A translator. source

More information