1
votes

I'm triying to use ANTLR with a kind of file where the value to retrieve can be any sequence of chars excluding { and }.

text = {Valid;String}
text = {Another"Valid"-String}

But now VALUE is matching the line from the begining:

line 1:0 mismatched input 'text = ' expecting 'text'

What I'm doing wrong? Should not match with TEXT first?

grammar Example;

example : (TEXT '=' '{' VALUE '}')+;

WS : [ \t\r\n]+ -> skip ;

TEXT : 'text';

VALUE : ~('{'|'}')+;
2

2 Answers

1
votes

As Terence (The ANTLR Guy) mentioned, the rule VALUE greedily matches text =. You could let the VALUE rule include the braces instead of matching them as separate tokens:

example : (TEXT '=' VALUE)+;

WS : [ \t\r\n]+ -> skip ;

TEXT : 'text';

VALUE : '{' ~('{'|'}')+ '}';
0
votes

I think it's because ANTLR 4 will try to match the longest string so "text ..." will match to VALUE.