SE250:lab-8:twon069

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

Task 1

hmm ok, so parsetree.c has been downloaded.

Task 2

Problem

  • Can't seen to find error function at first. Solved by moving the function up to be defined before called.
  • Strange error on compilation, (is this a debugging course??), problem solved with strcmpi being misspelled.
  • Had problem with printing the tree to a picture, thanks a lot to Jhor053 with changing the graphic directory.
  • Not really sure how to link picture so it displays on wiki, so will post link instead.

Solution

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

http://img84.imageshack.us/my.php?image=64827867ql1.jpg

Task 3

  • This is a VERRRY long task, most of the time spent writing a really long code to print out that line.
  • Very hard to picture this, and decided to just code it, then print a picture to see it.

Solution

	ParseTree* left = mkNode('>',mkNode('+',mkNode('a'),mkNode('b'),0),mkNode('c',0),0);
	ParseTree* middle = mkNode('*',mkNode('z',0),mkNode('+',mkNode('y',0),mkNode('b',0),0));
	ParseTree* right = 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);
	ParseTree* together = mkNode('?',left,middle,right,0);
	char *fname = "b.JPEG";
	prefix_tree(together);
	tree_to_graph(together,fname);
  • Phew, it looks right, and the picture was somewhat not what I expected, however I was able to understand that ternary means it will always have 3 children, meaning ? will have 0, which is the boolean it is checking, 1, the path it will take if boolean is correct, and 2, the path it will take if boolean is incorrect.

Task 4

  • oops, already printed the picture, below is what the tree looked like

http://img66.imageshack.us/img66/4760/67466415em1.jpg

Task 5

In C, it is needed to separate different condition when ? is called. However in parse tree, condition are already separated by the branches, so having a ":" seen redundant.

Task 6

uh.. will get back to this 1

String to Parse Tree

<E> ::= <E> <BinOp> <E> <E> will call itself, and will alternatively end in a loop, for example

a sequence such as 2+2+2, 2 is a <digit> hence a <E>, here <E> <BinOp> <E> will happily take 2+2 and say it's a <E>, however if +2 again, it will call itself: <E> being 2+2, and then <BinOp> with another <E>, ie 2+2+2+2.

This is just a guess, my mind hasn't gotten around with this yet, will get back to it.