CS代写 Object-oriented approach (contd)

Object-oriented approach (contd)

Key points:
Each node in an abstract parse tree must be a separate object

Copyright By PowCoder代写 加微信 powcoder

Each node is an instance of the If class
Each node is an instance of the SS class
Each node is an instance of the Exp class; etc

Object-Oriented approach
Knowledge check:
What are the methods of these class?
Knowledge check (Trick question!):
What are the member variables of these class?

class If{ // in C++ pseudo-code; pointers are explicit in C++
Cond* c; SS* ss1; SS* ss2;
If() { c = nil; ss1 = nil; ss2 = nil; //C++ doesn’t initialize}
void ParseIf() { int tokNo; // t is global pointer to tokenizer
t->skipToken(); // “->” because t is a pointer
c = new Cond(); c->ParseCond(); t->skipToken(); // why?
ss1 = new SS(); ss1->ParseSS(); tokNo=t->getToken();
if (tokNo==“end”) {t->skipToken(); t->skipToken(); return; }
t->skipToken(); // error check?
ss2 = new SS(); ss2->ParseSS(); ..skip tokens.. }
Object-Oriented approach (contd)

class If{ // contd.
Print If() {
write(“if ”); c->PrintCond(); write(“then”);
ss1->PrintSS();
ExecIf() {
if (c->EvalCond()) then {ss1->ExecSS(); return;}
if (ss2) {ss2->ExecSS();}
Object-oriented approach (contd)
Knowledge check:
How do we access the ParseTree?
Knowledge check:
How do we check the altNo?

During execution of an OO program, it is best to think of memory being organized into three pieces:
Code portion: contains the (compiled) code of running progam
Stack portion: organized as a collection of “activation frames”
a new a.f. is created when a function/method is called …

Heap: contains objects that are created by calls to “new”;
each call to “new” assigns memory as specified in the variables of the class

Back to the Core interpreter …
OO Languages
Knowledge check:
And released when?
Knowledge check:
What does an a.f. contain?
Knowledge check:
When is this memory released?

class Stmt{
private: int altNo; Assign* s1; IF* s2; Loop* s3; Input* s4; Output* s5;

Stmt() { altNo=0; s1 = nil; … }
void parseStmt() { int tokNo; tokNo = tok.getToken();
if (tokNo==…) { altNo=1; s1= new Assign(); s1->parseAssign();} if (tokNo==…) { altNo=2; s2= new IF(); s2->parseIF();}
if (tokNo==…) { altNo=3; s3= new Loop(); s3->parseLoop();}
if (tokNo==…) { altNo=4; s4= new Input(); s4->parseInput();}
if (tokNo==…){altNo=5;s5= new Output(); s5->parseOutput();}}

void printStmt() {
if (altNo==1) {s1->printAssign(); return; }
execStmt() { if (altNo==1) {s1->execAssign(); return; }… }
Object-oriented approach (contd)
Knowledge check:
What happens on stack, heap when “new Stmt(): is executed?
Knowledge check:
Shouldn’t parseStmt() return the Stmt that it just parsed?

class Prog {
DS* ds; SS* ss;

Prog() { ds = nil; ss = nil; }
PrintProg() {
write(“program”); ds->PrintDS();
write(“begin”); ss->PrintSS();
print(“end”); }
ExecProg() {
ds->ExecDS(); ss->ExecSS(); }

Object-oriented approach (contd)
Knowledge check:
This is incorrect. What is wrong with it?

class Prog{
DS* ds; SS* ss;
Prog() { ds = nil; ss = nil; }
ParseProg() {
skipToken(); // error check?
ds = new DS(); ds->ParseDS(); skipToken();
ss = new SS(); ss->ParseSS();
skipToken(); }
Object-oriented approach (contd)
Knowledge check:
Why is there no altNo?
Knowledge check:
Not clear this will work … Why not?

Probably best to have ParseDeclIdList(), ParseDeclId(), ParseSSIdList() and ParseSSId() to provide context; similarly execReadIdList() and execWriteIdList()

class Assign {
Id* id; Exp* exp;
ParseAssign() {
id = new Id(); id->ParseId();

Problem with Id class
Knowledge check:
This won’t work. Why not?
Knowledge check:
We need to check if an Id with a given name already exists and create a new one only when needed.
How do we do that?
We need to use the static mechanism

The meaning of “static”: single copy, no matter how many instances of the class are created, All the instances share that single copy

class Assign {
Id* id; Exp* exp; // nothing new here

ParseAssign() {
id = Id::ParseId();
exp = new Exp(); exp->ParseExp(); … // nothing new}

Problem with Id class (contd)
Knowledge check:
Why do we need a special syntax for calling static methods?
Knowledge check:
In the body of a static method, we can refer only to the static member variabless of the class. Why?
Note: “static” is used to mean “single copy”. In C, “static” means “global”. Here, “static “ means “global
lifetime. If the method/variable is public, then it is also global.

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) { //
name = n; declared = false; initialized = false; }
static Id* ParseId() { // get token; check if name matches
// eIds[k] ->name for any k = 0, …, idCount-1;
// if yes, return eIds[k]; if not:
Id* nId = new Id(n1); …add to eIds[]… return nId; }
int getIdVal() {return val; // check initialized first?
setIdVal()? getIdName()? …
Knowledge check:
Where would you expect space for these variables to be allocated?
Knowledge check:
Why is the constructor private??

We have a number of parse methods:
ParseProg(), ParseDS(), ParseDecl(), ParseSS() etc.
Each will need to use the tokenizer
How to ensure there is only one?
In the ParseTree class approach: Include a Tokenizer object as part of the ParseTree object; and have the ParseTree class provide methods, getToken(), skipToken(), etc.
These methods will simply call the corresponding methods of Tokenizer class on the tokenizer object
Since there is only one ParseTree object, this will work
Similar problem with Tokenizer

Global variable containing the single Tokenizer
Pass the single Tokenizer around as a parameter

Singleton class:
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 instance

In the OO approach
Knowledge check: Why should this be initialized to null?

class C { static C* instance = null;
C(){ } // private constructor
static C* Instance(){ //like ParseId()
if (instance == 0) {instance = new C();}
return instance; }
Singleton class
Knowledge check:
Is this approach better, worse or about the same as having the
Tokenizer object as a global variable?

Scope vs Life-Time
Scope vs life-time:
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?

Knowledge check:
What is the scope and life-time of static variables?
What is the scope and life-time of member variables?

/docProps/thumbnail.jpeg

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com