[antlr-interest] Grammar Problem

Johannes Luber jaluber at gmx.de
Thu Aug 7 10:47:58 PDT 2008


Henrique schrieb:
> Hi guys!
> I'm having some trouble with my grammar for parsing CFA files... As I 
> posted before, I'm trying to convert their Yacc grammar to ANTLR and now 
> I'm having some trouble. I suspect it's the left recursions or rather 
> the way I got ridden of them.
> The grammar is attached and can also be seen here 
> <http://hjrnunes.googlepages.com/antlrgrammar>.
> 
> here's the rules I'm having trouble with:
> 
> rule    :    RULE USER_STRING LEFTBRACKET buncha_replacements? RIGHTBRACKET
>     |    RULE USER_STRING USER_RATIONAL LEFTBRACKET buncha_replacements? 
> RIGHTBRACKET
>     ;
> 
> buncha_replacements
>     :    (replacement)+
>     ;
>    
> replacement
>     :    USER_STRING modification
>     |    USER_STRING STAR modification USER_STRING modification
>     ;
> 
> Here's an example of what it should parse:
> 
> rule Foo {
>     CIRCLE { b 1 }
>     CIRCLE {}
> }
> 
> Now I'm getting a MismatchedTokenException: mismatched input 'CIRCLE' 
> expecting RIGHTBRACKET in the first alternative.
> Here are the relevant original YACC rules:
> 
> rule:
>         RULE USER_STRING LEFTBRACKET buncha_replacements RIGHTBRACKET
>         |
>         RULE USER_STRING USER_RATIONAL LEFTBRACKET buncha_replacements 
> RIGHTBRACKET
>         ;
> 
> buncha_replacements:
>         buncha_replacements replacement
>         |
>         ;
> 
> The strange thing - at least for me is that the background rule that is 
> quite similar works like a charm:
> 
> background
>     :    BACKGROUND LEFTBRACKET buncha_color_adjustments? RIGHTBRACKET
>     ;
> 
> buncha_color_adjustments
>     :    (color_adjustment)+
>     ;
> 
> It should work almost similarly...
> What would be the right way to convert such left recursive rules into 
> something appropriate for ANTLR?
> I hope you guys can shed some insight on this... I'm somewhat of a 
> newbie concerning parsing and ANTLR.
> Thanks!
> 
> Henrique Nunes
> 
Did you use the approach outlined in 
<http://antlr.org/wiki/display/ANTLR3/Left-Recursion+Removal>?

Johannes


More information about the antlr-interest mailing list