[antlr-interest] Noob needs help with: warning "Decision can match input such as X... using multiple alternatives"
Norman Dunbar
Norman at dunbar-it.co.uk
Mon Apr 16 12:59:06 PDT 2012
Evening Adam,
as one noob to another then, here's what I think. I noticed you haven't
had any replies yet, so idf I put something obviously incorrect, I'm
sure we'll get jumped on by those who know better than I do! Let's hope
so anyway!
> I'm learning antlr in hopes of creating a DSL. To this end I need to
> define a rule for nested "expressions" like:
>
> (1+2)*((3-4)/5)+7
>
Does this help? It's not mine, it's one of the ANTLR examples:
grammar SimpleCalc;
tokens {
PLUS = '+' ;
MINUS = '-' ;
MULT = '*' ;
DIV = '/' ;
}
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
expr : term ( ( PLUS | MINUS ) term )* ;
term : factor ( ( MULT | DIV ) factor )* ;
factor : NUMBER ;
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
NUMBER : (DIGIT)+ ;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel =
HIDDEN; } ;
fragment DIGIT : '0'..'9' ;
Now, I'm not convinced that the above is complete, yet, but as a simple
grammar it's fine. I'd be thinking about a FACTOR being also able to be
a '(' followed by an expr followed by a ')'. Something like:
factor : NUMBER |
LPAREN expr RPAREN;
Where LPAREN and RPAREN are defined in the tokens table.
HTH
Cheers,
Norm.
--
Norman Dunbar
Dunbar IT Consultants Ltd
Registered address:
Thorpe House
61 Richardshaw Lane
Pudsey
West Yorkshire
United Kingdom
LS28 7EL
Company Number: 05132767
More information about the antlr-interest
mailing list