CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Symbol Table.
Advertisements

1 Compiler Construction Intermediate Code Generation.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Pascal By: Liane Tom. Outline o Background o Data types and Syntax o Procedures and Functions o Advantages o Disadvantages.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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 153: Concepts of Compiler Design September 14 Class Meeting Department of Computer Science San Jose State University Fall 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 October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chapter 1 Introduction Major Data Structures in Compiler
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 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 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
Lecture 9 Symbol Table and Attributed Grammars
CS 432: Compiler Construction Lecture 9
CS 153: Concepts of Compiler Design September 14 Class Meeting
CS 153: Concepts of Compiler Design August 29 Class Meeting
CS 326 Programming Languages, Concepts and Implementation
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
CS 432: Compiler Construction Lecture 10
CS 153: Concepts of Compiler Design September 7 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
CMPE 152: Compiler Design December 5 Class Meeting
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 6 Class Meeting
CMPE 152: Compiler Design September 18 Class Meeting
CMPE 152: Compiler Design September 13 Class Meeting
CMPE 152: Compiler Design October 2 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design September 11/13 Lab
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design August 21/23 Lab
CMPE 152: Compiler Design September 20 Class Meeting
CMPE 152: Compiler Design September 27 Class Meeting
CS 153: Concepts of Compiler Design November 6 Class Meeting
UNIT V Run Time Environments.
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design February 28 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design February 12 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
CMPE 152: Compiler Design February 28 / March 5 Lab
CMPE 152: Compiler Design February 21 Class Meeting
CMPE 152: Compiler Design February 26 Class Meeting
CMPE 152: Compiler Design February 19 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design March 5 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CMPE 152: Compiler Design March 7/12 Lab
CMPE 152: Compiler Design August 27 Class Meeting
CMPE 152: Compiler Design September 17 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design September 26 Class Meeting
CMPE 152: Compiler Design September 19 Class Meeting
Presentation transcript:

CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak 1

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 2 Executing a SELECT Parse Tree  Evaluate the first child expression subtree to get the selection value.  Examine each SELECT BRANCH subtree. Look for the selection value in the SELECT CONSTANTS list of each SELECT BRANCH. If there is a match, then execute the SELECT BRANCH’s statement subtree. CASE i+1 OF 1: j := i; 4: j := 4*i; 5, 2, 3: j := 523*i; END Is this a good way to do it?

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 3 Executing a SELECT Parse Tree, cont’d  Why is searching for a matching selection value among all the SELECT BRANCHes bad? It’s inefficient.  Selection values that appear earlier in the parse tree are found faster. The Pascal programmer shouldn’t have to consider the order of the selection values in a CASE statement.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 4 Executing a SELECT Parse Tree, cont’d  A better solution: For each SELECT tree, create a jump table implemented as a hash table.  Build the table from the SELECT parse tree.  Each jump table entry contains: Key: A constant selection value. Value: The root node of the corresponding statement subtree.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 5 Executing a SELECT Parse Tree, cont’d  During execution, the computed selection value is the key that extracts the correct statement subtree to execute.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 6 SELECT Jump Table Key: constant selection value Value: root node of the corresponding statement CASE i+1 OF 1: j := i; 4: j := 4*i; 5, 2, 3: j := 523*i; END Jump table This is an example of optimization for faster execution.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 7 Multiple CASE Statements  If a Pascal source program contains multiple CASE statements, there will be multiple SELECT parse trees.  Create a global jump cache, a hash table of jump tables.  Each jump cache entry contains: Key : The SELECT node of a SELECT parse tree. Value: The jump table created for that SELECT parse tree.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 8 Jump Cache of Jump Tables Key: SELECT node Value: Corresponding jump table Jump cache Jump tables SELECT parse trees

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 9 Class SelectExecutor  The global jump cache, which contains a jump table for each SELECT tree in the Pascal source program. // Jump cache: entry key is a SELECT node, // entry value is the jump table. // Jump table: entry key is a selection value, // entry value is the branch statement root node. private static HashMap > jumpCache = new HashMap >();

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 10 Class SelectExecutor public Object execute(ICodeNode node) { HashMap jumpTable = jumpCache.get(node); ArrayList selectChildren = node.getChildren(); ICodeNode exprNode = selectChildren.get(0); ExpressionExecutor expressionExecutor = new ExpressionExecutor(this); Object selectValue = expressionExecutor.execute(exprNode); ICodeNode statementNode = jumpTable.get(selectValue); if (statementNode != null) { StatementExecutor statementExecutor = new StatementExecutor(this); statementExecutor.execute(statementNode); } ++executionCount; // count the SELECT statement itself return null; } Get the right jump table from the jump cache. Evaluate the selection value. Get the right statement to execute. Can we eliminate the jump cache?

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 11 Simple Interpreter II  Demos java –classpath classes Pascal execute case.txt

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 12 Multipass Compilers  A compiler or an interpreter makes a “pass” each time it processes the source program. Either the original source text, or The intermediate form (parse tree)

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 13 Three-Pass Compiler  We’ve designed a 3-pass compiler or interpreter.  Pass 1: Parse the source in order to build the parse tree and symbol tables.  Pass 2: Work on the parse tree to do some optimizations. Example: Create CASE statement jump tables.  Pass 3: Walk the parse tree to generate object code (compiler) or to execute the program (interpreter).

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 14 Multipass Compilers, cont’d  Having multiple passes breaks up the work of a compiler or an interpreter into distinct steps.  Front end, intermediate tier, back end: Modularize the structure of a compiler or interpreter  Multiple passes: Modularize the work of compiling or interpreting a program.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 15 Multipass Compilers, cont’d  Back when computers had very limited amounts of memory, multiple passes were necessary.  The compiler code for each pass did its work and then it was removed from memory.  The code for the next pass was loaded into memory to do its work based on the work of the previous pass.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 16 Multipass Compilers, cont’d  Example: The FORTRAN compiler for the IBM 1401 could work in only 8K of memory and made up to 63 passes over a source program. See:

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 17 Scripting Engine  We now have a simple scripting engine! Expressions Assignment statements Control statements Compound statements Variables that are untyped

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 18 What’s Next?  Parse Pascal declarations  Type checking  Parse procedure and function declarations  Runtime memory management  Interpret entire Pascal programs.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 19 Parsing Declarations  The declarations of a programming language are often the most challenging to parse.  Declarations syntax can be difficult.  Declarations often include recursive definitions.  You must keep of track of diverse information.  Many new items to enter into the symbol table.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 20 Pascal Declarations  Classic Pascal declarations consist of 5 parts, each optional, but always in this order: 1. Label declarations 2. Constant definitions 3. Type definitions 4. Variable declarations 5. Procedure and function declarations  We will examine 2, 3, and 4 next. We’ll do procedures and functions in a couple of weeks.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 21 Pascal Declarations  The CONST, TYPE, and VAR parts are optional, but they must come in this order.  Note that constants and types are defined, but variables are declared.  Collectively, you refer to all of them as declarations.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 22 Pascal Constant Definitions  Example constant definition part: CONST factor = 8; epsilon = 1.0e-6; ch = 'x'; limit = -epsilon; message = 'Press the OK button to confirm your selection.';  Classic Pascal only allows a constant value after the = sign. No constant expressions.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 23 Pascal Type Definitions  A Pascal simple type can be: scalar ( integer, real, boolean, char ) enumeration subrange Not reserved words!

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 24 Pascal Simple Type Definitions  Examples of subrange and enumeration type definitions: CONST factor = 8; TYPE range1 = 0..factor; {subrange of integer (factor is constant)} range2 = 'a'..'q'; {subrange of char} range3 = range1; {type identifier} grades = (A, B, C, D, F); {enumeration} passing = A..D; {subrange of enumeration} week = (monday, tuesday, wednesday, thursday, friday, saturday, sunday); weekday = monday..friday; weekend = saturday..sunday;

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 25 Pascal Array Type Definitions  An array type specification has an index type and an element type.  The index type must be a simple type (subrange or enumeration).  The element type can be any type. Including another array type (multidimensional arrays). index typeelement type

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 26 Pascal Array Type Definitions  Examples of array definitions. TYPE ar1 = ARRAY [grades] OF integer; ar2 = ARRAY [(alpha, beta, gamma)] OF range2; ar3 = ARRAY [weekday] OF ar2; ar4 = ARRAY [range3] OF (foo, bar, baz); ar5 = ARRAY [range1] OF ARRAY [range2] OF ARRAY[c..e] OF enum2; ar6 = ARRAY [range1, range2, c..e] OF enum2;  A Pascal string type is an array of characters. The index type must be an integer subrange with a lower limit of 1. str = ARRAY [1..10] OF char; Type definitions ar5 and ar6 above are equivalent ways to define a multidimensional array.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 27 Pascal Record Type Definitions  A record field can be any type. Including another record type (nested records).

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 28 Pascal Record Type Definitions  Examples of record definitions : TYPE rec1 = RECORD i : integer; r : real; b1, b2 : boolean; c : char END; rec2 = RECORD ten : integer; r : rec1; a1, a2, a3 : ARRAY [range3] OF range2; END;

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 29 Pascal Variable Declarations  Variable declarations are syntactically similar to record field declarations:  Examples: VAR var1 : integer; var2, var3 : range2; var4 : ar2 var5 : rec1; direction : (north, south, east, west);  Types can be named or unnamed.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 30 Declarations and the Symbol Table  Identifiers from Pascal declarations that we will enter into a symbol table: Names of constants Names of types Names of enumeration values Names of record fields Names of variables  Information from parsing type specifications: Simple types Array types Record types

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 31 Scope and the Symbol Table Stack  Scope refers to the part of the source program where certain identifiers can be used.  Everywhere in the program where the definitions of those identifiers are in effect.  Closely related to nesting levels and the symbol table stack.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 32 Scope and the Symbol Table Stack, cont’d  Global scope Nesting level 0: At the bottom of the symbol table stack. All predefined global identifiers, such as integer, real, boolean, char.  Program scope Nesting level 1: One up from the bottom of the stack. All identifiers defined at the “top level” of a program (not in a procedure or function).

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 33 Scope and the Symbol Table Stack, cont’d  Record definitions, procedures, and functions each has a scope.  Scopes in a Pascal program are nested. An identifier can be redefined within a nested scope. Within the nested scope, the definition in the nested scope overrides the definition in an outer scope.  Each scope must have its own symbol table.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 34 Scope and the Symbol Table Stack, cont’d  As the parser parses a program from top to bottom, it enters and exits nested scopes.  Whenever the parser enters a scope, it must push that scope’s symbol table onto the symbol table stack.  Whenever the parser exits a scope, it must pop that scope’s symbol table off the stack.

Computer Science Dept. Fall 2015: September 21 CS 153: Concepts of Compiler Design © R. Mak 35 Scope and the Symbol Table Stack, cont’d  Scope example: Note that the program name Test is defined in the global scope at level 0. Global symbol table Program symbol table