[antlr-interest] floating point numbers and ranges

Gerhard Mennen gerhard at tasking.nl
Thu Mar 31 01:42:23 PST 2005


Hello All,

I am new to antlr (and java) and for a project a ran into a problem
similar to the "floating point numbers and ranges" problem that is
described in the antlr documentation.
So I tried that example and all looked well, but that changed when
I specified "k = 2;" in the lexer options part.
Then it gives an error "exception: line 3:3: unexpected char: '.'"
when I try to process my example input.

Is it correct that that example gives an error when "k = some value;"
is added to the lexer options?
Is there another solution to solve this kind op problem that does
work with lookahead depth value.

Below added are files that I used (RangeAndReal.g, Main.java and
test.txt). Run with the following commands:
    java antlr.Tool RangeAndReal.g
    javac Main.java
    java Main < test.txt

Version string printed by first command:
    ANTLR Parser Generator   Version 2.7.5 (20050128)   1989-2005 jGuru.com

Output of third command without "k = 2;":
    INT..INT
    token REAL
    INT..INT
    INT..INT
    token REAL
    INT..INT
    plain old INT
    token REAL
    INT..INT
    plain old INT

Output with "k = 2;"
    INT..INT
    token REAL
    exception: line 3:3: unexpected char: '.'


Any help appreciated

Gerhard




RangeAndReal.g:

class PascalParser extends Parser;

main
    :
        (prog)* EOF
    ;

prog
    :
        {
            Boolean range = false;
        }
        INT
        (    (RANGE INT
            { range = true; System.out.println("INT..INT"); })?
            { if (range == false) { System.out.println("plain old INT"); } }
        )
    |
        REAL    { System.out.println("token REAL"); }
    ;

class PascalLexer extends Lexer;
options
{
/*
*/
	k = 2;
	charVocabulary = '\3'..'\377';
}

WS
    :    (' ' | '\t' | '\n' { newline(); } | '\r')+
        {
            $setType(Token.SKIP);
        }
    ;

protected
INT
    :
        ('0'..'9')+
    ;

protected
REAL
    :
        INT '.' INT
    ;

RANGE
    :
        ".."
    ;

RANGE_OR_INT
    :
        ( INT ".." )    => INT      { $setType(INT); }
    |
        ( INT '.' )     => REAL     { $setType(REAL); }
    |
        INT                         { $setType(INT); }
    ;


Main.java:

import java.io.*;

class Main {
	public static void main(String[] args) {
		try {
			DataInputStream input = new DataInputStream(System.in);

			// attach lexer to the input stream
			PascalLexer lexer = new PascalLexer(input);

			// Create parser attached to lexer
			PascalParser parser = new PascalParser(lexer);

			// start up the parser by calling the rule
			// at which you want to begin parsing.
			parser.main();
		}
		catch (Exception e) {
			System.err.println("exception: "+e);
		}
	}
}

text.txt:

1 .. 4
1.4
1..4

123 .. 456
123.456
123..456

123456

333.666

333..666

998877



More information about the antlr-interest mailing list