[antlr-interest] Inject Lexer Tokens before parsing further

Des Hartman des at deshartman.com
Thu Apr 9 17:45:14 PDT 2009


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/ed529084/attachment.html 


More information about the antlr-interest mailing list