[antlr-interest] lexing expression ('a'..'z')+ not matching single character input

John B. Brodie jbb at acm.org
Wed Dec 13 07:09:03 PST 2006


Greetings!

On Wed, 13 Dec 2006 11:03:29, Matt Harrison wrote (in part):
>Unfortunately, it doesn't. For some bizarre reason, ('a'..'z')+ 
>stubbornly refuses to match any single alphabetic character, regardless 
>of context; that is, I can call the rule 'substituent' below directly 
>with a single character of input and it doesn't match, nor will it match 
>if a single character 'substituent' occurs in the middle of a token stream.
>
>Perhaps a bug in ANTLR? Surely this has got to be due to something else 
>I am missing due to my inexperience with ANLTR, but I can't for the life 
>of me discern what.
>
>.....snipped.....

Your lexer works just fine for me for IDENTIFIER's.

I am using ANTLR 2.7.7.

I will attach my versions of the Main.java and the parser at the end
of this message.

I did change your lexer rule for CR from:
>CR
>       : ( '\r' '\n' )
>       | '\n'         {   newline(); $setType( Token.SKIP );  }
>       ;

to be:

CR : ( ( '\r' ('\n')? ) | '\n' ) { newline(); $setType( Token.SKIP ); } ;

observe that in your version of the rule the action binds to only the
second alternative. Thus if a '\r' is encountered it is not SKIP'ed.

Could this be your problem?

Hope this helps...
   -jbb


//.....begin FooBarParser fragment.....

class FooBarParser extends Parser;

program : substituent EOF;
substituent : IDENTIFIER (HYPHEN IDENTIFIER)* ;

//.....end FooBarParser fragment.....

//.....begin Main.java.....

import java.io.*;
import java.util.*;

import antlr.*;

class Main {
   private static final String [] x =
      new String[]{
         "n", "nn", "nnn",
         "n-n", "nn-n", "n-nnn",
         "N", "nN", "Nn", // these should be errors
         "PRO" // prove we can recognize at least one keyword
      };

   public static void main(String[] args) {

      for (int i=0; i < x.length; ++i) {
         System.out.format("%nabout to process:%s%n",x[i]);

         try {
            
            FooBarLexer lexer = new FooBarLexer(new StringReader(x[i]));
            FooBarParser parser = new FooBarParser(lexer);
            parser.program();

         } catch(Exception e) {
            System.err.format("%s%n", e);
            //e.printStackTrace();
         }
      }
   }
}

//.....end Main.java.....


More information about the antlr-interest mailing list