[antlr-interest] please help on lexer rules antlr v3

John B. Brodie jbb at acm.org
Fri Sep 29 09:06:22 PDT 2006


>So even with the "--axi.reject"  test string the grammar I have sent in my 
>previous mail results in an EarlyExitException...

I do not get any exceptions. So maybe there is something missing in my Main?
Here is my Main.java. I use ANTLR 3.0b4. Both your previous grammar and the
attached (simpler to me) grammar just silently process the input. Also
attached is the bash shell script I use to build/compile/run.

Is any of this different from what you are using?

//--- Main.java ----
import java.io.*;
import java.util.*;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.*;

class Main {

   public static void main(String[] args) {
      try {
         TestParserLexer lexer =
            new TestParserLexer(new ANTLRStringStream("--axi.reject"));
         CommonTokenStream tokens = new CommonTokenStream(lexer);
         TestParser parser = new TestParser(tokens);
         parser.statement();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
}
//--- Main.java ----

//--- TestParser.g ----
grammar TestParser;

statement: ( directive )+ EOF;

directive: SL_COMMENT;


fragment ANYTHING_2_EOL: ( ~('\n'|'\r') )* ( '\n'|'\r'('\n')? );

// Directive or Single-line comment
// (comment is discarded, directive is kept)
SL_COMMENT
    :  '--' 
        ( ( ('\t'|' ')* ( 'axi.locate' | 'axi.reject' ) )
        | ( ANYTHING_2_EOL { channel=99; } )
        );

WS : ( '\t' | ' ' | '\r' | '\n' )+ { channel=99; };
//--- TestParser.g ----

//--- make.sh ----
#!/bin/bash

# directory containing the unpacked ANTLR v3 distribution:
V3=/usr/local/antlr-3.0b4/lib/

LIBS=${V3}/antlr-2.7.6.jar:${V3}/antlr-3.0b4.jar:${V3}/stringtemplate-2.3b9.jar
CP=${LIBS}:${CLASSPATH}

java -cp ${CP} org.antlr.Tool TestParser.g

javac -cp ${CP} *.java
java -cp ${CP} Main
//--- make.sh ----


More information about the antlr-interest mailing list