[antlr-interest] Grammar Problem

Gavin Lambert antlr at mirality.co.nz
Thu Aug 7 14:39:05 PDT 2008


At 06:20 8/08/2008, Henrique wrote:
>rule    : RULE USER_STRING (USER_RATIONAL)? LEFTBRACKET 
>buncha_replacements? RIGHTBRACKET;
>
>buncha_replacements
>     :    (replacement)+
>     ;
>
>replacement
>     :    USER_STRING modification
>     |    USER_STRING STAR modification USER_STRING modification
>     ;
>
>modification
>     :    LEFTBRACKET buncha_adjustments? RIGHTBRACKET
>     |    LEFTBRACE buncha_adjustments? RIGHTBRACE
>     ;
>
>buncha_adjustments
>     :    (adjustment)+
>     ;

There is one remaining point of left-side ambiguity -- both alts 
in the 'replacement' rule start with the same token.  ANTLR should 
be able to cope with this just fine, but it wouldn't hurt to help 
it out a little:

replacement
     :    USER_STRING
          (    modification
          |    STAR modification USER_STRING modification
          )
     ;

Also: are 'buncha_replacements' and 'buncha_adjustments' *always* 
optional?  If so, then you could replace the + in those rules with 
a * and then remove all the ?s.  Shouldn't make any actual 
difference to parsing, but you might find it tidier.

But getting back to the mismatch errors you're getting -- the most 
likely candidate here is that the input token stream isn't what 
you're expecting.  Try dumping the tokens that the lexer is 
generating and make sure that they make sense, first of all. 



More information about the antlr-interest mailing list