cse321, programming languages and compilers 1 6/19/2015 lecture #18, march 14, 2007 syntax directed...

22
Cse321, Programming Languages and Compilers 1 03/27/22 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

Post on 20-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Cse321, Programming Languages and Compilers

104/18/23

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

204/18/23

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.

Cse321, Programming Languages and Compilers

304/18/23

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

404/18/23

Example 1

• GrammarE -> E + E

E -> E * E

E -> number

• Abstract Typedatatype exp = Plus of exp * exp

| Times of exp * exp

| Num of int

• Meaning = integer value of E

Cse321, Programming Languages and Compilers

504/18/23

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

Cse321, Programming Languages and Compilers

604/18/23

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

704/18/23

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

804/18/23

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

904/18/23

Example 3 – terms with variablesdatatype 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

1004/18/23

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

1104/18/23

Example 4 – terms with assignmentdatatype 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

1204/18/23

Updating the statefun 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

1304/18/23

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

1404/18/23

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

1504/18/23

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

1604/18/23

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

1704/18/23

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

1804/18/23

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

1904/18/23

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

2004/18/23

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

2104/18/23

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

2204/18/23

Possible Exam Topics 3

• Procedural Abstraction– Name spaces and scoping

– Activation records

– Parameter passing