1 / 22

CSE 131B – Compiler Construction II

CSE 131B – Compiler Construction II. Discussion 2: Eclipse & Phase 1 1/17/2007. Overview. Eclipse Tutorial Types Type Checking Error Reporting Topics/Questions you may have. Announcements.

kat
Download Presentation

CSE 131B – Compiler Construction II

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. CSE 131B – Compiler Construction II Discussion 2: Eclipse & Phase 1 1/17/2007

  2. Overview • Eclipse Tutorial • Types • Type Checking • Error Reporting • Topics/Questions you may have

  3. Announcements • As listed on the webpage and WebBoard, the given Makefile has been slightly modified. You need to get the updated copy if you want to be able to compile correctly! • I have your graded quizzes – please pick them up from me

  4. Eclipse Tutorial • Updated and available on the website! • Here is a live demonstration

  5. Types Type ::= QualIdent // For aliases | BasicType // See below | T_ARRAY OptExprList T_OF Type | T_RECORD OptQualIdent FieldsList T_END | T_POINTER T_TO Type | T_PROCEDURE OptFormalParams ; BasicType ::= T_INTEGER | T_REAL | T_BOOLEAN | T_CHAR ;

  6. Types • We will need to create objects for Basic Types, Array Types, Record Types, and Pointer Types. • How can these objects be organized to make our lives easier? • What methods and fields should we provide within each?

  7. One Possible Type Hierarchy Type BasicType CompositeType NumericType BoolType CharType ArrayType RecType PointerType IntType RealType

  8. What Methods Are Useful • Look at how the STO.java and *STO.java files are written. • Consider making methods like isNumeric(), isReal(), isInt(), isChar(), isArray(), etc. available in your Type Hierarchy. • Method Overriding can be your friend!

  9. What Else Is Useful? • All Types would benefit from methods like: • isCompatible(Type t) – coercible type (ie, Int  Real) • isEquivalent(Type t) – same type • Some types will need to store more information: • ArrayType may need to store dimensions • RecordType may need to store Vector of fields • You will even eventually need the size of these types for Code Generation!

  10. Setting Types • Must ensure that STO’s all have some Type field within them and that this Type field is set when the type becomes known. • What changes need to be made to the CUP and OParser files?

  11. Example of Setting Type • CUP rule currently states: VarDecl ::= VarDecl IdentList:_2 T_COLON Type T_SEMI {: ((OParser) parser).DoVarDecl (_2); :} • We now want to incorporate the Type, so we pass the Type to the OParser method as well. VarDecl ::= VarDecl IdentList:_2 T_COLON Type:_3 T_SEMI {: ((OParser) parser).DoVarDecl (_2, _3); :}

  12. Example of Setting Type • Now, in OParser.java, want to add Type: void DoVarDecl (Vector lstIDs, Type t) { for (int i = 0; i < lstIDs.size (); i++) { String id = (String) lstIDs.elementAt (i); if (m_symtab.accessLocal (id) != null) { m_nNumErrors++; m_errors.print (Formatter.toString(ErrorMsg.redeclared_id, id)); } VarSTO sto = new VarSTO (id); // Add code here to set sto’s type field!!! m_symtab.insert (sto); } }

  13. Type Checking • Now that we have associated types with our STOs, how do we check them?

  14. Type Checking Example • Consider the following code: VAR x : INTEGER; (* VarSTO x gets created and Type set to INTEGER *) VAR y : REAL; (* VarSTO y gets created and Type set to REAL *) BEGIN x := 5; (* OK *) y := x + 12.5; (* OK *) x := y; (* Error *) RETURN 0; END. • Let’s focus on the statement y := x + 12.5

  15. Type Checking Example y := x + 12.5; Currently CUP has: Expr2 ::= Expr2:_1 AddOp:_2 Expr3:_3 {: RESULT = _1; :} • What needs to be done? • Based on AddOp (+, -, OR), we need to check the types of _1 and _3 • Based on the types of _1 and _3, we need to create a new STO (an ExprSTO) to return as the result.

  16. Type Checking Example • Getting a Type out of the STO • You have some STO variable “a” and you want to check if it is Equivalent to STO variable “b”: • a.getType().isEquivalent(b.getType()) • The isEquivalent method should return a Boolean • As Professor Griswold demonstrated, you can eventually further cleanup this into something like isCompatible(a, b).

  17. Questions for Types / Type Checking • Ask now! • We talk about Error Reporting next

  18. Error Reporting • Now that we can check types, we will find errors! • Once we find them, we want to print them out. • Use only the provided error messages in ErrorMsg.java – these correspond nicely with each check.

  19. Error Reporting • Only report the first error found in each statement. • Once an error is found in a statement, suppress all further errors that may occur in that statement. • We will do our best to not make ambiguous tests for grading (I.e., where it may not be clear which error should be reported with a compound error in a given statement).

  20. ErrorSTO • ErrorSTO is made such that it will appear to be any other STO. Once you find an error, you may want to make your result be ErrorSTO so the error does not propagate throughout the program.

  21. What to do Next! • Finish up Phase 1. • Write more test programs. • Start Phase 2. • Come to lab hours and ask questions.

  22. Topics/Questions you may have • Anything else you would like me to go over now? • Anything in particular you would like to see next week?

More Related