[antlr-interest] Inject Lexer Tokens before parsing further
Indhu Bharathi
indhu.b at s7software.com
Thu Apr 9 21:49:28 PDT 2009
Well, in that case the second solution that uses syntactic predicate
should work.
Des Hartman wrote:
> Indhu
>
> This is based on how users enter formulas in Excel. It allows you to
> prefix a formula with "=", "-" or "+" to indicate it is a formula
> being entered, so I have to accomodate it.
>
> I have a rough solution which is to Pre-parse the text and then change
> the text to "=-5+4" and then pass the new text to the second parser,
> but I was wondering if there is a more elegant solution
>
> Thanks
> Des
>
>
>
> 2009/4/10 Indhu Bharathi <indhu.b at s7software.com
> <mailto:indhu.b at s7software.com>>
>
> 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>
> [mailto: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/17a5b86b/attachment.html
More information about the antlr-interest
mailing list