[antlr-interest] Smalltalk-like grammar? Easy! Decimal number? Help!
Jim Idle
jimi at temporal-wave.com
Thu Nov 1 10:16:21 PDT 2007
See much discussion of this issue over the last 2 or 3 weeks, but you need a
predicate on your number rule, and your DECIMAL and DIGIT and LETTER rules
should be fragments (though you probably don't need them as separate rules
at all really):
NUMBER: ('0'..'9')+ ( ('.' '0'..'9')=> ('.' ('0'..'9')+)
|
)
;
fragment
LETTER
Etc...
Hope that helps :-)
Jim
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Kevin Twidle
> Sent: Thursday, November 01, 2007 8:59 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Smalltalk-like grammar? Easy! Decimal number?
> Help!
>
> Hi,
>
> I love ANTLR! I have a quite sophisticated Smalltalk-like grammar in
> ANTLR using an AST which works beautifully. I have decided to allow
> floating point numbers and have tried adding them to the grammar.
> Now, Smalltalk uses '.' as a statement separator, numbers have a '.'
> in them - uh-oh.
>
> A number should have the form:
>
> 12 or 12.34 but not 12.
>
> I want to be able to parse
>
> 13.
> 13.word.
> 14.0.13.
>
> to get 13,13,word,14.0,13
>
> all I get is
>
> line 1:3 required (...)+ loop did not match anything at character '\n'
> line 2:3 required (...)+ loop did not match anything at character 'w'
> recoverFromMismatchedToken
> BR.recoverFromMismatchedToken
> line 3:4 mismatched input '.13' expecting EOF
>
> with tokens ord 14.0
>
> I have simplified my problem to the following grammar. The problem
> is that DECIMAL always matches the first '.' and then fails (I ran
> through the code) it never says DECIMAL is not there, it must be a
> statement separator! I have tried the greedy option but then it
> never matches the DECIMAL. I have tried reordering, fragments,
> greedy and now this mailing list!
>
> DECIMAL is optional, why does it fail?
>
> Any help really appreciated!
>
> Kevin
>
> grammar Number;
> options {output = AST;}
>
> start : statement ( DOT statement? )+ EOF;
>
> statement : WORD | NUMBER;
>
> WORD : LETTER (LETTER | DIGIT)+;
>
> NUMBER : DIGIT+ DECIMAL?;
>
> DECIMAL : DOT DIGIT+;
> DOT : '.';
> DIGIT : '0'..'9';
> LETTER : 'a'..'z' | 'A'..'Z';
> WS :
> (' '
> | '\t'
> | '\r' '\n'
> | '\n'
> ) +
> { $channel=HIDDEN; }
> ;
More information about the antlr-interest
mailing list