CSE 3341/655; Part 3 35 This is all very wrong! What would the SW1/SW2 (RESOLVE) people say if they saw this? Problem: Lack of data abstraction; What would.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

CPSC 388 – Compiler Design and Construction
A little bit on Class-Based OO Languages CS153: Compilers Greg Morrisett.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Exercise: Balanced Parentheses
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Abstract Classes and Interfaces
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Chapter 5 Top-Down Parsing.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
Pointers OVERVIEW.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Semantic Analysis II. Messages Please check lecturer notices in the Moodle Appeals  Legitimate: “I don’t have the bug you mentioned…”  Illegitimate:
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
CompSci Today’s Topics Computer Science Noncomputability Upcoming Special Topic: Enabled by Computer -- Decoding the Human Genome Reading Great.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Core Core: Simple prog. language for which you will write an interpreter as your project. First define the Core grammar Next look at the details of how.
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Recursive Descent (contd)
Programming Language Syntax 5
Writing Functions.
Recap Week 2 and 3.
CS 350 – Software Design Singleton – Chapter 21
The Recursive Descent Algorithm
CSE 206 Course Review.
More C++ Classes Systems Programming.
SPL – PS3 C++ Classes.
Presentation transcript:

CSE 3341/655; Part 3 35 This is all very wrong! What would the SW1/SW2 (RESOLVE) people say if they saw this? Problem: Lack of data abstraction; What would happen if we changed the PT rep? We need a ParseTree class! Key point: Parser, Printer, Executor should not know the internals of the PT class Recursive Descent (contd)

CSE 3341/655; Part 3 36 What are the ParseTree operations? First: Need -- not just a ParseTree object, but a cursor to track where we are in PT Some ParseTree operations: int currNTNo(); // non-term. at current node int currAlt(); // Alternative no. used at current node void goDownLB(); // move cursor to left child void goDownMB(); //move cursor to second child void goDownRB(); //move cursor to third child ParseTree class

CSE 3341/655; Part 3 37 How do we go back up? void goUP(); // move cursor to parent node ParseTree class

CSE 3341/655; Part 3 38 void PrintIf(ParseTree& p) { write( “if” ); p.goDownLB(); PrintCond(p); p.goUp(); p.goDownMB(); PrintSS(p); // bug! p.goUp(); x = p.currAlt(); if (x == 2) then { p.goDownRB(); write (“else”); PrintSS(p); }; write(“end ;”); } // Bug! Recursive Descent

CSE 3341/655; Part 3 39 We didn’t see how the ParseTree is represented or what a node looks like - e.g.: how do we “go back up”? That is the point of data abstraction! You could use an array rep or pointers or whatever you like; the client (Parser/Printer/Executor) will be unaffected! ParseTree class

CSE 3341/655; Part 3 40 Need some more operations: If non-term at current node is, need its name, value etc. int currIdVal(); // error if we are not at node void setIdVal(int x); string idName(); void ExecAssign(ParseTree p) { p.goDownMB(); x = EvalExp(p); p.goUp(); p.goDownLB(); p.setIdVal(x); } // Bug! ParseTree class (contd.)

CSE 3341/655; Part 3 41 Problem: What about the parser? Key point: The parser adds nodes Hence: Need more operations Assumption: Calling procedure will create an “empty” node, set the cursor to that node, then call the appropriate parse procedure. ParseTree class

CSE 3341/655; Part 3 42 void ParseAssign(ParseTree& p) { p.setNTNo(7); p.setAltNo(1); // why? p.createLB(); p.createMB(); p.goDownLB(); ParseId(p); p.skipToken(); // why? and why p.skipToken? p.goUp(); p.goDownMB(); ParseExp(p); p.skipToken(); p.goUp; } Parsing

CSE 3341/655; Part 3 43 That is better than the original version but is not object-oriented. Object-oriented: Treat each node as a separate, independent object. Have a separate class for each non-terminal Each stmt. is an instance of the If class; each an instance of the Exp class;... Object-oriented?

CSE 3341/655; Part 3 44 class Prog { private: DS* ds; SS* ss; public: Prog() { ds = nil; ss = nil; } PrintProg() { write(“program”); ds →PrintDS(); write(“begin”); ss →PrintSS(); print(“end”); } ExecProg() { ds →ExecDS(); ss →ExecSS(); } // not quite Object-oriented approach (contd)

CSE 3341/655; Part 3 45 class Prog{ private: DS* ds; SS* ss; public: Prog() { ds = nil; ss = nil; } ParseProg() { skipToken(); // error check? ds = new DS(); ds →ParseDS(); ss = new SS(); ss→ParseSS(); // bug! skipToken(); skipToken();} Object-oriented approach (contd)

CSE 3341/655; Part 3 46 class If{ private: Cond* c; SS* ss1; SS* ss2; // why? public: If() { c = nil; ss1 = nil; ss1 = nil; } ParseIf() { int tokNo; skipToken(); // why? c = new Cond(); c →ParseCond(); skipToken(); ss1 = new SS(); ss1→ParseSS(); tokNo=getToken(); if (tokNo==“end”) {skipToken(); skipToken(); return; } skipToken(); // error check? ss2 = new SS(); ss2→ParseSS();..skip tokens.. } Object-oriented approach (contd)

CSE 3341/655; Part 3 47 class If{ // contd. Print If() { write(“if ”); c →PrintCond(); write(“then”); ss1 →PrintSS();... how to check alt. no? } // Each class can use its own approach ExecIf() { if ( c →EvalCond()) then { ss1 →ExecSS(); return;} if (!ss2) { ss2 →ExecSS();} } Object-oriented approach (contd)

CSE 3341/655; Part 3 48 class Stmt{ private: int altNo; Assign* s1; IF* s2; Loop* s3; Input* s4; Output* s5; public: Stmt() { altNo=0; s1 = nil;... } parseStmt() { int tokNo; tokNo = tok.getToken(); if (tokNo==...) { altNo=1; s1= new Assign(); s1->parseAssign();} if (tokNo==...) { altNo=2; s2= new IF(); s1->parseIF();} if (tokNo==...) { altNo=3; s3= new Loop(); s1->parseLoop();} if (tokNo==...) { altNo=4; s4= new Input(); s1->parseInput();} if (tokNo==...){altNo=5;s5= new Output(); s1->parseOutput();}} printStmt() { if (altNo==1) {s1->printAssign(); return; }... } execStmt() { if (altNo==1) {s1->execAssign(); return; }... } Object-oriented approach (contd)

CSE 3341/655; Part 3 49 Id class poses some problems: Need to make sure that two occurrences of same Id are not treated as two objects: class Assign { Id* id; Exp* exp; ParseAssign() { id = new Id(); id →Parse I d(); // Won’t Work! Need to check if the id in question already exists; and create a new one only if it does not. Solution: Make ParseId() a static method which will decide whether to create a new Id object Problem with Id class

50 class Assign { Id* id; Exp* exp;// nothing new here ParseAssign() { id = Id:: Parse I d(); // ParseAssign does not create a new // Id object since this Id object may already exist exp = new Exp(); exp  ParseExp();... // nothing new}... Static methods such as ParseId -- also called "class methods" Can be used even if we don't have an instance of the class But: in their bodies *cannot* refer to the normal member variables of the class! (why not?); they can only refer to static variables Problem with Id class (contd) CSE 3341/655; Part 3

51 class Id { string name; int val; bool declared; bool initialized; static Id* eIds[20]; // pointers to existing Id’s; *static* var static int idCount = 0; // also static Id(string n) { // *private* constructor! name = n; declared = false; initialized = false; } public: static Id* ParseId() { // get token; check if name matches eIds[k] →name for // any k = 0,..., idCount-1; if yes, return e Ids[k]; // if not: Id* nId = new Id(n1);...add to eIds[]... return nId; } int getIdVal() {return val; // check initialized first? setIdVal()? getIdName()?... Problem with Id class (contd) CSE 3341/655; Part 3

52 C++ needs special syntax for dealing with static variables: class Id { static Id* eIds[20]; static int idCount = 0; That is wrong -- you can't initialize in the header More generally, these are *declarations*; you also need defs; so change above to: static Id* eIds[]; static int idCount; Then outside the class: Id*Id::eIds[20]; // this is the def & allocates space int Id::idCount = 0; // this is the def & allocates space C++ : complications with static variables CSE 3341/655; Part 3

53 We have a number of parse procedures: ParseProg(), ParseDS(), ParseDecl(), ParseSS() etc. Each will need to use the tokenizer How to ensure there is only one? If you are using the ParseTree class approach: Include a tokenizer object as part of the parseTree object; and have ParseTree class provide methods, getToken(), skipToken(), etc. These methods will simply call the corresponding methods of Tokenizer class on the tokenizer object Tokenizer details...

CSE 3341/655; Part 3 54 If you are using the "OO" approach: We have a number of parse procedures: ParseProg(), ParseDS(), ParseDecl(), ParseSS() etc. Each will need to use the tokenizer How to ensure there is only one? Possible answers: Global variable Pass a single tokenizer around as a parameter Singleton pattern Tokenizer details (contd.)

CSE 3341/655; Part 3 55 Idea: Make constructor private (or protected); Have a local static variable (initialized to null) to hold a reference to the single instance; Provide a method Instance() that returns that class C { private: static C* instance = null; C(){ } // private constructor public: static C* Instance(){ //bit like ParseId() if (instance == 0) {instance = new C();} return instance; }... }; Singleton class

CSE 3341/655; Part 3 56 Scope of a variable/object: In which parts of the program do we have access to it? Life-time: When does the object come into existence and when does it go out of existence? Scope vs. Life-time