Download Power Point Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Ada (programming language) wikipedia , lookup

ALGOL 68 wikipedia , lookup

Join-pattern wikipedia , lookup

Subroutine wikipedia , lookup

Program optimization wikipedia , lookup

String literal wikipedia , lookup

Falcon (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Corecursion wikipedia , lookup

Standard ML wikipedia , lookup

Control flow wikipedia , lookup

Interpreter (computing) wikipedia , lookup

Functional programming wikipedia , lookup

Object-oriented programming wikipedia , lookup

Abstraction (computer science) wikipedia , lookup

C syntax wikipedia , lookup

C++ wikipedia , lookup

Reactive programming wikipedia , lookup

Programming language wikipedia , lookup

Parsing wikipedia , lookup

Go (programming language) wikipedia , lookup

Compiler wikipedia , lookup

Structured programming wikipedia , lookup

Name mangling wikipedia , lookup

Cross compiler wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

Transcript
Cse321, Programming Languages and Compilers
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
5/24/2017
1
Cse321, Programming Languages and Compilers
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.
5/24/2017
2
Cse321, Programming Languages and Compilers
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 subexpressions
• Often we think of this meaning being an attribute of each
syntax node.
• Attribute grammars provide a natural way of describing
this.
5/24/2017
3
Cse321, Programming Languages and Compilers
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/24/2017
4
Cse321, Programming Languages and Compilers
Example 1 (cont)
30
+
5
5
6
*
3
3
2
2
fun mean x =
case x of
Plus(x,y) => (mean x) + (mean y)
| Times(x,y) => (mean x) * (mean y)
| Num n => n
5/24/2017
5
Cse321, Programming Languages and Compilers
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
5/24/2017
6
Cse321, Programming Languages and Compilers
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
5/24/2017
7
Cse321, Programming Languages and Compilers
Example 2 (cont)
(“T5”,
“T1 = 5
T2 = 3
T3 = 2
T4 = T2 * T3
T5 = T1 + T4”)
(“T4”,
“T2 = 3
T3 = 2
T4 = T2 * T3” )
+
*
(“T1”,
“T1 = 5”)
5
(“T2”,
“T2 = 3”)
5/24/2017
3
(“T3”,
“T3 = 2”)
2
8
Cse321, Programming Languages and Compilers
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.
5/24/2017
9
Cse321, Programming Languages and Compilers
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)
5/24/2017
10
Cse321, Programming Languages and Compilers
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.
5/24/2017
11
Cse321, Programming Languages and Compilers
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)]
5/24/2017
X := Y + (Z := 4)
[(Y,2),(Z,4),(X,6)]
12
Cse321, Programming Languages and Compilers
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;
5/24/2017
13
Cse321, Programming Languages and Compilers
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.
5/24/2017
14
Cse321, Programming Languages and Compilers
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.
5/24/2017
15
Cse321, Programming Languages and Compilers
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
–
5/24/2017
16
Cse321, Programming Languages and Compilers
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!
5/24/2017
17
Cse321, Programming Languages and Compilers
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?
5/24/2017
18
Cse321, Programming Languages and Compilers
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
5/24/2017
19
Cse321, Programming Languages and Compilers
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
–
–
–
–
5/24/2017
The current object
Judgments about classes and hierarchies
The role of subtyping
Tables for inheritance hierarchies
20
Cse321, Programming Languages and Compilers
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
–
–
–
–
5/24/2017
sets of items construction
using ambiguity
Resolving shift reduce errors
using ml-yacc
21
Cse321, Programming Languages and Compilers
Possible Exam Topics 3
• Procedural Abstraction
– Name spaces and scoping
– Activation records
– Parameter passing
5/24/2017
22