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

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Symbol Table.
Semantics Static semantics Dynamic semantics attribute grammars
Intermediate Code Generation
1 Compiler Construction Intermediate Code Generation.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Environments and Evaluation
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
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
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
Compiler Construction
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 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
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Run-Time Environments How do we allocate the space for the generated target code and the data object.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
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
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 23 Class Meeting Department of Computer Science 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 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
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
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
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Lecture 9 Symbol Table and Attributed Grammars
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 September 7 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
CS 153: Concepts of Compiler Design December 5 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
CS 3304 Comparative Languages
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 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
Run Time Environments 薛智文
CMPE 152: Compiler Design February 21 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 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 19 Class Meeting
Presentation transcript:

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

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 2 PROGRAM Test; VAR i, j, k, n : integer; PROCEDURE p(j : real); VAR k : char; FUNCTION f(x : real) : real; VAR i:real; BEGIN {f} f := i + j + n + x; END {f}; BEGIN {p} k := chr(i + trunc(f(n))); END {p}; BEGIN {test} p(j + k + n) END {test}. Nested Scopes and the Symbol Table Stack

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 3 PROGRAM Test; VAR i, j, k, n : integer; PROCEDURE p(j : real); VAR k : char; FUNCTION f(x : real) : real; VAR i:real; BEGIN {f} f := i + j + n + x; END {f}; BEGIN {p} k := chr(i + trunc(f(n))); END {p}; BEGIN {test} p(j + k + n) END {test}. Nested Scopes and the Symbol Table Stack

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 4 PROGRAM Test; VAR i, j, k, n : integer; PROCEDURE p(j : real); VAR k : char; FUNCTION f(x : real) : real; VAR i:real; BEGIN {f} f := i + j + n + x; END {f}; BEGIN {p} k := chr(i + trunc(f(n))); END {p}; BEGIN {test} p(j + k + n) END {test}. Nested Scopes and the Symbol Table Stack

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 5 PROGRAM Test; VAR i, j, k, n : integer; PROCEDURE p(j : real); VAR k : char; FUNCTION f(x : real) : real; VAR i:real; BEGIN {f} f := i + j + n + x; END {f}; BEGIN {p} k := chr(i + trunc(f(n))); END {p}; BEGIN {test} p(j + k + n) END {test}. Nested Scopes and the Symbol Table Stack

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 6 PROGRAM Test; VAR i, j, k, n : integer; PROCEDURE p(j : real); VAR k : char; FUNCTION f(x : real) : real; VAR i:real; BEGIN {f} f := i + j + n + x; END {f}; BEGIN {p} k := chr(i + trunc(f(n))); END {p}; BEGIN {test} p(j + k + n) END {test}. Nested Scopes and the Symbol Table Stack

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 7 PROGRAM Test; VAR i, j, k, n : integer; PROCEDURE p(j : real); VAR k : char; FUNCTION f(x : real) : real; VAR i:real; BEGIN {f} f := i + j + n + x; END {f}; BEGIN {p} k := chr(i + trunc(f(n))); END {p}; BEGIN {test} p(j + k + n) END {test}. Nested Scopes and the Symbol Table Stack

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 8 PROGRAM Test; VAR i, j, k, n : integer; PROCEDURE p(j : real); VAR k : char; FUNCTION f(x : real) : real; VAR i:real; BEGIN {f} f := i + j + n + x; END {f}; BEGIN {p} k := chr(i + trunc(f(n))); END {p}; BEGIN {test} p(j + k + n) END {test}. Nested Scopes and the Symbol Table Stack

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 9 PROGRAM Test; VAR i, j, k, n : integer; PROCEDURE p(j : real); VAR k : char; FUNCTION f(x : real) : real; VAR i:real; BEGIN {f} f := i + j + n + x; END {f}; BEGIN {p} k := chr(i + trunc(f(n))); END {p}; BEGIN {test} p(j + k + n) END {test}. Nested Scopes and the Symbol Table Stack Each routine’s name is defined in the parent’s scope. Each routine’s local names are defined in that routine’s scope.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 10 Parsing Programs, Procedures, and Functions  Classes ProgramParser and DeclaredRoutineParser are subclasses of DeclarationsParser.  DeclaredRoutineParser depends on VariableDeclarationsParser to parse the formal parameter list.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 11 DeclarationsParser.parse()  Modify the method to look for the PROCEDURE and FUNCTION reserved words.  Call DeclaredRoutineParser.parse() to parse the procedure or function header.  Note: ProgramParser.parse() also calls DeclaredRoutineParser.parse() to parse the program header.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 12 Class DeclaredRoutineParser  Parse any declared (programmer-written) routine: a procedure a function the program itself

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 13 DeclaredRoutineParser Methods  parse() First call parseRoutineName() Enter the routine name into the enclosing scope’s symbol table. Push a new symbol table for the routine onto the symbol table stack. Pop off the routine’s symbol table when done parsing the routine.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 14 DeclaredRoutineParser Methods, cont’d  parseRoutineName()  parseHeader()  parseFormalParameters()  parseParmSublist() Call VariableDeclarationsParser.parseIdentifierSublist() Enter each formal parameter into the routine’s symbol table defined either as a VALUE_PARM or a VAR_PARM.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 15 Procedure and Function Calls  A procedure call is a statement.  A function call is a factor.  If there are no actual parameters, there are also no parentheses.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 16 Class Predefined  Load the global symbol table with the predefined identifiers. integer, real, char, boolean, true, false.  Now it must also load the global symbol table with the identifiers of the predefined procedures and functions. write, writeln, read, readln abs, arctan, chr, cos, eof, eoln, exp, ln, odd, ord, pred, round, sin, sqr, sqrt, succ, trunc

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 17 Classes for Parsing Calls  A new statement parser subclass: CallParser.  CallParser has two subclasses, CallDeclaredParser and CallStandardParser. CallStandardParser parses calls to the standard (predefined) Pascal routines.  Each parse() method returns the root of a parse subtree. _

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 18 Class CallParser  CallParser.parse() calls either: CallDeclaredParser.parse() CallStandardParser.parse()  Protected method parseActualParameters() is used by both subclasses.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 19 Formal Parameters and Actual Parameters  Formal parameters: In a procedure or function declaration:  Actual parameters: In a procedure or function call:

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 20 Formal and Actual, cont’d  In class CallParser, method parseActualParameters() calls checkActualParameter() to check each actual parameter against the corresponding formal parameter.  There must be a one-to-one correspondence.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 21 Formal and Actual, cont’d  Value parameter (pass a copy) The actual parameter can be any expression. The value of the actual parameter must be assignment-compatible with the corresponding formal parameter.  VAR parameter (pass a reference) The actual parameter must be a variable  Can have subscripts and fields The actual parameter must have the same type as the type of the corresponding formal parameter.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 22 Formal Parameters and Actual Parameters  Example declarations: TYPE arr = ARRAY [1..5] OF real; VAR i, m : integer; a : arr; v, y : real; t : boolean; PROCEDURE proc( j, k : integer; VAR x, y, z : real; VAR v : arr; VAR p : boolean; ch : char ); BEGIN... END;  Example call: proc( i, -7+m, a[m], v, y, a, t, 'r' )

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 23 CallParser.parseActualParameters()  Example call to procedure proc : proc(i, -7 + m, a[m], v, y, a, t, 'r') generates the parse tree: The CALL node can have a PARAMETERS node child. The PARAMETERS node has a child node for each actual parameter.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 24 Parsing Calls to the Standard Routines  Class CallStandardParser parses calls to the standard procedures and functions.  These calls are handled as special cases.  Method parse() calls private ad hoc parsing methods.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 25 Parsing Calls to the Standard Routines  Example: method parseAbsSqr() One integer or one real actual parameter. The return type is the same as the parameter type.  Each actual argument to standard procedure write or writeln can be followed by specifiers for the field width and the number of decimal places after the point. Example: writeln(str:10, k:12, x:20:5) _

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 26 Pascal Syntax Checker IV  Now we can parse entire Pascal programs! Demo Onward to interpreting Pascal programs.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 27 Review: The Front End Parser  Now it can parse an entire Pascal program.  For the main program and each procedure and function: Create a symbol table for the local identifiers. Create a parse tree for the compound statement. The symbol tables and parse trees live in the intermediate tier. The symbol table entry for a program, procedure, or function name points to the corresponding symbol table and parse tree.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 28 Review: The Front End Parser, cont’d  The front end creates only correct symbol tables and parse trees.  The structure of the parse tree encodes the operator precedence rules.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 29 Review: The Back End Executor  Up until now, it can only execute a main program. Using only the main program’s symbol table and parse tree.  It assumes the symbol table and parse tree were correctly built by the front end. No syntax or type errors. The parse tree structure accurately represents the operator precedence rules.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 30 Review: The Back End Executor, cont’d  The back end executor executes the program by “walking” the parse tree and accessing the symbol table.  The executor does not need to know what the source program looks like or even what language it was written in.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 31 Review: Temporary Hacks  Store values of variables calculated at run time into the variable’s symbol table entry. Why won’t this always work?  Print out debugging information each time we assign a value to a variable. We don’t know how to execute a write or writeln call yet.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 32 Temporary Hacks Removed!  A variable is “declared” the first time it appears as the target of an assignment statement. We now can parse variable declarations.  A variable can hold a value of any type. The parser can check for assignment compatibility.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 33 Back to the Interpreter Back End Execute entire Pascal programs!

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 34 The Interpreter  The back end executes the source program.  Uses the symbol tables and the parse trees built by the front end parser.  One symbol table per main program, procedure, and function for that routine’s local identifiers.  The symbol table is pointed to by the symbol table entry for the routine’s name.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 35 The Interpreter, cont’d  One parse tree per main program, procedure, and function.  For that routine’s compound statement. Pointed to by the symbol table entry for the routine’s name.  The interpreter never sees the original source program. Only the symbol tables and parse trees.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 36 Compile Time vs. Run Time  Compile time: The time during which the front end is parsing the source program and building the symbol tables and the parse trees. It’s called “compile time” whether the back end is a compiler or an interpreter. AKA translation time

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 37 Compile Time vs. Run Time  Run time: The time during which the back end interpreter is executing the source program. 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.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 38 Runtime Memory Management  The interpreter must manage the memory that the source program uses during run time.  Up until now, we’ve used the hack of storing values computed during run time into the symbol table.  Why is this a bad idea? This will fail miserably if the source program has recursive procedure and function calls.

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 39 Symbol Table Stack vs. Runtime Stack  The front end parser builds symbol tables and manages the symbol table stack as it parses the source program. The parser pushes and pops symbol tables as it enters and exits nested scopes.  The back end executor manages the runtime stack as it executes the source program. The executor pushes and pops activation records as it calls and returns from procedures and functions. These are similar concepts, so don’t confuse them!

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 40 Runtime Activation Records  An activation record (AKA stack frame) maintains information about the currently executing routine a procedure a function the main program itself

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 41 Runtime Activation Records  In particular, an activation record contains the routine’s local memory. values of local variables values of formal 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. Local memory is a hash table!

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 42 Runtime Activation Records, cont’d PROGRAM main1; PROCEDURE proc2a; PROCEDURE proc3; BEGIN... END; BEGIN {proc2a} proc3; END; PROCEDURE proc2b; BEGIN proc2a; END; BEGIN {main1} proc2b; END. AR: main1 AR: proc2b AR: proc2a AR: proc3 RUNTIME STACK In this example, the names of the routines indicate their nesting levels. Call a routine: Push its activation record onto the runtime stack. Return from a routine: Pop off its activation record. main1  proc2b  proc2a  proc3

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 43 PROGRAM main1; VAR j : integer; PROCEDURE proc2b; VAR j : integer; BEGIN j := 14; END; BEGIN {main1} j := 55; proc2b; END. Runtime Access to Local Variables RUNTIME STACK AR: main1 j AR: proc2b j Accessing local values is simple, because the currently executing routine’s activation record is on top of the runtime stack main1  proc2b

Computer Science Dept. Fall 2015: October 5 CS 153: Concepts of Compiler Design © R. Mak 44 PROGRAM main1; VAR i, j : integer; PROCEDURE proc2a; VAR m : integer; PROCEDURE proc3; VAR j : integer BEGIN j := i + m; END; BEGIN {proc2a} i := 11; m := j; proc3; END; PROCEDURE proc2b; VAR j, m : integer; BEGIN j := 14; m := 5; proc2a; END; BEGIN {main1} i := 33; j := 55; proc2b; END. Runtime Access to Nonlocal Variables RUNTIME STACK AR: main1 i j AR: proc2a m AR: proc3 j AR: proc2b j m  Each parse tree node for a variable contains the variable’s symbol table entry as its VALUE attribute. Each symbol table entry has the variable’s nesting level n.  To access the value of a variable at nesting level n, the value must come from the topmost activation record at level n. Search the runtime stack from top to bottom for the topmost activation record at level n. main1  proc2b  proc2a  proc3 145