Original Post
Hello all,
Okay say I have this line of source code...
a = b + c * d;
I can convert the expression into a list of tokens like this:
IDENTIFIER "a"
OPERATOR_ASSIGN
IDENTIFIER "b"
OPERATOR_ADD
IDENTIFIER "c"
OPERATOR_MULTIPY
IDENTIFIER "d"
But here's the confusing bit. Jack Crenshaw's tutorial says it's possible to parse arithmetic expressions without looking ahead by more than one token. I can't figure out how to do it. And I don't understand his code. It's the operator precedence that's causing problems. I want to translate the above expression (or any other) into a tree like this:
Also I have another question. Would it be possible to write a generalised routine to parse grammar (like control structures) and arithmetic expressions, based on a set of rules provided at runtime? If so how would you approach it? I already have the blueprint for a generic lexical scanner, so I've negated the need to use lex.
Obviously I'd like to do the same for yacc.
Thanks for reading.
EDIT: Okay I just noticed I said 'binary tree' in the subject line. Obviously I'll need to include one or two unary operators as well.
Oh shit the tree's buggered up now. Hang on....
[edited by - MutteringGoblin on May 10, 2003 9:17:58 PM]
[edited by - MutteringGoblin on May 10, 2003 9:19:39 PM]
ASSIGN
/ a ADD
/ b MULTIPLY
/ c d