I am writing an ANTLR grammar to parse log files and ran into a problem. I simplified my grammar to reproduce the problem as follows:
stmt1: '[ ' elapse ': ' stmt2 ; stmt2: '[xxx' ; stmt3: ': [yyy' ; elapse : FLOAT; FLOAT : ('0'..'9')+ '.' ('0'..'9')* ;
When I used the following line to check grammar:
[ 98.9: [xxx
I got an error:
E:\work\antlr\output\__Test___input.txt line 1:9 mismatched character 'x' expecting 'y' E:\work\antlr\output\__Test___input.txt line 1:10 no viable alternative at character 'x' E:\work\antlr\output\__Test___input.txt line 1:11 no viable alternative at character 'x' E:\work\antlr\output\__Test___input.txt line 1:12 mismatched input '<EOF>' expecting ': '
But if I remove the ruel 'stmt3', the same line will be accepted.
I'm not sure what happened ...
Thanks for any advice!
Leon
Thanks from Bart. I tried to fix the grammar. I think the basic level, I have to eliminate all tokens. And I am adding a WS token to simplify the rule.
stmt1: '[' elapse ':' stmt2 ; stmt2: '[' 'xxx' ; stmt3: ':' '[' 'yyy' ; elapse : FLOAT; FLOAT : ('0'..'9')+ '.' ('0'..'9')* ; WS : (' ' |'\t' |'\n' |'\r' )+ {skip();} ;
source share