CSE 131B – Compiler Construction II

Slides:



Advertisements
Similar presentations
CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Advertisements

Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
CSE 131B – Compiler Construction II Discussion 1: Getting Started 1/10/2007.
CSE 131B – Compiler Construction II Discussion 4: Project 1 1/31/2007.
CSE 131B – Compiler Construction II Discussion 3: Project 1 – Constants, Type Aliases, Procedures 1/24/2007.
CSE 131B – Compiler Construction II Discussion 7: Short-Circuiting, Pointers/Records, and Arrays 2/28/2007.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Looping and Counting Lecture 3 Hartmut Kaiser
Contextual Analysis (Chapter 5) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
Shyam Gurram Graduate Assistant
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Selection (also known as Branching) Jumail Bin Taliba by
Motivation and Overview
Semantic Analysis with Emphasis on Name Analysis
Numeric Arrays Numeric Arrays Chapter 4.
Sorry these slides weren’t available last evening!
C Basics.
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Learning to Program in Python
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CS 3304 Comparative Languages
(c)opyright Brent M. Dingle 2001
File Handling Programming Guides.
Instructor: Ioannis A. Vetsikas
Chapter 16 Pointers and Arrays
Chapter 4 void Functions
slides created by Ethan Apter
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Pointers and Arrays.
Computer Science II First With files.
Computer Science II First With files.
Review and Q/A.
Object-Oriented Programming Using C++ Second Edition
Java Tutorial – Application Building
Defining methods and more arrays
Parameter Passing in Java
slides created by Ethan Apter
Beginning C for Engineers
Chapter 16 Pointers and Arrays
Designing a Method and Test Program
Lecture 14: Inheritance Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Jeff West - Quiz Section 8
Jeff West - Quiz Section 9
Suggested self-checks: Section 7.11 #1-11
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
CMSC 202 Exceptions 2nd Lecture.
Java Programming Language
Dr. David Matuszek Spring, 2003
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
slides created by Ethan Apter and Marty Stepp
Class rational part2.
CS31 Discussion 1H Fall18: week 7
CMSC 202 Exceptions 2nd Lecture.
Classes and Objects Object Creation
Computer Science II First With files.
CMSC 202 Constructors Version 9/10.
Presentation transcript:

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

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

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

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?

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

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!

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!

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?

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

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

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

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

Type Checking Example y := x + 12.5; What needs to be done? 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.

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

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

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.

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

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.

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

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?