SE250:lab-8:hpan027
Initial problems
- The error function was called before it was defined and hence needed a prototype declaration.
- "strcmpi" was misspelled as "strcimp"
Task Two
mkNode('-',mkNode('-',mkNode('a',0),mkNode('b',0),0),0);
- Took a while to figure out how to use mkNode
http://studwww.cs.auckland.ac.nz/~hpan027/250-8-1.jpg
- Still can't get the converter to work internally. Had to convert the .dot file manually
Task Three
mkNode('?',mkNode('>',mkNode('+',mkNode('a',0),mkNode('b',0),0),mkNode('c',0),0),mkNode('*', mkNode('z',0),mkNode('+',mkNode('y',0),mkNode('b',0),0)),mkNode('?',mkNode('=',mkNode('a',0), mkNode('2',0),0),mkNode('-',mkNode('x',0),mkNode('y',0),0),mkNode('-',mkNode('y',0),mkNode('x',0),0),0),0);
- Is it supposed to be this long?
Task Four
- Cannot even begin to imagine what this would look like
http://studwww.cs.auckland.ac.nz/~hpan027/250-8-2.jpg
- Makes sense
- Horrible quality for some reason ('C', some '-' and some '=' not drawn properly)
Task Five
- Possibly because it expects two set of statements to follow the '?'
- Not really sure at this point
Task Six
- Initial impression: could take a while
- Draft code
int eval( ParseTree* pt ) { //base case: if + - * / //evalute number within brackets with the operator //else eval( subtree ) }
- Very basic code working for single operation between two single-digit integers
int eval( ParseTree* pt ) { //base case: if + - * / if( pt->arity == 2 ) switch(pt->name){ case '+': return atoi(&pt->arg[0]->name)+atoi(&pt->arg[1]->name); break; case '-': return atoi(&pt->arg[0]->name)-atoi(&pt->arg[1]->name); break; case '*': return atoi(&pt->arg[0]->name)*atoi(&pt->arg[1]->name); break; case '/': return atoi(&pt->arg[0]->name)/atoi(&pt->arg[1]->name); break; } //evalute number within brackets for with the operator //else eval( subtree ) }
- This won't work for anything with one or three arguments
- Perhaps base case should be if it is a single integer leaf?
- Turned out to work fine
int eval( ParseTree* pt ) { if( atoi(&pt->name) > 0 ) //base case integer leaf return atoi(&pt->name); else switch(pt->name){ case '+': return eval(pt->arg[0])+eval(pt->arg[1]); break; case '-': return eval(pt->arg[0])-eval(pt->arg[1]); break; case '*': return eval(pt->arg[0])*eval(pt->arg[1]); break; case '/': return eval(pt->arg[0])/eval(pt->arg[1]); break; } }
- However screwed for anything that isn't an operation with two numbers (e.g. -(1+2))
- Although single argument operations would only apply to "-" and "+" - i.e. *(2-3) or /(5) makes no sense
- Use if statements to catch single argument operations?
case '+': return ( pt->arity>1 ? eval(pt->arg[0])+eval(pt->arg[1]) : eval(pt->arg[0]) ); case '-': return ( pt->arity>1 ? eval(pt->arg[0])-eval(pt->arg[1]) : -1* eval(pt->arg[0]) );
- Seems to work
- Now need to consider 2+ arguments & if statements?
- If statement would probably be a separate case
- For 2+ argument possibly loop through the whole arg array for values
- Each case statement would probably look something like
case '+': int i=0; int value = 0; for( i = 0; i < pt->arity-1; i++ ) { value+=eval(pt->arg[i]); return value;
After the lab
Comment on what?
Neat reports.