[antlr-interest] [newbie] Precedence of rules

Johannes Luber jaluber at gmx.de
Tue Jul 8 11:18:59 PDT 2008


Robert Munteanu schrieb:
> Hi,
> 
> I am trying to build a larger ANTLR grammar, but I have one single
> point of error. It boils down to the following very simple grammar:
> 
> grammar Test;
> 
> options {
> 	language = Java;
> 	output = AST;
> }
> 
> startup:
>   first=STRING COMMA second=STRING
>   {
>     System.out.println($first.text + " and " + $second.text);
>   }
> ;
> 
> COMMA : ',';
> STRING : ('a'..'z'|'A'..'Z')+;
> STRING_WITH_COMMA : STRING (COMMA STRING)+ ;
> 
> I would like the input
> 
> hello,world
> 
> to be treated as STRING COMMA STRING and to get the output
> 
> hello and world
> 
> However, all I get is
> 
> BR.recoverFromMismatchedToken
> line 1:0 mismatched input 'hello,world' expecting STRING
> 
> I assume it's something very basic I have overlooked ( only possible
> conclusion after staring for about 4 hours at it ).
> 
> Can someone please point it out ?
> 
> Thank you,
> 
> Robert
> 

Instead STRING_WITH_COMMA write string_with_comma. Uppercase rules are 
lexer rules, lowercase rules parser rules.

Johannes


More information about the antlr-interest mailing list