Reputation: 43
Here is a very simple grammar:
grammar test;
DIGIT : [0-9] ;
WS : [ \r\n\t]+ -> skip ;
value : DIGIT+ ;
Using antlr v4.0b3 under OSX and JAVA version 1.6
Compiling the grammar with the following commands:
antlr4 test.g4
javac *.java
I then used grun to test the grammar:
grun test value -tokens
And here is the result:
grun test value -tokens
12
[eof]
[@0,0:0='1',<1>,1:0]
[@1,1:1='2',<1>,1:1]
[@2,3:2='<EOF>',<-1>,2:0]
My question is: why do I get two tokens ('1' and '2') instead of one ('12')?
Thanks a lot for those who can help me!!
Cheers!
Guy
Upvotes: 3
Views: 338
Reputation: 5962
You have asked DIGIT to match a single digit at a time, which are then passed to the parser rule: value. I think what you want is really
value : INT ;
INT : [0-9]+ ;
:)
Terence
Upvotes: 4