You can search for recursive descent. You can pretty much ignore
any of the technical sites. Left factoring and all of the
complicated stuft isn't needed. Try to understand a recursive
description of the language | stands for or and -> stands
for "is defined as".
expr-> term | term + expr | term - expr
term-> factor * term | factor / term | factor
factor-> (expr) | num
num-> [0..N]
The actual code will use a function called match(t) that
will check the next token against t. If t equals the
next token then it will read the next token and return true
else it will just return false.
Here is example of how expr could be coded
Code:
int expr()
{
int val = 0;
val = term();
switch(next_token.type) {
case PLUS:
match(PLUS);
val += expr();
break;
case MINUS:
match(MINUS);
val -= expr();
break;
default:
break;
}
return val;
}
A function such as factor could be coded
Code:
int factor()
{
int val = 0;
switch(next_token.type) {
case NUMBER:
val = next_token.val;
break;
case LEFT_PARAN:
match(LEFT_PARAN);
val = expr();
match(RIGHT_PARAN);
break;
default:
break;
}
return val;
}
This preserves the structure of the expression but
it evaluates it. Instead of doing that you could pass
in the root of each subtree and have each function
add a node to the tree.