程序代做 Computer and Information Science Engineering

Computer and Information Science Engineering

The main problem with the PT[] approach:
“client code” is intimately tied to “provider” details

Copyright By PowCoder代写 加微信 powcoder

(aka “lack of data abstraction”)
What would happen if we changed the PT rep?
We need a ParseTree class in which the representation details are hidden
Key point: Parser, Printer, Executor functions should not know the internals of the PT class
In SWI/II, you were probably given the interface the client code should see and asked to implement it.
Here, we will design the interface, then worry about implementing it
A Better Approach

What is the interface that clients of ParseTree should see?
First: Need — not just a ParseTree object, but a cursor
to track where we are in PT
Some ParseTree operations:
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
Knowledge check:
How do we go back up to the parent node?

void PrintIf(ParseTree& p) {
write( “if” );
p.goDownLB(); PrintCond(p);
p.goUp(); p.goDownMB(); PrintSS(p);
p.goUp(); x = p.currAlt();
if (x == 2) then {
p.goDownRB(); write (“else”); PrintSS(p); };
write(“end ;”); } // Bug!
void printIf(int n) { write(“if);
printCond(PT[n,3]); write(“then”);
printSS(PT[n,4]);
if (PT[n,2]==2) … }
Recursive Descent Printing
Knowledge check:
What is the bug here?
Knowledge check:
What, if anything, is wrong here?
Knowledge check:
How can we be sure we are at
an node?

We didn’t see how the ParseTree is represented
or what a node looks like –
e.g.: how do we “go back up”?

You could use an array rep or pointers or whatever
The client (Parser/Printer/Executor) will be unaffected
Point of Abstraction

int currIdVal();
void setIdVal(int x);
void ExecAssign(ParseTree p) {
p.goDownMB(); x = EvalExp(p); p.goUp();
p.goDownLB(); p.setIdVal(x); } // Bug!

ParseTree Class (contd.)
Knowledge check:
Do we need any other operations?
What if we are at an node?
What if we are *not*, in fact, at an node?
Knowledge check:
Do we need any other operations?

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

void ParseAssign(ParseTree& p) {
p.setNTNo(7); p.setAltNo(1);
p.createLB(); p.createMB();
p.goDownLB();
ParseId(p); p.skipToken();
p.goUp(); p.goDownMB(); ParseExp(p);
p.skipToken(); p.goUp; }
Knowledge check: Why?
Why? And why skipToken()?

It is not object-oriented.

An important OO precept:
Treat each (semi-)independent entity as a separate object

Knowledge Check:
Anything Wrong with the ParseTree Class Approach?
Knowledge check: Why not?
What does object-oriented mean, anyway?
Knowledge check:
How are we violating this?
Knowledge check:
How can we do better?

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