Antlr多余的Predicate需要吗?

时间:2012-07-09 18:26:42

标签: antlr predicate

我有一个文件,我想忽略它的一部分。在Lexer中,我使用门控语义谓词来避免为文件中不感兴趣的部分创建令牌。我的规则类似于以下内容。

A 
: {!ignore}?=> 'A' 
;
START_IGNORE
: 'foo' {ignore = true; skip();}
;
END_IGNORE
: 'oof' {ignore = false; skip();}
;
IGNORE
: {ignore}?=> . {skip();}
;    

然而,除非我改变START和END以使用语义谓词(如下所示),否则它不起作用..

A 
: {!ignore}?=> 'A' 
;
START_IGNORE
: {true}?=> 'foo' {ignore = true; skip();}
;
END_IGNORE
: {true}?=> 'oof' {ignore = false; skip();}
;    
IGNORE
: {ignore}?=> . {skip();}
;  

为什么我必须添加谓词?

编辑:我正在使用antlr-3.4

1 个答案:

答案 0 :(得分:1)

  

为什么我必须添加谓词?

你没有。至少,不使用ANTLR v3.3。我不知道你正在测试如何,但不要使用ANTLRWorks的解释器或Eclipse ANTLR IDE插件。总是从命令行做一点测试。

grammar T;

@parser::members {
  public static void main(String[] args) throws Exception {
    TLexer lexer = new TLexer(new ANTLRStringStream("A foo A B C oof A"));
    TParser parser = new TParser(new CommonTokenStream(lexer));
    parser.parse();
  }
}

@lexer::members {
  private boolean ignore = false;
}

parse
 : (t=. 
     {System.out.printf("[\%02d] type=\%s text='\%s'\n", $t.getCharPositionInLine(), tokenNames[$t.type], $t.text);}
   )* EOF
 ;

A 
 : {!ignore}?=> 'A' 
 ;

START_IGNORE
 : 'foo' {ignore = true; skip();}
 ;

END_IGNORE
 : 'oof' {ignore = false; skip();}
 ;

IGNORE
 : {ignore}?=> . {skip();}
 ;    

SPACE
 : ' ' {skip();}
 ;

像这样运行:

java -cp antlr-3.3.jar org.antlr.Tool T.g
javac -cp antlr-3.3.jar *.java
java -cp .:antlr-3.3.jar TParser

将打印以下内容:

[00] type=A text='A'
[16] type=A text='A'

I.e。:来自输入"A foo A B C oof A"以下内容:"foo A B C oof"skip ped。

相关问题