简体   繁体   English

Antlr语法意外行为

[英]Antlr grammar unpredicted behavior

I've begun experimenting with ANTLR3 today. 我今天开始尝试使用ANTLR3。 There seems to be a discrepency in the expressions that I use. 我使用的表达式似乎存在差异。

I want my class name to start with a capital letter, followed by mixed case letters and numbers. 我希望班级名称以大写字母开头,然后是大小写混合的字母和数字。 For instance, Car is valid, 8Car is invalid. 例如, Car有效, 8Car无效。

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

This works fine when I test it individually. 当我单独测试它时,这很好。 However when I use it in the following rule, 但是,当我在以下规则中使用它时,

model
    : '~model' CLASS_NAME model_block
    ;

However, the CLASS_NAME begins to pick up class names beginning with numbers as well. 但是, CLASS_NAME开始选择以数字开头的班级名称。 In this case, ANTLR picks up Car , 8Car or even #Car as valid tokens. 在这种情况下,ANTLR会选择Car8Car甚至#Car作为有效令牌。 I'm missing something silly. 我想念一些愚蠢的东西。 Any pointers would be appreciated. 任何指针将不胜感激。 Thanks. 谢谢。

CLASS_NAME will not match 8Car or #Car . CLASS_NAME8Car#Car不匹配。 You're probably using ANTLRWorks' interpreter (or the Eclipse plugin, which uses the same interpreter), which is printing errors on a UI tab you're not aware of, and displaying the incorrect chars in the tokens. 您可能正在使用ANTLRWorks的解释器(或使用相同解释器的Eclipse插件),这会在您不知道的UI选项卡上打印错误,并在令牌中显示不正确的字符。 Use ANTLRWorks' debugger instead, or write a small test class yourself: 请改用ANTLRWorks的调试器,或者自己编写一个小的测试类:

Tg TG

grammar T;

parse : CLASS_NAME EOF;

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

Main.java Main.java

import org.antlr.runtime.*;

public class Main {

  public static void main(String[] args) throws Exception {

    TLexer lexer = new TLexer(new ANTLRStringStream("8Car"));
    TParser parser = new TParser(new CommonTokenStream(lexer));
    parser.parse();  
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM