Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 432: Compiler Construction Lecture 11

Similar presentations


Presentation on theme: "CS 432: Compiler Construction Lecture 11"— Presentation transcript:

1 CS 432: Compiler Construction Lecture 11
Department of Computer Science Salisbury University Fall 2017 Instructor: Dr. Sophie Wang 4/4/2019

2 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

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

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

5 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

6 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

7 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

8 Records and the Symbol Table Stack
4/4/2019

9 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

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

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

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

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

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

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

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

17 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

18 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

19 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

20 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

21 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

22 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

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

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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

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

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


Download ppt "CS 432: Compiler Construction Lecture 11"

Similar presentations


Ads by Google