Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Programming Languages and Paradigms
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
1 Compiler Construction Intermediate Code Generation.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Tim Sheard Oregon Graduate Institute Lecture 4: Staging Interpreters CSE 510 Section FSC Winter 2004 Winter 2004.
Compiler Construction
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
FE.1 CSE4100 Final Exam Advice and Hints Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 191 Auditorium.
Programming Languages Structure
CS 280 Data Structures Professor John Peterson. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Imperative Programming
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
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.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
The TINY sample language and it’s compiler
Compiler Chapter# 5 Intermediate code generation.
Chapter 6 Programming Languages (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Basic Semantics Associating meaning with language entities.
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.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Advanced Functional Programming Tim Sheard 1 Lecture 6 Functional Programming Tim Sheard & Mark Jones Monads & Interpreters.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Code Generation CPSC 388 Ellen Walker Hiram College.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Lecture 9 Symbol Table and Attributed Grammars
Run-Time Environments Chapter 7
Programming Languages and Compilers (CS 421)
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Review: Chapter 5: Syntax directed translation
CMPE 152: Compiler Design October 4 Class Meeting
Chapter 6 Intermediate-Code Generation
CSE401 Introduction to Compiler Construction
Course Overview PART I: overview material PART II: inside a compiler
Recursive Procedures and Scopes
Presentation transcript:

Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing a compiler, Intermediate code Pascal-like language Run-time environments Calling sequence Variable references

Cse321, Programming Languages and Compilers 2 6/19/2015 Notices The Final Exam will be Monday, March 19, –Monday, Mar. 19, Time: 1930 – 2120 (7:30pm – 9:20pm). –It will NOT start at about 6:00 pm like our regular class meeting. –I have no control over this. – Project 3 is due Monday, March 19. I will accept projects until midnight. I must grade exams and projects and get all grades in before I leave on Thursday. So late projects will not be accepted.

Cse321, Programming Languages and Compilers 3 6/19/2015 Syntax Directed Translation Translation is directed by the syntax, or the structure of the program. Can be done –Directly, while parsing. –From the structure of an abstract syntax tree. Syntax directed translation attaches a meaning to every production (or piece of syntax) –This meaning is often given in terms of the meaning of the sub- expressions Often we think of this meaning being an attribute of each syntax node. Attribute grammars provide a natural way of describing this.

Cse321, Programming Languages and Compilers 4 6/19/2015 Example 1 Grammar E -> E + E E -> E * E E -> number Abstract Type datatype exp = Plus of exp * exp | Times of exp * exp | Num of int Meaning = integer value of E

Cse321, Programming Languages and Compilers 5 6/19/2015 Example 1 (cont) fun mean x = case x of Plus(x,y) => (mean x) + (mean y) | Times(x,y) => (mean x) * (mean y) | Num n => n + 5 *

Cse321, Programming Languages and Compilers 6 6/19/2015 Example 2 Meaning = Code to compute E –represented as a (string * string) fun mean x = case x of Plus(x,y) => let val (namex,codex) = (mean x) val (namey,codey) = (mean y) val new = newtemp() in (new, codex ^ codey ^ (new ^ “ = “ ^namex ^ “ + “ ^ namey)) end

Cse321, Programming Languages and Compilers 7 6/19/2015 Example 2 (cont) | Times(x,y) => let val (namex,codex) = (mean x) val (namey,codey) = (mean y) val new = newtemp() in (new, codex ^ codey ^ (new ^ “ = “ ^namex ^ “ * “ ^ namey)) end | Num n => let val new = newtemp() in (new, new ^” = “^(int2str n)) end

Cse321, Programming Languages and Compilers 8 6/19/2015 Example 2 (cont) + 5 * 3 2 (“T1”, “T1 = 5”) (“T2”, “T2 = 3”) (“T3”, “T3 = 2”) (“T4”, “T2 = 3 T3 = 2 T4 = T2 * T3” ) (“T5”, “T1 = 5 T2 = 3 T3 = 2 T4 = T2 * T3 T5 = T1 + T4”)

Cse321, Programming Languages and Compilers 9 6/19/2015 Example 3 – terms with variables datatype exp = Plus of exp * exp | Times of exp * exp | Num of int | Var of string; exception NoSuchVar of string; Meaning = function from environment to integer mean3: exp -> (string * int) list -> int An environment associates variables with their values. Could be a table, a linked list, a binary search tree, etc.

Cse321, Programming Languages and Compilers 10 6/19/2015 Example 3 (cont) fun mean3 x env = case x of Var s => (case List.find (fn (x,v) => x=s) env of NONE => raise (NoSuchVar s) | SOME(_,v) => v) | Plus(x,y) => (mean3 x) env + (mean3 y) env | Times(x,y) => (mean3 x) env * (mean3 y) env | Num n => n fun mean3 x = case x of Var s => (fn env => (case List.find (fn (x,v) => x=s) env of NONE => raise (NoSuchVar s) | SOME(_,v) => v))) | Plus(x,y) => (fn env => (mean3 x) env + (mean3 y) env) | Times(x,y) => (fn env => (mean3 x) env * (mean3 y) env) | Num n => (fn env => n)

Cse321, Programming Languages and Compilers 11 6/19/2015 Example 4 – terms with assignment datatype exp = Plus of exp * exp | Times of exp * exp | Num of int | Var of string | Assign of string * exp Meaning = function from environments to environments and integers. mean4: exp -> (string * int) list -> (string * int) list * int Best to think of this has changing the state of the variables.

Cse321, Programming Languages and Compilers 12 6/19/2015 Updating the state fun update [] var value = [(var,value)] | update ((s,v)::xs) var value = if s=var then (s,value)::xs else (s,v)::(update xs var value); X := Y + (Z := 4) [(Y,2)] [(Y,2),(Z,4),(X,6)] X := Y + (Z := 4)

Cse321, Programming Languages and Compilers 13 6/19/2015 Example 4 (cont) fun mean4 x env = case x of Var s => (case List.find (fn (x,v) => x=s) env of NONE => raise (NoSuchVar s) | SOME(_,v) => (env,v)) | Plus(x,y) => let val (env2,x') = (mean4 x) env val (env3,y') = (mean4 y) env2 in (env3,x' + y') end | Times(x,y) => let val (env2,x') = (mean4 x) env val (env3,y') = (mean4 y) env2 in (env3,x' * y') end | Num n => (env,n) | Assign(x,exp) => let val (env2,exp') = (mean4 exp) env in (update env2 x exp',exp') end;

Cse321, Programming Languages and Compilers 14 6/19/2015 Discussion The meaning of a program varies widely –A simple value, like an integer –An abstract representation of some thing else (A string representing a program in another language). –A function. –A state transformer. (A function from the state of the variables to a new state for the variables) Key properties –Every term or sub-expression has its own meaning independent of the other terms. –Computable as an attribute computation. Thinking abstractly can help us write a compiler.

Cse321, Programming Languages and Compilers 15 6/19/2015 When writing a compiler Think only about Abstract syntax –this is fairly stable, concrete syntax changes much more often Use algebraic datatypes to encode the abstract syntax –use a language which supports algebraic datatypes –Makes use of types to separate expressions from statements, etc. Figure out what the result of executing a program is –this is your “value” domain. –values can be quite complex –think about a purely functional encoding. This helps you get it right. It doesn’t have to be how you actually encode things or have anything to do with the result of compilation. This is to help you understand what is going on in a way abstract from the real translation.

Cse321, Programming Languages and Compilers 16 6/19/2015 When writing a compiler (cont.) Construct a purely functional interpreter for the abstract syntax. –This becomes your “reference” implementation. It is the standard by which you judge the correctness of your compiler. To build the compiler think about what happens when –What is known completely at compile time? –What is known only at run time? –Which data structures will correspond to each component. »Type environments or intermediate code at compile-time »Run-time environment at run-time –

Cse321, Programming Languages and Compilers 17 6/19/2015 When writing a compiler (cont.) Analyze the target environment –What properties does it have? –What are the primitive actions that get things done? –These will help you structure the run-time environment. Relate the primitive actions of the target environment to the values of the interpreter. –Can the values be implemented by the primitive actions? –If so you have a possible path to a compiler!

Cse321, Programming Languages and Compilers 18 6/19/2015 Intermediate Code The goal of most compilers is to generate a sequence of intermediate forms. –An intermediate form is some "machine independent " intermediate code –We will create it from a walk of the abstract syntax tree (an attribute computation). What possible intermediate forms are there? What kind of things should appear in this intermediate code?

Cse321, Programming Languages and Compilers 19 6/19/2015 Needs Need to implement the primitive operators +,-, * etc. Need to move data around (copy). Need to make jumps and branches. –conditional jumps –unconditional jumps –label statements (perhaps this can be avoided, or removed in a second pass) Need to deal with allocation of memory for objects and temporaries. Need to deal with parameter passing Need to deal with method call and return Need to deal with indexing of arrays

Cse321, Programming Languages and Compilers 20 6/19/2015 Possible Exam Topics Type Checking –How to represent types as data –Constructing equality functions over types –Type rules as attribute computations –Translating type rules into ML-programs –Type coercions performed at type checking time –Declarations as type checking environment producing functions –using type-checking inference rules as specifications for type checkers. Mini Java Type Checking –The current object –Judgments about classes and hierarchies –The role of subtyping –Tables for inheritance hierarchies

Cse321, Programming Languages and Compilers 21 6/19/2015 Possible Exam Topics 2 Syntax directed translation –Intermediate code –Translating Block structured languages –Handling variables references (using tables) Type checking Mutual recursion –2 pass approach »compute types for each mutually-recursive item »then check each is consistent under the collected types Shift reduce Parsing –sets of items construction –using ambiguity –Resolving shift reduce errors –using ml-yacc

Cse321, Programming Languages and Compilers 22 6/19/2015 Possible Exam Topics 3 Procedural Abstraction –Name spaces and scoping –Activation records –Parameter passing