[antlr-interest] Viable Alternative Rule?

John B. Brodie jbb at acm.org
Sun Aug 29 08:53:48 PDT 2010


Greetings!

On Sun, 2010-08-29 at 07:40 -0700, Ted Hoise wrote:
> VERY simple example
> 
> rule : 'H'  ;
> 
> When I test this very simple rule, it works (in Antlrworks and Eclipse) of course for input 'H', but it also works for input 'fH', resulting in a token 'fH'.  I do I suppress this behavior:  it seems like an error should occur.
> 

Unable to reproduce.

When I run your examples from the command line I do indeed get an error
on the second example.

Maybe there is something unexpected/unusual about your test rig? (i have
attached the grammar i used in my test so you can compare it to yours)

Hope this helps
   -jbb

-------------- next part --------------
grammar Test;

options {
   output = AST;
   ASTLabelType = CommonTree;
}

@members {
   private static final String [] x = new String[] {
      "H", "fH"
   };

   public static void main(String [] args) {
      for( int i = 0; i < x.length; ++i ) {
         try {
            System.out.println("about to parse:`"+x[i]+"`");
            TestLexer lexer = new TestLexer(new ANTLRStringStream(x[i]));
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            TestParser parser = new TestParser(tokens);
            TestParser.rule_return p_result = parser.rule();

            CommonTree ast = p_result.tree;
            if( ast == null ) {
               System.out.println("resultant tree: is NULL");
            } else {
               System.out.println("resultant tree: " + ast.toStringTree());
            }
            System.out.println();
         } catch(Exception e) {
            e.printStackTrace();
         }
      }
   }
}

rule : 'H' ;


More information about the antlr-interest mailing list