SE250:lab-8:sdal039

From Marks Wiki
Jump to navigation Jump to search

First off it wouldn't compile.

/cygdrive/c/Users/sdal039/AppData/Local/Temp/ccztcwNj.o:lab-8.c:(.text+0x30b): undefined     reference to `_strcimp'
collect2: ld returned 1 exit status

Thanks to hpan027 for putting up solution, which was to search for 'strcimp' in parsetree.c and replace with 'stricmp'.

Task 1

k.

Task 2

ParseTree* expr = mkNode('-',  mkNode('-', mkNode('a', 0), mkNode('b', 0), 0), 0);
prefix_tree( expr );

Unfortunately the tree_to_graph function still only seems to produce .dot files, so Graphviz had to be used again to convert the .dot to a .jpeg. Sweet, just found the fix for this. Much easier now. Awesome points for those who figured that out.

Graph 1


Task 3

mkNode('?', mkNode('>', mkNode('+', mkNode('a', 0), mkNode('b', 0), 0), mkNode('c', 0)), 
   mkNode('*', mkNode('z', 0), mkNode('+', mkNode('y', 0), mkNode('b', 0), 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 ));
prefix_tree( expr2 );

Many parentheses later, this produced

?(>(+(a b) c) *(z +(y b)) ?(=(a 2) -(x y) -(y x)))

Task 4

Graph 2

There were not any discrepancies between what I had down on paper and what the graph produced. This implies that either there wasn't supposed to be any differences, or I didn't make my nodes correctly.

Task 5

Because the '?' node has three children, there is no ambiguity as to which expression belongs where. The ':' is used in C to differentiate between e1 and e1, whereas this is not needed for the parse tree as e1 and e2 are physically separated. For example, with the ternary expression (a == 0) ? b = 5 : b = 4; The tokenizer needs the ':' to split up 'b = 5' and 'b = 4' into different tokens, but once this has been done, the parse tree structure will ensure they remain separated.


Task 6

Looks hard...

int eval( ParseTree* t ) {
   return t->name;
}

This code works for individual numbers at least. :P