java - How to apply operator precedence for calculator? Library? Is ANTLR the right library to use? -
i'm trying make calculator app applies operator precedence.
however, haven't been able find clear references on how apply in java. understand have use recursive descent parser (unless there's method or better way this). if so, better me code parser myself? or should utilize library.
while browsing forums, came upon answer suggested using antlr find seems language grammar rather applying operator precedence in expressions.
if it's better me code recursive descent parser myself, please link me references on how so?
if should use library, please link me 1 examples how apply operator precedence?
i'm not sure how apply antlr operator precedence , haven't seen clear references in docs either on how apply leaves me doubts whether or not right library use.
thanks!
i don't see why mixing antlr operator precedence. antlr parser (and lexer) generator. use bison/flex same result.
now, precedence if given grammar (there might error, i've seen example doing mult '+' mult
):
expr: mult '+' expr | mult '-' expr | mult ; mult: atom '*' mult | atom '/' mult | atom ; atom: t_number | '(' expr ')' ;
this grammar map naturally java code: each rules function getting next token, , on.
for example, expression 1 + 2 * 7 do:
rule expr: read token '1' (from rule atom) read '+' read new expr read '2' (from rule atom) read '*' -> use rule 'mult'. read '7' (rule atom). -> 1 + ( (2) * (7))
i can't explain logic behind that, learned in france 9 years ago, , don't need parser/lexer.
Comments
Post a Comment