Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing."— Presentation transcript:

1 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

2 Cse321, Programming Languages and Compilers 2 6/19/2015 Notices The Final Exam will be Monday, March 19, 2007. –Monday, Mar. 19, 2007. 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.

3 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.

4 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

5 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 * 3 2 2 6 3 30 5

6 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

7 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

8 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”)

9 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.

10 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)

11 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.

12 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)

13 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;

14 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.

15 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.

16 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 –

17 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!

18 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?

19 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

20 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

21 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

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


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

Similar presentations


Ads by Google