如何使用 ANTLR4 解析嵌套源文件 - 再试一次 [英] How can I parse nested source files with ANTLR4 - Trying one more time

查看:28
本文介绍了如何使用 ANTLR4 解析嵌套源文件 - 再试一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆