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 <[email protected] > <mailto:[email protected]>> > > 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:* [email protected] > <mailto:[email protected]> > [mailto:[email protected] > <mailto:[email protected]>] *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 > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en -~----------~----~----~----~------~----~------~--~---
List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
