在使用parse听众玩了一下后,我发现了一个我没想到的行为。 我的问题是,我的期望是错的,这个行为是否需要或者是一个错误?如果需要这种行为,请解释一下。
这里是示例语法:
grammar Labeled;
file: stmt;
stmt: stmt '+' stmt # Add
| stmt '*' stmt # Mult
| FLOAT # Value
| INTEGER # Value
;
FLOAT: '-'? DIGIT* '.' DIGIT+;
INTEGER: '-'? DIGIT+;
COMMENT: (COMMENT_LINE | COMMENT_BLOCK) -> skip;
WS: [ \t\r\n] -> skip;
fragment
DIGIT: [0-9];
COMMENT_LINE: '//' ~'\n'*;
COMMENT_BLOCK: '/*' .*? '*/';`
示例监听器:
import org.antlr.v4.runtime.misc.NotNull;
import java.util.HashMap;
import java.util.Map;
public class TestListener extends LabeledBaseListener {
public static final String ALL_KEY = "All";
public static final String MULT_KEY = "Mult";
public static final String ADD_KEY = "Add";
public static final String VALUE_KEY = "Value";
public static final String FILE_KEY = "File";
public Map<String, Integer> enterValues = new HashMap<>();
public Map<String, Integer> exitValues = new HashMap<>();
@Override
public void enterMult(@NotNull LabeledParser.MultContext ctx) {
addEnter(ALL_KEY);
addEnter(MULT_KEY);
}
@Override
public void exitMult(@NotNull LabeledParser.MultContext ctx) {
addExit(ALL_KEY);
addExit(MULT_KEY);
}
@Override
public void enterValue(@NotNull LabeledParser.ValueContext ctx) {
addEnter(ALL_KEY);
addEnter(VALUE_KEY);
}
@Override
public void exitValue(@NotNull LabeledParser.ValueContext ctx) {
addExit(ALL_KEY);
addExit(VALUE_KEY);
}
@Override
public void enterFile(@NotNull LabeledParser.FileContext ctx) {
addEnter(ALL_KEY);
addEnter(FILE_KEY);
}
@Override
public void exitFile(@NotNull LabeledParser.FileContext ctx) {
addExit(ALL_KEY);
addExit(FILE_KEY);
}
@Override
public void enterAdd(@NotNull LabeledParser.AddContext ctx) {
addEnter(ALL_KEY);
addEnter(ADD_KEY);
}
@Override
public void exitAdd(@NotNull LabeledParser.AddContext ctx) {
addExit(ALL_KEY);
addExit(ADD_KEY);
}
// region map helper
private static void addValue(Map<String, Integer> valueMap, String name) {
if(valueMap.containsKey(name)) {
valueMap.put(name, valueMap.get(name) + 1);
} else {
valueMap.put(name, 1);
}
}
private void addEnter(String name) {
addValue(enterValues, name);
}
private void addExit(String name) {
addValue(exitValues, name);
}
// endregion
}
主要班级:
import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
String filePath = args[0];
ANTLRInputStream input = new ANTLRFileStream(filePath);
LabeledLexer lexer = new LabeledLexer(input);
CommonTokenStream token = new CommonTokenStream(lexer);
LabeledParser parser = new LabeledParser(token);
TestListener testListener = new TestListener();
parser.addParseListener(testListener);
parser.file();
System.out.println("Enter Values:");
System.out.println(getMapString(testListener.enterValues));
System.out.println("Exit Values:");
System.out.println(getMapString(testListener.exitValues));
System.out.println("End");
}
private static String getMapString(Map<?, ?> map) {
StringBuffer buffer = new StringBuffer();
for(Map.Entry<?, ?> curEntry: map.entrySet()) {
buffer.append("Key: " + curEntry.getKey() + "\tValue: " + curEntry.getValue() + "\n");
}
String result = buffer.toString();
return result;
}
}
现在,我执行包含内容的文件:
-4 + 8
输出将是:
Enter Values:
Key: File Value: 1
Key: Add Value: 1
Key: All Value: 2
Exit Values:
Key: Value Value: 2
Key: File Value: 1
Key: Add Value: 1
Key: All Value: 4
End
但我希望这个输出:
Enter Values:
Key: Value Value: 2
Key: File Value: 1
Key: Add Value: 1
Key: All Value: 4
Exit Values:
Key: Value Value: 2
Key: File Value: 1
Key: Add Value: 1
Key: All Value: 4
End
因为你可以看到从不调用enterValue()方法。经过一些测试之后,如果规则的备选方案中只有一个令牌/规则,则不会调用 enterXXX()方法。
提前致谢!
答案 0 :(得分:1)
是的,这种行为是预期的(或者至少是允许的)。行为本身及其基本原理包含在您使用的addParseListener
方法的文档中: