SE250:lab-8:tlou006

From Marks Wiki
Jump to navigation Jump to search

Q2

-(-(a b)) = -(a - b) ??

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

I am not sure how to draw the graph, tried the fix suggested but still doesnt work :(

but I have a pretty good idea what the graph should look like from other people's reports

tree_to_graph( t, 'image.png' );

produces errors


Q3

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

=

if( ( a + b ) > c )

z * ( y + b )

elseif( a == 2 )

x - y

else

y - x

Asked someone and found out tenary means 3(while binary means 2 etc..)

Setting the variables a,b,c,x,y,z to 1

then running

printf( "%d\n", ( a + b ) > c ? z * ( y + b ) : a == 2 ? x - y : y - x );

produces the expected output 2, So my syntax hypothesis was correct


ParseTree* t = 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 ), 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 );


Q4

Again I was having trouble with the graph


Q5

This was because the syntax of the tenary tree meeans that each parent has 3 branches.

Each branch representing cond, e1 and e2


Q6