SE250:lab-8:klod004

From Marks Wiki
Revision as of 05:20, 3 November 2008 by Mark (Sọ̀rọ̀ | contribs) (8 revision(s))
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Lab 8

This lab deals with making parse trees. There was a lot of hassle trying to get this lab to run. The error was in the error function which was supposed to be placed earlier, so there was a little cut and paste there. And the other error was a typo, ""stricmp"" was supposed to be ""strcmp"". After I got these rectified, the lab was compiling fine.

Task 1

The code needed for this task was

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

Which spits out the output needed:

-(-(a b))

This was relatively simple once I figured out that the brackets are inserted automatically.


Task 2

This task was way harder, trying to figure out where the terms go and all.

The easiest way I could do, use to manually draw out the tree on paper. Then for the code, make a separate parse tree for each branch, then in the end, combine them all into the one main tree:

ParseTree* t1 = mkNode('+', mkNode('a',0),mkNode('b', 0),0);
ParseTree* t2 = mkNode('>', t1, mkNode('c',0),0);
ParseTree* t3 = mkNode('+', mkNode('y', 0),mkNode('b',0),0);
ParseTree* t4 = mkNode('*', mkNode('z',0),t3,0);
ParseTree* t5 = mkNode('=', mkNode('a',0),mkNode('2',0),0);
ParseTree* t6 = mkNode('-', mkNode('x',0),mkNode('y',0),0);
ParseTree* t7 = mkNode('-', mkNode('y',0),mkNode('x',0),0);
ParseTree* t8 = mkNode('?', t5, t6, t7,0);
ParseTree* t9 = mkNode('?',t2,t4, t8, 0);
prefix_tree(t9);
tree_to_graph(t9, g2);

Output given was:

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

This is as far as I was able to reach in the lab, might go home and do more later.