[antlr-interest] Beginner grammar question

Matt Palmer mattpalms at gmail.com
Fri Sep 12 02:37:06 PDT 2008


Hi Fabian,

this is a classic problem - it's specifically mentioned in the ANTLR
reference book.   There's more than one way of solving it, but where you
have keywords that are the same as IDs, you can use predicates to do it.
Basically, make bar a NAME too, but put a predicate before it to see if it's
"bar".  Then the lexer will just recognise NAME tokens, but the parser can
switch to a bar rule if necessary:

grammar test;

list     :    (element)+
    ;

element  :   bar NAME
    ;

bar      :   {input.LT(1).getText().equals("bar")}? NAME
    ;

BAR      :   'bar'
    ;

NAME
    :     ('a'..'z' | 'A'..'Z')
          ('a'..'z' | 'A'..'Z' | '0'..'9' | '_')*
    ;

WS
    :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
    ;

Matt

On Fri, Sep 12, 2008 at 8:39 AM, Fabian Baboschi <fabian20ro at gmail.com>wrote:

> Hi,
> I have a question about the following grammar:
>
> grammar test;
>
> list     :    (element)+
>     ;
>
> element    :   BAR NAME
>     ;
>
> BAR    :     'bar'
>     ;
>
> NAME
>     :     ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
>     ;
>
> WS
>     :  (' '|'\r'|'\t'|'\u000C'|'\n') {$channel=HIDDEN;}
>     ;
>
> I tried to use the following input: bar bar
> and I get errors because the parser doesn't recognize that the second bar
> should be treated as a name.
> I tried to change the grammar or options but without success. What am I
> missing?
>
> If it matters I'm using antlrworks-1.2 and antlr-3.1.
>
> Thank you,
> Fabian
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080912/85742e0f/attachment.html 


More information about the antlr-interest mailing list