开发者

why does 'a'..'z' in ANTLR match wildcards like $ or £

When I run the following grammer:

test : WORD+;

WORD : ('a'..'z')+;
WS : ' '+ {$channel = HIDDEN;};

and I give the input "?test" why does antlr accept this as valid input? I tho开发者_如何学JAVAught the ('a'..'z') would only match characters within the lowercase alphabet?


ANTLR does produce an error when parsing the input string ?test with the grammar you posted. As is usually the case, the error lies with the tool being used around ANTLR (I see it happen a lot with ANTLRWorks as well, unfortunately!).

To test it yourself (properly), create a file Test.g:

grammar Test;

test : WORD+;

WORD : ('a'..'z')+;

WS   : ' '+ {$channel = HIDDEN;};

and a file Main.java:

import org.antlr.runtime.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ANTLRStringStream in = new ANTLRStringStream("?test");
        TestLexer lexer = new TestLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        TestParser parser = new TestParser(tokens);
        parser.test();
    }
}

and download a copy of the ANTLR 3.2 JAR in the same directory.

Now generate a lexer & parser:

java -cp antlr-3.2.jar org.antlr.Tool Test.g 

compile all Java source files:

javac -cp antlr-3.2.jar *.java

and run the Main class:

java -cp .:antlr-3.2.jar Main 

(replace the : with ; if you're on Windows!)

which will produce the following error message:

line 1:0 no viable alternative at character '?'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜