Download presentation
Presentation is loading. Please wait.
Published byΆρχέλαος Νικολαΐδης Modified over 6 years ago
1
CMPE 152: Compiler Design October 2 Class Meeting
Department of Computer Engineering San Jose State University Fall 2018 Instructor: Ron Mak
2
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);
3
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
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.
5
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.
6
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.
7
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.
8
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.
9
Records and the Symbol Table Stack
10
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}.
11
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}.
12
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}.
13
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}.
14
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}.
15
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}.
16
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}.
17
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.
18
Parsing Programs, Procedures, and Functions
Classes ProgramParser and DeclaredRoutineParser are subclasses of DeclarationsParser. DeclaredRoutineParser depends on VariableDeclarationsParser to parse the formal parameter list.
19
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.
20
Class DeclaredRoutineParser
Parse any declared (programmer-written) routine: a procedure a function the program itself
21
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.
22
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.
23
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.
24
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
25
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. _
26
Class CallParser CallParser.parse() calls either:
CallDeclaredParser.parse() CallStandardParser.parse() Protected method parseActualParameters() is used by both subclasses.
27
Formal Parameters and Actual Parameters
Formal parameters: In a procedure or function declaration: Actual parameters: In a procedure or function call:
28
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.
29
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.
30
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' )
31
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.
32
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.
33
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)
34
Pascal Syntax Checker IV
Now we can parse entire Pascal programs! Demo Onward to interpreting Pascal programs.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.