开发者

ANTLR reg. expression error

Can you see any error with the following regular expression? I am defining it in Antlr 3.4, but it accepts arguments like $one, £one although it shouldn't. However, it doesn't accept o£ne, 开发者_高级运维or o$ne.

ID : ('a'..'z'|'A'..'Z'|' _ ') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;

Thanks in advance.


No, ID does not match "$one" or "£one" as you can see:

T.g

grammar T;

parse : ID EOF;
ID    : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;

Main.java

import org.antlr.runtime.*;

public class Main {
  public static void main(String[] args) throws Exception {
    TLexer lexer = new TLexer(new ANTLRStringStream("$one"));
    TParser parser = new TParser(new CommonTokenStream(lexer));
    parser.parse();
  }
}

Run the test

java -cp antlr-3.4-complete.jar org.antlr.Tool T.g
javac -cp antlr-3.4-complete.jar *.java
java -cp .;antlr-3.4-complete.jar Main

will produce the following error:

line 1:0 mismatched character '$' expecting set null

Note that both antlr-3.4-complete.jar and antlr-3.4-complete-no-antlrv2.jar produce the same error (or any other 3.x version, for that matter).

My guess is that you're using ANTLRWorks and you didn't notice the error messages on the console tab (they're there!).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜