[antlr-interest] Smalltalk-like grammar? Easy! Decimal number? Help!

Kevin Twidle kpt at doc.ic.ac.uk
Thu Nov 1 08:59:27 PDT 2007


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