[antlr-interest] distinguish "3 + 4" from "3 +4"

Andreas Liebig liebigandreas at yahoo.com
Wed Oct 5 05:17:00 PDT 2011


Hello Bart,
I don't need to glue the + or - to the NUMBER token. I could as well recognize it later in the parser.

I tried your grammar, but it doesn't make any difference between "3 + 4" and "3 +4". Both are interpreted as an "add" rule of "3" and "4".
What I need is another "sequence" rule which interpretes e.g. "3 +4 -5" as the sequence (3 4 -5) or e.g. "3 +4" as (3 4).

Regards,
Andreas



________________________________
From: Bart Kiers <bkiers at gmail.com>
To: Andreas Liebig <liebigandreas at yahoo.com>
Cc: "antlr-interest at antlr.org" <antlr-interest at antlr.org>
Sent: Wednesday, October 5, 2011 1:43 PM
Subject: Re: [antlr-interest] distinguish "3 + 4" from "3 +4"


Hi Andreas,

I think it's a bad idea to "glue" a '+' (or '-') to a NUMBER token. If I were you, I'd resolve that in a unary-parser rule:

expression 
>  :  add
>  ;
>
>
>add 
>  :  multiplication (('+' | '-') multiplication)*
>  ;
>
>
>multiplication 
>  :  unary (('*' | '/') unary)*
>  ;
>
>
>unary
>  :  '+' atom
>  |  '-' atom
>  |  atom
>  ;
>
>
>atom
>  :  NUMBER 
>  |  '(' expression ')'
>  ;


The grammar above would handle expressions like:

+ 1 + -5
>^     ^
>|     |
>+-----+---- unary '+' and '-'

Regards,

Bart.



On Wed, Oct 5, 2011 at 1:14 PM, Andreas Liebig <liebigandreas at yahoo.com> wrote:

Hello,
>I am not very experienced with ANTLR, and I would like to ask for some ideas how to solve this task:
>
>I have to distinguish input streams like
>"3 + 4" (parsed as three tokens NUMBER PLUS NUMBER) from
>"3 +4" (parsed as NUMBER NUMBER, because the + is part of the number +4).
>
>I would like to ignore whitespace in general using the "$channel=HIDDEN;" syntax. But only in this situation whitespace does matter.
>Can you guide me to a good explanation of a possible solution?
>
>Thanks
>Andreas
>
>List: http://www.antlr.org/mailman/listinfo/antlr-interest
>Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list