CMPE 152: Compiler Design October 2 Class Meeting

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

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.
Introduction to Programming CSCE 110 Drawn from James Tam’s material.
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
Block-Structured Procedural Languages Lecture 11: Dolores Zage.
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
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
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
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
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
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
CMPE 152: Compiler Design December 5 Class Meeting
CSCI 161: Introduction to Programming Function
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
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 4 Class Meeting
CMPE 152: Compiler Design September 11/13 Lab
CMPE 152: Compiler Design October 4 Class Meeting
kbkjlj/m/lkiubljj'pl;
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
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
CMPE 152: Compiler Design February 21/26 Lab
CMPE 152: Compiler Design February 28 / March 5 Lab
COMPILERS Semantic Analysis
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
Corresponds with Chapter 5
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:

CMPE 152: Compiler Design October 2 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor: Ron Mak www.cs.sjsu.edu/~mak

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

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.

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.

Formal Parameter List, cont’d VAR parameters are passed by reference. The formal parameter is assigned a reference (pointer) to the actual parameter value. The routine can change the actual parameter value.

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.

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 forward. forward is not a reserved word.

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.

Records and the Symbol Table Stack

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}.

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}.

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}.

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}.

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}.

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}.

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}.

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 that routine’s scope.

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

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.

Class DeclaredRoutineParser Parse any declared (programmer-written) routine: a procedure a function the program itself

DeclaredRoutineParser Methods First call parse_routine_name() 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 symbol table for the routine when done parsing the routine.

DeclaredRoutineParser Methods, cont’d parse_routine_name() parse_header() parse_formal_parameters() parse_parm_sublist() Call VariableDeclarationsParser ::parse_identifier_sublist() Enter each formal parameter into the routine’s symbol table defined either as a VALUE_PARM or a VAR_PARM.

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.

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

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. _

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

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

Formal and Actual, cont’d In class CallParser, method parse_actual_parameters() calls check_actual_parameter() to check each actual parameter against the corresponding formal parameter. There must be a one-to-one correspondence.

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 by 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.

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' ) Example call: proc( i, -7+m, a[m], v, y, a, t, 'r' )

CallParser::parse_actual_parameters() 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.

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.

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)

Pascal Syntax Checker IV Now we can parse entire Pascal programs! Demo Onward to interpreting Pascal programs.