需要 Antlr 多余的谓词吗? [英] Antlr superfluous Predicate required?
问题描述
我有一个文件,我想忽略其中的一部分.在 Lexer 中,我使用门控语义谓词来避免为文件的无趣部分创建标记.我的规则类似于以下.
A: {!ignore}?=>'一种';START_忽略: 'foo' {ignore = true;跳过();};END_忽略:'oof'{忽略=假;跳过();};忽略:{忽略}?=>.{跳过();};
但是,除非我将 START 和 END 更改为也使用语义谓词(如下所示),否则它不起作用..
A: {!ignore}?=>'一种';START_忽略:{真}?=>'foo' {忽略 = 真;跳过();};END_忽略:{真}?=>'oof' {忽略 = 假;跳过();};忽略:{忽略}?=>.{跳过();};
为什么我必须添加谓词?
我使用的是 antlr-3.4
为什么我必须添加谓词?
你没有.至少,没有使用 ANTLR v3.3.我不知道如何你正在测试,但不要使用 ANTLRWorks 的解释器或 Eclipse ANTLR IDE 插件.总是从命令行做一个小测试.
语法T;@parser::members {public static void main(String[] args) 抛出异常 {TLexer lexer = new TLexer(new ANTLRStringStream("A foo A B C oof A"));TParser parser = new TParser(new CommonTokenStream(lexer));parser.parse();}}@lexer::members {私有布尔忽略 = 假;}解析:(t=.{System.out.printf("[\%02d] type=\%s text='\%s'\n", $t.getCharPositionInLine(), tokenNames[$t.type], $t.text);})* EOF;一种: {!ignore}?=>'一种';START_忽略: 'foo' {ignore = true;跳过();};END_忽略:'oof'{忽略=假;跳过();};忽略:{忽略}?=>.{跳过();};空间: ' ' {跳过();};
像这样运行:
java -cp antlr-3.3.jar org.antlr.Tool T.gjavac -cp antlr-3.3.jar *.javajava -cp .:antlr-3.3.jar TParser
将打印以下内容:
[00] type=A text='A'[16] type=A text='A'
即:从输入 "A foo ABC oof A"
如下:"foo ABC oof"
是 skip
ped.>
I have a file where I want to ignore parts of it. In the Lexer I use gated semantic predicates to avoid creating tokens for the uninteresting part of the file. My rules are similar to the following.
A
: {!ignore}?=> 'A'
;
START_IGNORE
: 'foo' {ignore = true; skip();}
;
END_IGNORE
: 'oof' {ignore = false; skip();}
;
IGNORE
: {ignore}?=> . {skip();}
;
However unless I change START and END to also use semantic predicates (as below) it does not work..
A
: {!ignore}?=> 'A'
;
START_IGNORE
: {true}?=> 'foo' {ignore = true; skip();}
;
END_IGNORE
: {true}?=> 'oof' {ignore = false; skip();}
;
IGNORE
: {ignore}?=> . {skip();}
;
Why do I have to add the predicates?
EDIT: I am using antlr-3.4
Why do I have to add the predicates?
You don't. At least, not using ANTLR v3.3. I don't know how exactly you're testing, but don't use ANTLRWorks' interpreter or the Eclipse ANTLR IDE plugin. Always do a little test from the command line.
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();}
;
Run it like this:
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
which will print the following:
[00] type=A text='A' [16] type=A text='A'
I.e.: from the input "A foo A B C oof A"
the following: "foo A B C oof"
is skip
ped.
这篇关于需要 Antlr 多余的谓词吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!