查看解析文件输出的方法?

发布于 2024-10-20 10:20:24 字数 407 浏览 1 评论 0原文

我是 Vinod,有兴趣在 Java 项目中使用 ANTLR v3.3 生成 C 解析器,并以某种可查看的形式生成解析树。我从 本教程

ANTLR 为语法生成词法分析器和解析器文件,但我不完全了解如何查看这些生成的文件。例如,在上面文章的几个示例中,作者使用 ASTFrame 生成了输出。我在 ANTLRWorks 中只找到了一个解释器选项,它显示了一些树,但如果谓词更多,它会给出错误。

任何好的参考书或文章都会非常有帮助。

I am Vinod and am interested to use an ANTLR v3.3 for C parser generation in a Java project and generate the parsed tree in some viewable form. I got help to write grammar from this tutorial

ANTLR generates lexer and parser files for the grammar but I don't exactly get how these generated files are viewed. e.g. in few examples from above article, author has generated output using ASTFrame. I found only an interpreter option in ANTLRWorks which shows some tree but it gives error if predicates are more.

Any good reference book or article would be really helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始于初秋 2024-10-27 10:20:24

您只需要一本书:

在此处输入图像描述

权威 ANTLR 参考:构建特定于领域的语言

之后,还有许多更优秀的书籍(关于 DSL 创建),但这是 ANTLR 入门书籍。

There's only one book you need:

enter image description here

The Definitive ANTLR Reference: Building Domain-Specific Languages.

After that, many more excellent books exist (w.r.t. DSL creation), but this is the book for getting started with ANTLR.

独夜无伴 2024-10-27 10:20:24

正如您所见,ANTLRWorks 将打印解析树和 AST,但不适用于谓词和 C 目标。虽然不像 ANTLRWorks 那样漂亮,但当您将 AST 传递给树的根时,它会打印 AST 的文本版本。

void printNodes(pANTLR3_BASE_TREE thisNode, int level)
{
  ANTLR3_UINT32 numChildren = thisNode->getChildCount(thisNode);
  //printf("Child count %d\n",numChildren);

  pANTLR3_BASE_TREE loopNode;
  for(int i=0;i<numChildren;i++)
  {
    //Need to cast since these can hold anything
    loopNode = (pANTLR3_BASE_TREE)thisNode->getChild(thisNode,i);

    //Print this node
    pANTLR3_STRING thisText = loopNode->getText(loopNode);
    for(int j=0;j<level;j++)
      printf(" ");
    printf("%s\n",thisText->chars);

    //If this node has a child
    if(loopNode->getChildCount(loopNode) > 0)
      printNodes(loopNode, level + 2);
  }
}

As you saw, ANTLRWorks will print both parse trees and the AST but won't work with predicates and the C target. While not a nice picture like ANTLRWorks, this will print a text version of the AST when you pass it the root of your tree.

void printNodes(pANTLR3_BASE_TREE thisNode, int level)
{
  ANTLR3_UINT32 numChildren = thisNode->getChildCount(thisNode);
  //printf("Child count %d\n",numChildren);

  pANTLR3_BASE_TREE loopNode;
  for(int i=0;i<numChildren;i++)
  {
    //Need to cast since these can hold anything
    loopNode = (pANTLR3_BASE_TREE)thisNode->getChild(thisNode,i);

    //Print this node
    pANTLR3_STRING thisText = loopNode->getText(loopNode);
    for(int j=0;j<level;j++)
      printf(" ");
    printf("%s\n",thisText->chars);

    //If this node has a child
    if(loopNode->getChildCount(loopNode) > 0)
      printNodes(loopNode, level + 2);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文