SE250:lab-9:hpan027
Jump to navigation
Jump to search
Starting up
- Chose first task
- 10 minutes in, considering switching tasks.
- Chose second task
Reading the code
Parse by default calls the function for if-then-else statements - which has not been written. Hence code does not run.
Need to recode Parse into something like:
Given an input Work out what type of statement it is <- hard part Call appropriate method
Looking at the various inputs and attempting to translate them into code
"a=1", "a=1;b=2;k;k;k",
Self explanatory?
"ia>btwtda=a*2ek",
if (a > b) then while (t) do (a = a * 2) else k
"ict{x=1;wtda=a*2}ek",
if ( c ) then { x = 1; while (t) do (a = a*2); } else k;
"junk that won't parse",
Lols.
- Getting "a=2" to parse is pretty easy.
- Call Exp(tokens,2)
- Output
Parse("a=1"): =(a 1) a 1 =2
Onto the actual task
S ::= "if" E "then" S [ "else" S ] | "while" E "do" S | "{" Ss "}" | Ide "=" E | "skip"
Code will need a series of if statements to check for the keywords
//if ("if") //parse for if statements //else if ("while") //parse for while statements
etc
Very basic if-then-else
Tree* Stmt( TokenStream* tokens ) { //if then else statements if( current( tokens ) == TOK_IF ) printf("True\n"); advance( tokens ); Tree* t1 = Exp( tokens, 1 ); expect( tokens, TOK_THEN ); Tree* t2 = Exp( tokens, 1 ); expect( tokens, TOK_ELSE ); Tree* t3 = Exp( tokens, 1 ); Tree* t = mkNode3('?',t1,t2,t3); return t; }
- Doesn't work for anything except the most basic form of if-then-else.
- Need to change the Exp calls into Stmt calls - but can't do that until the rest of the function is finished
Revised if-then-else code
Tree* Stmt( TokenStream* tokens ) { //if then else statements if( current( tokens ) == TOK_IF ) { advance( tokens ); Tree* t1 = Exp( tokens, 1 ); expect( tokens, TOK_THEN ); Tree* t2 = Stmt( tokens ); if (!end_of_tokens(tokens)) { expect( tokens, TOK_ELSE ); Tree* t3 = Stmt( tokens ); return mkNode3('?',t1,t2,t3); } return mkNode2('?',t1,t2); } if( isVariable( current( tokens ) ) ) return Exp( tokens, 0 ); return mkNode('0',0); }
- Seems to work for the input "ia=1ta=2ea=3"
- Need to write the if blocks for other things defined under S
Other S grammar added
Tree* Stmt( TokenStream* tokens ) { //if then else statements /* IF */ if( current( tokens ) == TOK_IF ) { advance( tokens ); Tree* t1 = Exp( tokens, 0 ); expect( tokens, TOK_THEN ); Tree* t2 = Stmt( tokens ); if (!end_of_tokens(tokens)) { expect( tokens, TOK_ELSE ); Tree* t3 = Stmt( tokens ); return mkNode3('?',t1,t2,t3); } return mkNode2('?',t1,t2); } /* Variable assignment (e.g. a = 1) */ if( isVariable( current( tokens ) ) ) return Exp( tokens, 0 ); /* While loops */ if( current( tokens ) == TOK_WHILE ) { advance( tokens ); Tree* t1 = Exp( tokens, 0 ); expect( tokens, TOK_DO ); Tree* t2 = Stmt( tokens ); return mkNode2(TOK_WHILE,t1,t2); } /* { Curly brackets } */ if( current( tokens ) == TOK_OPENBRACE ) { advance( tokens ); Tree* t = StmtSeq(tokens); expect( tokens, TOK_CLOSEBRACE); return t; } /* SKIP */ if( current( tokens ) == TOK_SKIP) { return mkNode(0,0); } /* Print */ if( current( tokens ) == TOK_PRINT) { return mkNode1(TOK_PRINT, Exp(tokens,0)); } /* Base fail case */ return mkNode(0,0); }
- Trying to get this code to run with "ia=btwa=1da=a*2ek"
- Having some problems at the moment
- The default test cases do not work at all
- Variable assignment needed to be rewritten
if( isVariable( current( tokens ) ) ) { Tree* t1 = P(tokens); expect(tokens, TOK_EQ); return mkNode2(TOK_EQ, t1, Exp(tokens,0)); }
- Still problems
- Order matters...variable assignment should be right at bottom as i, p, e, etc are all considered variables
- /facepalm
- However getting there
- Output currently ?(=(a b) w(=(a 1) =(a *(a 2)))
- If statement seems to have been broken by the while
- Actually caused by the skip
- No idea how to produce an external node
- Changing test case to "ia=btwa=1da=a*2ea=2"
Parse("ia=btwa=1da=a*2ea=2"): ?(=(a b) w(=(a 1) =(a *(a 2))) =(a 2))
- Seems to work
Conclusion
Very long lab for 2 hours. Might finish later.