[antlr-interest] Interesting problem with labels in V4b1

Mike Cargal mike at cargal.net
Sat Sep 22 14:56:01 PDT 2012


This:

grammar Foo;
expr        :   '(' expr ')'           # parenExpr             
            |   '%' expr               # pctExpr      
            !   '^' expr               # expnExpr          
            |   expr ('*'|'/') expr    # multExpr          
            |   expr ('+'|'-') expr    # addExpr           
            ;
gives me the following errors…
error(50): Foo.g4:4:16: mismatched input ''^'' expecting SEMI while matching a rule
error(50): Foo.g4:4:37: '#' came as a complete surprise to me while matching rule preamble


This:

grammar Foo;
expr        :   '(' expr ')'           # parenExpr             
            |   '%' expr               # pctExpr      
//            !   '^' expr               # expnExpr          
            |   expr ('*'|'/') expr    # multExpr          
            |   expr ('+'|'-') expr    # addExpr           
            ; 

generates without error

and this:

grammar Foo;
expr        :   '(' expr ')'                        
            |   '%' expr                     
            !   '^' expr                         
            |   expr ('*'|'/') expr              
            |   expr ('+'|'-') expr               
            ;

generates without error

all compiled using:
java -jar /usr/local/lib/antlr-4.0b1-complete.jar -visitor Foo.g4

Somehow the exponentiation expression is a problem if and only if I'm using labels.


More information about the antlr-interest mailing list