An even simpler way imo, is explicit functions instead of a precedence table, then the code pretty much has the same structure as EBNF.<p>Need to parse * before +?
Begin at add, have it call parse_mul for its left and right sides, and so on.<p><pre><code> parse_mul() {
left = parse_literal()
while(is_mul_token()) { // left associative
right = parse_literal()
make_mul_node(left, right)
}
}
parse_add() {
left = parse_mul()
while(is_add_token()) { // left associative
right = parse_mul()
make_add_node(left, right)
}
}
</code></pre>
Then just add more functions as you climb up the precedence levels.