CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Advertisements

Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 185C: The History of Computing August 24 Class Meeting Department of Computer Science San Jose State University Fall 2011 Instructor: Ron Mak
CS 151: Object-Oriented Design December 5 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 157B: Database Management Systems II March 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 235: User Interface Design November 3 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CS 153: Concepts of Compiler Design September 9 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 149: Operating Systems April 7 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CMPE 226 Database Systems November 18 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design October 29 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 235: User Interface Design April 28 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 16 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 174: Web Programming November 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 154 Formal Languages and Computability April 12 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 154 Formal Languages and Computability April 19 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 160 and CMPE/SE 131 Software Engineering May 12 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
CS 154 Formal Languages and Computability May 12 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 14 Class Meeting
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
CS 153: Concepts of Compiler Design November 28 Class Meeting
CMPE Database Systems Exercise #1 Solutions
CMPE 152: Compiler Design February 6 Class Meeting
CMPE 152: Compiler Design September 25 Class Meeting
CMPE 152: Compiler Design September 4 Class Meeting
CMPE 152: Compiler Design September 18 Class Meeting
CMPE 152: Compiler Design October 2 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
The Metacircular Evaluator
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design August 21/23 Lab
CMPE 135: Object-Oriented Analysis and Design December 6 Class Meeting
CMPE 152: Compiler Design December 6 Class Meeting
CMPE 152: Compiler Design November 29 Class Meeting
CMPE 135: Object-Oriented Analysis and Design November 29 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
CS 153: Concepts of Compiler Design November 6 Class Meeting
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design February 28 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
6.001 SICP Interpretation Parts of an interpreter
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design February 21 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CS 144 Advanced C++ Programming April 25 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CMPE 152: Compiler Design May 2 Class Meeting
CMPE 152: Compiler Design September 17 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
Presentation transcript:

CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer Science Spring 2014: April 9 CS 152: Programming Language Paradigms © R. Mak 2 Unofficial Field Trip  Computer History Museum in Mt. View  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 Science Spring 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 Hollerith Census Machine

SJSU Dept. of Computer Science Spring 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 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!

SJSU Dept. of Computer Science Spring 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 Science Spring 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 message with the subject line: Museum trip, your name, your class where your class is CS , CS , or CS 152

SJSU Dept. of Computer Science Spring 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 Science Spring 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 Science Spring 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))) PRINT

SJSU Dept. of Computer Science Spring 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 Science Spring 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 Science Spring 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 Science Spring 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 Science Spring 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 Science Spring 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” (define proc (lambda (a b x) (let ((sum (+ a b)) (x 3)) (- sum x) )))

SJSU Dept. of Computer Science Spring 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 Science Spring 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”

SJSU Dept. of Computer Science Spring 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 Science Spring 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 Science Spring 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-Level Symbol Table (define x 2) (define y 3) (proc x y) xy The parse trees and the symbol tables are interlinked. proc lambda b a proc define sum b func a + let sum a b Level 2 sum Level 3 lambda a func define let* prod 2 b b a * b Level 3 a Level 2 func

SJSU Dept. of Computer Science Spring 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 Science Spring 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 Science Spring 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 Science Spring 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 Science Spring 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 Science Spring 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 Science Spring 2014: April 9 CS 152: Programming Language Paradigms © R. Mak 27 Runtime Activation Records, cont’d RUNTIME STACK AR: main x AR: lambda a 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: let sum 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.