SE250:lab-9:llay008

From Marks Wiki
Jump to navigation Jump to search

After looking at all of the tasks I decided to do Task Two, as this seemed the easiest of the three (although they all seemed quite hard). I spent a considerable amount of time (more than half of the lab) looking at the code and trying to understand how it worked and what I was actually trying to accomplish. An improvement for next year could perhaps be a couple of hints to help get people going. In the end, I asked the lab tutor for help and she was able to explain it to me so that I had a clearer idea of what I was doing.

I decided to go for a simple if else solution to go through each of the cases and then implement the relevant code for it. This left me with something like the following code. As a side note, while it was nice to have a list of all the functions (current etc) it would have been nice if the parameters and return types could have been included as well as it was annoying to have to keep looking it up.

Tree* Stmt( TokenStream* tokens ) {
    Tree *t = P(tokens);

    if(eqToken(TOK_IF, current(tokens))){

    }else if(eqToken(TOK_WHILE, current(tokens))){

     }else if(eqToken(TOK_OPENBRACE, current(tokens))){

    }else if(isVariable(current(tokens))){

    }else if(eqToken(TOK_SKIP, current(tokens))){

    }else if(eqToken(TOK_PRINT, current(tokens))){

    }else {
	return t;
}

I then proceeded to expand this code. Thanks to the clear explanations of what the provided functions did, this was not too hard, but there was a couple of things that I found really annoying. The first was that my code WOULD NOT COMPILE... I kept getting a segmentation fault. I tried creating new files and copying my code to them, but it made no difference. This was EXTREMELY frustrating as I had no way (and still don't) of checking my code... therefore it MAY BE WRONG.

Unfortunately, by the time I was up to this stage there was no one to ask for help. I looked on the wiki to check my code against what other people had done. I found that at the time there was only one other person who had the same kind of approach and that was dols008. Assuming that his solution is correct, then I found that the logic behind my tree was right, but some of my implementation of the tree and the mkNodes was faulty. Which brings me on to my second complaint...

I was mildly frustrated that the mkNode function had changed from last week, after all the time that I had spent figuring out how it worked then, I had to re-adjust my ideas on how it worked and this took some time (and as mentioned above - I didn't get it right the first time).

Tree* Stmt( TokenStream* tokens ) {
    Tree *t = P(tokens);
    Tree *t1,*t2;

    if(eqToken(TOK_IF, current(tokens))){
	advance(tokens);
	t1 = Exp(tokens,0);
	expect(tokens, TOK_THEN);
	t2 = Stmt(tokens);
	advance(tokens);
	if(eqToken(TOK_ELSE, current(tokens))){
 	    t = mkNode3(TOK_IF, t1, t2, Stmt(tokens));
            return t;
	}else{
	    t = mkNode2(TOK_IF, t1, t2);
            return t;
    }

    }else if(eqToken(TOK_WHILE, current(tokens))){
	advance(tokens);
	t1 = Exp(tokens,0);
	expect(tokens, TOK_DO);
	t = mkNode1(TOK_WHILE, Stmt(tokens));
        return t;

    }else if(eqToken(TOK_OPENBRACE, current(tokens))){
	advance(tokens);
	t = StmtSeq(tokens);
	advance(tokens);
	expect(tokens, TOK_CLOSEBRACE);
        return t;
	
    }else if(isVariable(current(tokens))){
	t1 = mkNode0(current(tokens));
        advance(tokens);
	expect(tokens, TOK_EQ);
	t = mkNode2(TOK_EQ, t1, Exp(tokens, 0));
	return t;

    }else if(eqToken(TOK_SKIP, current(tokens))){
	t = mkNode0(TOK_SKIP);
        return t;

    }else if(eqToken(TOK_PRINT, current(tokens))){
	advance(tokens);
	t = mkNode1(TOK_PRINT, Exp(tokens,0));
	return t;
   
 }else {
	return t;
}

Because I have not been able to test it, I know that that this code needs improving before it would work as it is supposed to. Nevertheless I felt that I should show something for the effort I put into it so here it is.