CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose...
-
Upload
harold-stewart -
Category
Documents
-
view
219 -
download
0
Transcript of CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose...
CS 152: Programming Language Paradigms
April 9 Class Meeting
Department of Computer ScienceSan Jose State University
Spring 2014Instructor: Ron Mak
www.cs.sjsu.edu/~mak
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
2
Unofficial Field Trip
Computer History Museum in Mt. View http://www.computerhistory.org/
Saturday, May 3, 11:30 – closing time
Special free admission.
Do a self-guided tour of the new Revolution exhibit.
See a life-size working model of Charles Babbage’s Difference Engine in operation, a hand-cranked mechanical computer designed in the early 1800s.
Experience a fully restored IBM 1401 mainframe computer from the early 1960s in operation.
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
3
Unofficial Field Trip, cont’d
The new Revolution exhibit is now open! Walk through a timeline of the
First 2000 Years of Computing History. Historic computer systems, data processing equipment,
and other artifacts. Small theater presentations.
Atanasoff-Berry Computer
HollerithCensus
Machine
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
4
Unofficial Field Trip, cont’d
Babbage Difference Engine, fully operational Hand-cranked mechanical
computer. Computed polynomial
functions. Designed by Charles Babbage
in the early to mid 1800s. Arguably the world’s first
computer scientist, lived 1791-1871.
He wasn’t able to build it because he lost his funding.
Live demo at 1:30 His plans survived and this
working model was built. Includes a working printer!http://www.computerhistory.org/babbage/
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
5
Unofficial Field Trip, cont’d IBM 1401 computer, fully restored and operational
A small transistor-based mainframe computer. Extremely popular with small businesses in the late 1950s
through the mid 1960s Maximum of 16K bytes of memory. 800 card/minute card reader (wire brushes). 600 line/minute line printer (impact). 6 magnetic tape drives, no disk drives.
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
6
Unofficial Field Trip, cont’d
Completely optional.
Limit of 100 free passes. Otherwise, $12 admission.
My students only!
CS 149 (both sections) CS 152
If you plan to go, send me an email messagewith the subject line:
Museum trip, your name, your class
where your class is CS 149-02, CS 149-03, or CS 152
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
7
Assignment #5 Tips
What goes into the (single) symbol table? Anything that’s a symbol.
What’s a symbol? Anything that can be bound to a value at run time. Anything that returns #t when the predicate symbol?
is applied.
Enter alpha and other identifiers into the symbol table. Enter + into the symbol table. Do not enter characters, numbers, or boolean values.
(symbol? 'alpha) #t(symbol? '+) #t(symbol? #\r) #f(symbol? 132) #f(symbol? #t) #f
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
8
Assignment #5 Tips, cont’d
What are special symbols? Special symbols are the punctuation marks of Scheme. They are not symbols and therefore
should not go into the symbol table. Your scanner should recognize them as special symbols. Special symbols include:
Scheme has keywords:
Your scanner should recognize the keywords. Keywords do not go into the symbol table.
( ) [ ] { } ; , . " ' # \
and begin begin0 break-var case cond cycle define delay delay-list-cons do else extend-syntax for freeze if lambda let letrec let* macro object-maker or quote repeat safe-letrec set! stream-cons variable-case while wrap
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
9
Assignment #5 Tips, cont’d
Your scanner should recognize both integer and real numbers. Your scanner can simply call either one a “number”. Numbers do not go into the symbol table.
Printing out a list. After your parser has translated a list into intermediate code,
your back end should walk the binary tree and print the list. The printed list does not have to be “pretty”.
(define proc (lambda (a b) (let ((sum (+ a (func b)))) sum)))
(define proc (lambda (a b) (let ( (sum (+ a (func b)))) sum)))
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
10
The Symbol Table Stack
The symbol table stack is used during parsing.
Ensure that any symbol that’s used is defined. The definition can be in the local scope or an enclosing scope.
Ensure that no symbol is multiply defined within a single scope._
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
11
(define proc (lambda (a b x) (let ((sum (+ a b)) (x 3)) (- sum x))))
Nested Scopes and the Symbol Table Stack
“car” “cdr” “+”
Level 0 symbol table
Symbol table stack
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
12
Nested Scopes and the Symbol Table Stack
“car” “cdr” “+”
Level 0 symbol table
Symbol table stack
“proc”
Level 1 symbol table
(define proc (lambda (a b x) (let ((sum (+ a b)) (x 3)) (- sum x))))
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
13
Nested Scopes and the Symbol Table Stack
“car” “cdr” “+”
Level 0 symbol table
Symbol table stack
“proc”
Level 1 symbol table
“a”
Level 2 symbol table
“b”
(define proc (lambda (a b x) (let ((sum (+ a b)) (x 3)) (- sum x))))
“x”
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
14
Nested Scopes and the Symbol Table Stack
“car” “cdr” “+”
Level 0 symbol table
Symbol table stack
“proc”
Level 1 symbol table
“a”
Level 2 symbol table
“b”
“sum”
Level 3 symbol table
“x”
(define proc (lambda (a b x) (let ((sum (+ a b)) (x 3)) (- sum x))))
“x”
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
15
Nested Scopes and the Symbol Table Stack
“car” “cdr” “+”
Level 0 symbol table
Symbol table stack
“proc”
Level 1 symbol table
“a”
Level 2 symbol table
“b”
“sum”
Level 3 symbol table
“x”
“x”
(define proc (lambda (a b x) (let ((sum (+ a b)) (x 3)) (- sum x))))
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
16
Nested Scopes and the Symbol Table Stack
“car” “cdr” “+”
Level 0 symbol table
Symbol table stack
“proc”
Level 1 symbol table
“a”
Level 2 symbol table
“b” Two ways to search the
symbol table stack:1. Search only the local
symbol table (at the top of the stack) to ensure that a symbol being defined is not defined multiple times within a scope.
(define proc (lambda (a b x) (let ((sum (+ a b)) (x 3)) (- sum x))))
“x”
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
17
Nested Scopes and the Symbol Table Stack
“car” “cdr” “+”
Level 0 symbol table
Symbol table stack
“proc”
Level 1 symbol table
“a”
Level 2 symbol table
“b”
“sum”
Level 3 symbol table
Two ways to search the symbol table stack:2. Search the stack from top
to bottom to ensure that a symbol being used is defined in the local scope or in an enclosing scope.
(define proc (lambda (a b x) (let ((sum (+ a b)) (x 3)) (- sum x))))
“x”
“x”
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
18
Symbol Tables and Nesting Levels
Each symbol table should have its nesting level as one of its fields.
Each symbol table entry should contain a reference to the symbol table that it’s contained in. child parent link
Therefore, if you have a reference to a symbol table entry, you can determine the nesting level of that entry._
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
19
When Parsing is Done
At the conclusion of parsing:
Each defined symbol will have an entry in the level 1 symbol table.
The symbol table entry for each defined symbol will point to the lambda expression’s parse tree._
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
20
(define proc (lambda (a b) (let ((sum (+ a (func b)))) sum)))
(define func (lambda (a) (let* ((b 2) (prod (* a b))) prod)))
Parse Trees and Symbol Tables
Top-LevelSymbolTable
(define x 2)(define y 3)(proc x y)
x y
The parse treesand the
symbol tables are interlinked.
proc
lambda
ba
proc
define
sum
bfunc
a+
let
sum
a
b
Level 2
sum
Level 3
lambda
a
func
define
let*
prod
2b
prod
ba
*
b
prod
Level 3
a
Level 2
func
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
21
Executing Scheme Programs
Now the executor in the interpreter’s back end can execute the program.
Use only the symbol tables and the parse trees. The executor never sees the original Scheme program.
_
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
22
Executing Scheme Programs, cont’d
Each time a procedure is called, the procedure’s parse tree can be obtained from the symbol table entry of the procedure’s name.
You must write Java code to perform:
List operations such as car, cdr, cons, list Predicates such as null? and equality tests such as = Basic operations such as +, -, *, and, or
_
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
23
Executing Scheme Programs, cont’d
Where can we store values computed at run time to be bound to variables?
Why not in the symbol tables?
During a recursive call of a procedure, we don’t want values computed during an earlier incomplete call to be overwritten._
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
24
The Runtime Stack
The interpreter must manage the memory that the source program uses during run time.
English grammar alert! Noun: run time
Example: The program executes during run time. Adjective: runtime (or run-time)
Example: Division by zero is a runtime error.
Values computed during run time are kept on the runtime stack.
run time: The time when the interpreter is executing the Scheme program.
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
25
Runtime Activation Records
An activation record maintains information about the currently executing procedure.
In particular, it contains the procedure’s local memory. Values of local variables. Values of parameters.
This local memory is a memory map. Key: The name of the local variable or formal parameter. Value: The current value of the variable or parameter. In other words, local memory is a hash table!
_
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
26
Runtime Activation Records, cont’d
Each time a procedure is called, push its activation record onto the runtime stack.
`
Pop off the activation record when the procedure returns.
SJSU Dept. of Computer ScienceSpring 2014: April 9
CS 152: Programming Language Paradigms© R. Mak
27
Runtime Activation Records, cont’d
RUNTIME STACK
AR: mainx
AR: lambdaa
2
2
(define proc (lambda (a b) (let ((sum (+ a b))) sum)))
(define x 2)(define y 3)(proc x y)
y 3
b 3
AR: letsum 5
Push main’s activation record. Push lambda’s activation record when proc is called.
Pop off when returning from proc.
Push let’s activation record when it is executed. Pop off when leaving the let.