1 / 33

CS 153: Concepts of Compiler Design October 3 Class Meeting

This study covers type checking, control statements, syntax diagrams, parse trees, nested scopes with symbol table stacks, and parsing programs, procedures, and functions using Pascal language concepts.

vbenny
Download Presentation

CS 153: Concepts of Compiler Design October 3 Class Meeting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 153: Concepts of Compiler DesignOctober 3 Class Meeting Department of Computer ScienceSan Jose State UniversityFall 2019Instructor: Ron Mak www.cs.sjsu.edu/~mak

  2. Type Checking Control Statements wci.frontend.pascal.parsers.IfStatementParser public ICodeNode parse(Token token) throws Exception { token = nextToken(); // consume the IF ICodeNodeifNode = ICodeFactory.createICodeNode(ICodeNodeTypeImpl.IF); ExpressionParserexpressionParser = new ExpressionParser(this); ICodeNodeexprNode = expressionParser.parse(token); ifNode.addChild(exprNode); TypeSpecexprType = exprNode != null ? exprNode.getTypeSpec() : Predefined.undefinedType; if (!TypeChecker.isBoolean(exprType)) { errorHandler.flag(token, INCOMPATIBLE_TYPES, this); } token = synchronize(THEN_SET); ... }

  3. Syntax Diagram for Variables • A variable can have any combination of subscripts and fields. • Appear in an expression or as the target of an assignment statement. • Example: var9.rec.flda[b][0,'m'].flda[d] := 'p' • The parser must do type checking for each subscript and field. The outer loop back allows any number of subscripts and fields.

  4. Parse Tree for Variables • VARIABLE nodes can now have child nodes: • SUBSCRIPTS • FIELD Assume that b and d are enumeration constants and that b =1 and d = 3 Parser classVariableParser

  5. Demo • Pascal Syntax Checker III • Parse a Pascal block • declarations • statements with variables • Type checking

  6. 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);

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

  8. 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 changethe value of the caller’s actual parameter value.

  9. Formal Parameter List, cont’d • VAR parameters are passed by reference. • The formal parameter is assigned a reference (pointer) to the actual parameter value. Since the routine has access to the actual parameter, it can changethe caller’s actual parameter value.

  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 DeclaredRoutineParserare subclasses of DeclarationsParser. • DeclaredRoutineParser depends on VariableDeclarationsParser to parse the formal parameter list.

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

  20. DeclaredRoutineParserMethods • parse() • 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 routinewhen done parsing the routine.

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

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

  23. Classes for Parsing Calls • A new statement parser subclass: CallParser. • CallParser has two subclasses, CallDeclaredParser and CallStandardParser. • CallStandardParserparses calls to the standard (predefined) Pascal routines. • Each parse() method returns the root of a parse subtree._

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

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

  26. 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"> <VARIABLE id="m" level="1" type_id="integer" /> </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.

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

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

  29. Pascal Syntax Checker IV • Now we can parse entire Pascal programs! • Demo • Onward to executing entire Pascal programs!

  30. Assignment #5: Time Type • Add a built-in time data type to Pascal. • Add the type to the global symbol table. • Implement it as a record type with integer fields hour, minute, and second. • Field hour should be limited to values 0 through 23, and fields minute and second should each be limited to values 0 through 59.

  31. Assignment #5: Time Type • Be able to declare timevariables and assign values to their fields. • Compile and executetime.txt. • Each valid assignmentstatement should printthe assignment message. time.txt VAR     now, later: time; wrong: time; BEGIN  now.hour := 12; now.minute := 15; now.second := 37; later := now; wrong.hour := 24; wrong.minute := 60; wrong.second := 0; END.

  32. Assignment #5, cont’d • Start with the Java source files from Chapter 11. • Examine wci.intermediate.symtabimpl.Predefinedto see how the built-in types like integer and real are defined. • Examine wci.frontend.pascal.parsers.RecordTypeParserto see what information is entered into the symbol table for a record type.

  33. Assignment #5, cont’d • The only source files you should need to modify is Predefined.java. • If you successfully implement the new time type, runtime assignments to time variables should work because the backend executor already knows how to handle record variables. • Due Monday, October 14.

More Related