CS 152 Programming Language Paradigms April 7 Class
- Slides: 26
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www. cs. sjsu. edu/~mak
Conceptual Design (Version 3) o A compiler and an interpreter can both use the same front end and intermediate tier. SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 2
Scheme Intermediate Code o Scheme programs have a simple structure. n o Everything is a list. The Scheme parser can translate a list into a binary tree. n Example: (1 2 3) n The left subtree is the car of the list. The right subtree is the cdr of the list. n n 1 2 3 Each leaf node contains an element of the list. _ SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 3
Scheme Intermediate Code, cont’d o Example: ((a b) c (d)) a c b d o Do a preorder walk of the tree to recreate the list: n Visit the root. o n Visit the left subtree. o n If the left subtree is not an element node, open a set of parentheses. If the left subtree is a leaf, print its element. Visit the right subtree. SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 4
How to Build the Intermediate Code o Write a list parser method that can read a list and return the root of the binary parse tree. n As the scanner returns elements, the parse tree “adopts” them. n Example: (1 2 3) The parse tree adopts the elements 1, 2, and 3. Return this root. 1 2 3 o If a list element is a nested sublist, recursively call the list parser to build a subtree. n The main tree adopts the root of the subtree. _ SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 5
How to Build the Intermediate Code, cont’d n Example: (1 2 3) 1 2 3 n Example: (a (1 2 3) b) o Recursively call the list parser. a 1 b 2 3 SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 6
Top-Down Recursive-Descent Parser o Start with the topmost rule. n o The syntax diagram helps you write the parsing method. Recursively work down to the lower level rules. n o In our case, the rule that defines a list. Rules that define list elements. One token lookahead. n Example: If the scanner fetches a ( token, the parser knows that it will parse a list next. SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 7
The Symbol Table: Basic Concepts o Purpose n o To store information about certain tokens during the translation process (i. e. , parsing and scanning). What information to store? n n Anything that’s useful! For a symbol: o o o its name how it’s defined (as a variable, procedure name, etc. ) Basic operations n n n Enter new information Look up existing information Update existing information SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 8
The Symbol Table: Implementation o Each symbol table entry includes the name of a symbol and its attributes. n To maintain maximum flexibility, we can implement the attributes as a hash table. SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 9
The Symbol Table: Implementation, cont’d o The symbol table itself can be a hash table. n n Key: The symbol name. Value: The symbol table entry for the symbol. SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 10
The Symbol Table Stack o o Scheme constructs (and their scopes) can be nested. n A lambda expression can be nested inside another lambda expression. n A let special form can be nested inside another let special form. Therefore, symbol tables need to be kept on a symbol table stack. n n The stack handles nested scopes. You can implement the stack as an array list. _ SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 11
The Symbol Table Stack, cont’d o Whichever symbol table is on top of the stack is the local symbol table. o The first symbol table created (the one at the bottom of the stack) is the global symbol table. n o Global symbol table During the translation process, symbol tables are pushed onto and popped off the stack … n SJSU Dept. of Computer Science Spring 2014: April 7 It stores the predefined symbols, such as entries for the names of the standard Scheme procedures car, cdr, cons, +, -, etc. … as the parser enters and exits scopes. _ CS 152: Programming Language Paradigms © R. Mak 12
The Global Symbol Table o Before starting to parse, initialize the global symbol table by populating it with Scheme’s predefined symbols, such as car, cdr, cons, +, -, *, etc. o Push the global symbol table onto the symbol table stack, so that it’s at the bottom of the stack. o The global symbol table has nesting level 0. Global symbol table SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 13
The Top-Level Symbol Table o After initializing the global symbol table, push a new empty symbol table onto the stack. n n o This is the symbol table for all the top-level names in your program. n n o It goes on top of the global symbol table. It has nesting level 1. Top-level names are not nested. This table should contain all the names that are each declared by the define special form. As you parse each define special form, enter the symbol (name) into the top-level symbol table. n One of the attributes of a symbol table entry should be a reference to the symbol table that it’s contained in. SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 14
Scope and the Symbol Table Stack o Scheme special forms lambda, letrec, let*, etc. each creates a new scope. o Each scope must have its own symbol table. n o Whenever your parser enters a new scope (i. e. , it begins to parse one of the special forms listed above), create a symbol table for the scope and push it onto the symbol table stack. n o Each symbol table has a nesting level number according to how deeply nested the scope is. Enter symbols declared by the scope into its symbol table. When the parser is done parsing the scope, pop off the symbol table SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 15
Scope and the Symbol Table Stack, cont’d (define proc (lambda (a b) (let ((sum (+ a b))) sum) )) Symbol table stack Level 0 symbol table “car” SJSU Dept. of Computer Science Spring 2014: April 7 “cdr” CS 152: Programming Language Paradigms © R. Mak “+” 16
Scope and the Symbol Table Stack, cont’d (define proc (lambda (a b) (let ((sum (+ a b))) sum) )) Symbol table stack Level 1 symbol table “proc” Level 0 symbol table “car” SJSU Dept. of Computer Science Spring 2014: April 7 “cdr” CS 152: Programming Language Paradigms © R. Mak “+” 17
Scope and the Symbol Table Stack, cont’d (define proc (lambda (a b) (let ((sum (+ a b))) sum) )) Symbol table stack Level 2 symbol table “a” “b” Level 1 symbol table “proc” Level 0 symbol table “car” SJSU Dept. of Computer Science Spring 2014: April 7 “cdr” CS 152: Programming Language Paradigms © R. Mak “+” 18
Scope and the Symbol Table Stack, cont’d (define proc (lambda (a b) (let ((sum (+ a b))) sum) )) Symbol table stack Level 3 symbol table “sum” Level 2 symbol table “a” “b” Level 1 symbol table “proc” Level 0 symbol table “car” SJSU Dept. of Computer Science Spring 2014: April 7 “cdr” CS 152: Programming Language Paradigms © R. Mak “+” 19
Scope and the Symbol Table Stack, cont’d (define proc (lambda (a b) (let ((sum (+ a b))) sum) )) Symbol table stack Level 3 symbol table “sum” Level 2 symbol table “a” “b” Level 1 symbol table “proc” Level 0 symbol table “car” SJSU Dept. of Computer Science Spring 2014: April 7 “cdr” CS 152: Programming Language Paradigms © R. Mak “+” 20
Scope and the Symbol Table Stack, cont’d (define proc (lambda (a b) (let ((sum (+ a b))) sum) )) o Two ways to search the symbol table stack: 1. Search only the local symbol table (at the top of the stack) to make sure that a symbol isn’t being defined multiple times within a scope. o Example: (lambda (a b). . . SJSU Dept. of Computer Science Spring 2014: April 7 Symbol table stack Level 2 symbol table “a” “b” Level 1 symbol table “proc” Level 0 symbol table “car” “cdr” CS 152: Programming Language Paradigms © R. Mak “+” 21
Scope and the Symbol Table Stack, cont’d (define proc (lambda (a b) (let ((sum (+ a b))) sum) )) o Two ways to search the symbol table stack: 2. Search the stack from top to bottom to look for a symbol defined in an outer scope. o Example: (+ a b) _ Symbol table stack Level 3 symbol table “sum” Level 2 symbol table “a” Level 1 symbol table “proc” Level 0 symbol table “car” SJSU Dept. of Computer Science Spring 2014: April 7 “b” “cdr” CS 152: Programming Language Paradigms © R. Mak “+” 22
Scope and the Symbol Table Stack, cont’d (define proc (lambda (a b) (let ((sum (+ a b))) sum) )) o o After the define has been parsed, make the entry for “proc” point to the parse tree of the lambda expression. Ready to parse the next define special form! SJSU Dept. of Computer Science Spring 2014: April 7 Symbol table stack Level 1 symbol table “proc” Level 0 symbol table “car” “cdr” CS 152: Programming Language Paradigms © R. Mak “+” 23
Scope and the Symbol Table Stack, cont’d o o After popping a symbol table off the stack, you must still keep a reference to that symbol table. n In your parse tree, each lambda node will have a reference to its symbol table. n Similarly for each let, letrec, let*, etc. node. The symbol table entry for a defined symbol will point to the root node of the value’s parse tree. n Example: If a procedure is being defined, then the symbol table entry for the procedure name will point to the root node of the lambda parse tree. _ SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 24
When Parsing is Done o At the conclusion of parsing: n Each defined symbol will have an entry in the level 1 symbol table. n The symbol table entry for each procedure name will point to the lambda expression’s parse tree. _ SJSU Dept. of Computer Science Spring 2014: April 7 CS 152: Programming Language Paradigms © R. Mak 25
Parse Trees and Symbol Tables (define x 2) (define y 3) (proc x y) (define proc (lambda (a b) (let ((sum (+ a (func b)))) sum))) Top-Level Symbol Table define Level 2 proc a define proc lambda b (define func (lambda (a) (let* ((b 2) (prod (* a b))) prod))) a func x b Level 2 func a lambda Level 3 a y b let* let prod sum Level 3 sum + a func SJSU Dept. of Computer Science Spring 2014: April 7 b The parse trees and the symbol tables are interlinked. b 2 CS 152: Programming Language Paradigms © R. Mak prod * a b 26
- R programming language paradigms
- Binding in programming paradigms
- Ktu programming paradigms notes
- Hdl language
- Unr152
- Ntuser.dat forensics
- Przedszkole 152 łódź
- Law society of tasmania v richardson [2003] tassc 9
- Rounding to the nearest hundred thousand
- Mae 152
- Blending function in computer graphics
- Cs 152 stanford
- Cs 152 berkeley
- Ba 152
- Ece 152
- Ba 152
- Econ 152
- What ports are defined above 49 152
- Ba 152
- Ba 152
- Organizational atrophy
- Hasil dari 202-152 adalah
- Econ 152
- Ron mak sjsu
- Gfi 152
- Hall policy paradigms
- Paradigms of cognitive psychology