[antlr-interest] Inject Lexer Tokens before parsing further
Indhu Bharathi
indhu.b at s7software.com
Thu Apr 9 19:47:07 PDT 2009
Curious. Why not change formula production to
formula
: EQ expression
| expression
;
If for some reason you want to enforce that the input has to start with PLUS
or MINUS then maybe you can rewrite formula production as
formula
: EQ expression
| (PLUS | MINUS)=> expression
;
This way, PLUS or MINUS won't be consumed while checking and it will be
consumed as part of expression. But first option is more elegant and go for
second only if you need it.
- Indhu
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Des Hartman
Sent: Friday, April 10, 2009 6:15 AM
To: ANTLR
Subject: [antlr-interest] Inject Lexer Tokens before parsing further
I have two ways of entering an expression to evaluate:
1) =-5+4
2) -5+4
I have written the usual expression evaluator that evaluates the expression
as per 1) above to -1. The problem lies with the second form of input. Since
the parser recognises the "=" OR the "-" as the start of an expression, it
then strips the "=" and "-" off and passes the rest to the expression
evaluator. This means for "=" this is correctly done as "-5+4", but for the
"-" this is incorrect as "5+4".
Is there a way to "reinject" the "-" so that the expression evaluated is
"-5+4" again?
For reference here is the relevant parts of the parser
cell
: formula EOF
| EOF
;
formula
: EQ expression
| PLUS expression
| MINUS expression
;
expression
: equalityExpression
;
equalityExpression
: relationalExpression (( EQ | NOTEQ) relationalExpression)*
;
<snip>
unaryOperation
: PLUS operand
| MINUS operand
| operand
;
operand
: literal
| LPAREN expression RPAREN
;
literal
: NUMBER
| STRING
;
--
Thanks
Des
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090410/ef33b42d/attachment.html
More information about the antlr-interest
mailing list