如何使用ANTLR4解析嵌套的源文件-再试一次 [英] How can I parse nested source files with ANTLR4 - Trying one more time
问题描述
我在Terrence Parr的一篇文章中找到了该代码(如下所示),该代码显示了如何在ANTLR3 for Java中处理INCLUDE文件.我试图将其添加到与ANTLR4(带有C ++目标)一起使用的语法中,但是当我尝试生成解析器时,却出现了错误
I found the code (reproduced below) in an article from Terrence Parr showing how INCLUDE files could be handled in ANTLR3 for Java. I tried to add this to a grammar I use with ANTLR4 (with a C++ target) but when I tried to generate a parser, I got the errors
error(50): : syntax error: '^' came as a complete surprise to me
error(50): : syntax error: mismatched input '->' expecting SEMI while matching a rule
error(50): : syntax error: '^' came as a complete surprise to me
error(50): : syntax error: '(' came as a complete surprise to me while matching rule preamble
,我不知道这些错误是什么意思.有人可以解释一下,也许告诉我前进的方向吗?
and I have no idea what these error means. Can anyone explain and perhaps show me the way forward?
(注意:我不喜欢用代码污染语法文件,我使用的是访问者模式,但如果可以的话,我会接受它!)
(NB: I'm not wild about polluting the grammar file with code, I'm using the visitor pattern but I'll take it if I can!)
谢谢
include_filename :
('a'..'z' | 'A'..'Z' | '.' | '_')+
;
include_statement
@init { CommonTree includetree = null; }
:
'include' include_filename ';' {
try {
CharStream inputstream = null;
inputstream = new ANTLRFileStream($include_filename.text);
gramLexer innerlexer = new gramLexer(inputstream);
gramParser innerparser = new gramParser(new CommonTokenStream(innerlexer));
includetree = (CommonTree)(innerparser.program().getTree());
} catch (Exception fnf) {
;
}
}
-> ^('include' include_filename ^({includetree}))
;
推荐答案
从ANTLR4开始,不再可以使用语法规则来操纵生成的解析树.实际上,ANTLR3生成了AST(抽象语法树),它是解析树的子集(由ANTLR4生成).反过来,这意味着您无法保留树重写语法(以->
开头的部分).因此,您应该将代码更改为:
Starting with ANTLR4 it is no longer possible to manipulate the generated parse tree with grammar rules. In fact ANTLR3 generated an AST (abstract syntax tree), which is a subset of a parse tree (as generated by ANTLR4). That in turn means you cannot keep the tree rewrite syntax (the part starting with ->
). Hence you should change the code to:
include_statement
@init { CommonTree includetree = null; }
:
'include' Include_filename ';' {
try {
CharStream inputstream = null;
inputstream = new ANTLRFileStream($include_filename.text);
gramLexer innerlexer = new gramLexer(inputstream);
gramParser innerparser = new gramParser(new CommonTokenStream(innerlexer));
includetree = (CommonTree)(innerparser.program().getTree());
} catch (Exception fnf) {
;
}
}
;
这篇关于如何使用ANTLR4解析嵌套的源文件-再试一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!