[antlr-interest] ANTLR 3.0b4 DIGIT-INTEGER grammar question

Ron Blaschke mailing-lists at rblasch.org
Sun Oct 22 08:08:36 PDT 2006


I have a simple question, and it may well be something stupid.  But I
just can't put my finger on what I'm missing.  Any help is greatly
appreciated.

Consider this grammar.
----
grammar TestParser;
options {
    output = AST;
    ASTLabelType = CommonTree;
}

number
    :    INTEGER EOF!
    ;

protected
DIGIT
    :   '0'..'9'
    ;

INTEGER
    :   (DIGIT)+
    ;

WHITESPACE
    :      (' '
    |       '\t'
    |       '\n'
    |       '\r')
            { channel=99; }
    ;
----

And this simple test program.
----
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;

public class Test {
    public static void main(final String[] args) throws Exception {
        final TestParserLexer lexer = new TestParserLexer(
                                      new ANTLRFileStream("test.txt"));
        final CommonTokenStream tokens = new CommonTokenStream(lexer);
        final TestParser parser = new TestParser(tokens);

        final TestParser.number_return result = parser.number();
        final CommonTree testTree = (CommonTree) result.getTree();

        System.out.println(testTree.toStringTree());
    }
}
----

If I put "425" into test.txt I get:

    >echo 425 >test.txt
    >java -cp antlr-3.0b4.jar;. Test
    425

with just a single digit, e.g. "4", I get:

    >echo 4 >test.txt
    >java -cp antlr-3.0b4.jar;. Test
    [number]: line 1:0 mismatched token: [@0,0:0='4',<5>,1:0]; expecting
type INTEGER
nil

Feels like the parser doesn't know it should go from DIGIT to INTEGER.
Removing the DIGIT rule and replacing INTEGER with

    INTEGER
        :   ('0'..'9')+
        ;

yields:

    >echo 425 >test.txt
    >java -cp antlr-3.0b4.jar;. Test
    425

    >echo 4 >test.txt
    >java -cp antlr-3.0b4.jar;. Test
    4

Any thoughts?

Thanks,
Ron


More information about the antlr-interest mailing list