It took me only about 10 minutes
to add the few lines to build the tree (:
These are the two most important functions to build the
tree the rest of the functions follow the same form.
Code:
Node* term()
{
Node* root, *left, *right;
left = factor();
switch(next_token.type) {
case MULTIPLY:
match(MULTIPLY);
right = term();
root = new Node('*', left, right);
break;
case DIVIDE:
match(DIVIDE);
right = term();
root = new Node('/', left, right);
break;
default:
root = left;
break;
}
return root;
}
Node* factor()
{
Node* root = 0;
switch(next_token.type) {
case NUMBER:
root = new Node('0' + next_token.val, 0, 0);
match(NUMBER);
break;
case LEFT_PARAN:
match(LEFT_PARAN);
root = expr();
match(RIGHT_PARAN);
break;
default:
break;
}
return root;
}