[antlr-interest] A simple question in my antlr code. Is it a antlr code bug or some thing wrong with my code

André van der Merwe AndrevdM at pyrogenesis.co.za
Thu Jun 26 06:33:54 PDT 2008


I'm also fairly new to ANTLR so I definitely stand to be corrected...

You are implicitly creating tokens in your rules. Your columnKey rule is going to match the numbers 5, 7, 8, 10 etc. DIGIT will then match any integer that *has not already been matched* this is why you are getting the error.


Consider this grammar based on yours

        -----------------------------------
        yearKey : year_two_digit;
        swallow : '10';

        year_two_digit : number;
        number: DIG+;

        DIG: ('0'..'9');
        -----------------------------------


Here DIG will never match 10 as it was already matched by swallow. Rules higher up get run before ones lower down. If you generate the code for this grammar you will be able to see what is going on

        -----------------------------------
      public void mTokens() throws RecognitionException {
        int alt1=2;
        int LA1_0 = input.LA(1);

        if ( (LA1_0=='1') ) {
            int LA1_1 = input.LA(2);

            if ( (LA1_1=='0') ) {
                alt1=1;
            }
            else {
                alt1=2;}
        }

        switch (alt1) {
           ...
        -----------------------------------

The if block is a DFA that is looking for 10 and then returning a swallow token (alt1=1) else it returns a DIG (alt1=2)


If I can make two suggestions; get used to looking at the generated code you can learn a lot about how ANTLR works from it. Secondly consider getting the ANTLR book it really is fantastic.


Andre


>
>From: Gnanasegaran Vinayagamoorthy
>
>I am a new antlr user. I have made a simple code to decode a string.
>...


More information about the antlr-interest mailing list