CS 432: Compiler Construction Lecture 11

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

1 Compiler Construction Intermediate Code Generation.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Block-Structured Procedural Languages Lecture 11: Dolores Zage.
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
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
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 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
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 September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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 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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lecture 9 Symbol Table and Attributed Grammars
Functions + Overloading + Scope
CS 153: Concepts of Compiler Design September 14 Class Meeting
Chapter 7: User-Defined Functions II
Constructing Precedence Table
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
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
Principles of programming languages 4: Parameter passing, Scope rules
CS 153: Concepts of Compiler Design September 7 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
User-Defined Functions
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 432: Compiler Construction Lecture 7
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
Chapter 6 Intermediate-Code Generation
CMPE 152: Compiler Design October 4 Class Meeting
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
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
COMPILERS Semantic Analysis
CMPE 152: Compiler Design February 21 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
Corresponds with Chapter 5
CMPE 152: Compiler Design September 17 Class Meeting
CMPE 152: Compiler Design September 19 Class Meeting
Presentation transcript:

CS 432: Compiler Construction Lecture 11 Department of Computer Science Salisbury University Fall 2017 Instructor: Dr. Sophie Wang http://faculty.salisbury.edu/~xswang 4/4/2019

Pascal Program Header The program parameters are optional. Examples: Identifiers of input and output file variables. Default files are standard input and standard output. Examples: PROGRAM newton; PROGRAM hilbert(input, output, error); 4/4/2019

Pascal Programs, Procedures, and Functions Procedure and function declarations come last. Any number of procedures and functions, and in any order. A formal parameter list is optional. 4/4/2019

Formal Parameter List By default, parameters are passed by value. The actual parameter value in the call is copied and the formal parameter is assigned the copied value. The routine cannot change the actual parameter value. VAR parameters are passed by reference. The formal parameter is assigned a reference to the actual parameter value. The routine can change the actual parameter value. 4/4/2019

Example Procedure and Function Declarations PROCEDURE proc (j, k : integer; VAR x, y, z : real; VAR v : arr; VAR p : boolean; ch : char); BEGIN ... END; PROCEDURE SortWords; FUNCTION func (VAR x : real; i, n : integer) : real; func := ...; Value and VAR parameters. No parameters. Function return type. Assign the function return value. 4/4/2019

Forward Declarations In Pascal, you cannot have a statement that calls a procedure or a function before it has been declared. To get around this restriction, use forward declarations. Example: FUNCTION foo(m : integer; VAR t : real) : real; forward; Instead of a block, you have the word “forward”. “Forward” is not a reserved word. _ 4/4/2019

Forward Declarations, cont'd When you finally have the full declaration of a forwarded procedure or function, you do not repeat the formal parameters or the function return type. FUNCTION foo(m : integer; VAR t : real) : real; forward; PROCEDURE proc; VAR x, y : real; BEGIN x := foo(12, y); END; FUNCTION foo; ... foo := ...; Use the function before its full declaration. Now the full function declaration. 4/4/2019

Records and the Symbol Table Stack 4/4/2019

Nested Scopes and the Symbol Table Stack 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}. 4/4/2019

Nested Scopes and the Symbol Table Stack 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}. 4/4/2019

Nested Scopes and the Symbol Table Stack 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}. 4/4/2019

Nested Scopes and the Symbol Table Stack 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}. 4/4/2019

Nested Scopes and the Symbol Table Stack 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}. 4/4/2019

Nested Scopes and the Symbol Table Stack 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}. 4/4/2019

Nested Scopes and the Symbol Table Stack 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}. 4/4/2019

Nested Scopes and the Symbol Table Stack 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}. Each routine’s name is defined in the parent’s scope. Each routine’s local names are defined in the routine’s scope. 4/4/2019

Parsing Programs, Procedures, and Functions Classes ProgramParser and DeclaredRoutineParser are subclasses of DeclarationsParser. DeclaredRoutineParser depends on VariableDeclarationsParser to parse the formal parameter list. _ 4/4/2019

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. _ 4/4/2019

Class DeclaredRoutineParser Parses any declared (programmer-written) routine A procedure, a function, or the program itself. Methods parse() First call parseRoutineName() Push a new symbol table for the routine onto the stack. The routine name is entered into the enclosing scope’s symbol table. Pop off the routine’s symbol table when done parsing the routine. 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. 4/4/2019

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. _ 4/4/2019

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 _ 4/4/2019

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. _ 4/4/2019

Class CallParser CallParser.parse() calls either: CallDeclaredParser.parse() CallStandardParser.parse() Protected method parseActualParameters() is used by both subclasses. _ 4/4/2019

Formal Parameters and Actual Parameters Formal parameters: In a procedure or function declaration: Actual parameters: In a procedure or function call: 4/4/2019

Formal Parameters and Actual Parameters, 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. 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. 4/4/2019

Formal Parameters and Actual Parameters, cont'd 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' ) Example call: proc( i, -7+m, a[m], v, y, a, t, 'r' ) 4/4/2019

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. <CALL id="proc" level="1" line="81"> <PARAMETERS> <VARIABLE id="i" level="1" type_id="integer" /> <ADD type_id="integer"> <NEGATE> <INTEGER_CONSTANT value="7" type_id="integer" /> </NEGATE> <VARIABLE id="m" level="1" type_id="integer" /> </ADD> <VARIABLE id="a" level="1" type_id="real"> <SUBSCRIPTS type_id="real"> </SUBSCRIPTS> </VARIABLE> <VARIABLE id="v" level="1" type_id="real" /> <VARIABLE id="y" level="1" type_id="real" /> <VARIABLE id="a" level="1" type_id="arr" /> <VARIABLE id="t" level="1" type_id="boolean" /> <STRING_CONSTANT value="r" type_id="char" /> </PARAMETERS> </CALL> The PARAMETERS node has a child node for each actual parameter. 4/4/2019

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. 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) _ 4/4/2019

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. The front end creates only correct symbol tables and parse trees. The structure of the parse tree encodes the operator precedence rules. 4/4/2019

Review: The Front End Parser, cont’d Each type specification is represented by a data structure that completely describes the type. A type specification object represents each type, whether the type is named or unnamed. If a type is named in a type definition, its type specification object points to the type identifier’s symbol table entry, and the symbol table entry points to the type specification object. The type specification data structures of one type can point to the type specification data structures of another type. The type specification object for a record type points to a separate symbol table that contains the names of the record fields. 4/4/2019

Review: The Front End Parser, cont’d As the parser parses the source program from beginning to end: It enters and exits nested scopes. It pushes a symbol table onto the symbol table stack whenever it enters a scope (program, procedure, function, record type definition). It pops off a symbol table whenever it exits a scope. Besides doing syntax checking, the parser also must do type checking. Semantic action. Look up type information in the symbol table. _ 4/4/2019

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. _ 4/4/2019

Back to the Compiler Back End Code Generator! 4/4/2019