如何使用 ANTLR 树语法语法从树解析器中获取信息? [英] How can I get information out of a tree parser using the ANTLR tree grammar syntax?

查看:26
本文介绍了如何使用 ANTLR 树语法语法从树解析器中获取信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功构建了一个解析器/词法分析器,它为我的语言创建了一个 AST.是的!!我现在进入了翻译"阶段.我用引号说解释器是因为该语言是声明性的,因为它不像程序语言那样真正被执行.它被翻译成 Java 对象,稍后在我的应用程序运行中使用.

I have successfully built a parser/lexer that creates an AST for my language. Yeah!! I am now onto the "interpreter" stage. I say interpreter in quotes because the language is declarative as isn't really being executed like a procedural language might. It is being translated into Java objects that are used later on in the run of my application.

当我走我的 AST 时,我需要将树节点转换为 Java 对象.这些 Java 对象在我的程序中的其他地方使用.我了解如何创建操作以创建 Java 对象,但如何将操作的结果返回到我的主程序中?

As I am walking my AST I need to translate the tree nodes into Java objects. These Java objects are used else where in my program. I understand how to create actions to cause the Java objects to be created, but how do I get the result of the actions back into my main program?

我是否使用@members{} 标记并在其中编写我的 getter 方法?

Do I use the @members{} tag and write my getter methods in there?

推荐答案

我是否使用@members{} 标记并在其中编写我的 getter 方法?

Do I use the @members{} tag and write my getter methods in there?

是的,这是一个选项.但是,(树)语法规则可以返回您的自定义对象,以便当您调用树遍历器(或解析器)的入口点时,会返回此自定义对象:

Yes, that is an option. However, (tree) grammar rules can return your custom objects so that when you invoke the entry point of your tree walker (or parser), this custom object is returned:

tree grammar YourTreeWalker;

...

walk returns [CustomObject obj]
 : ... 
   {
     $obj = ...
   }
 ;

...

代码中的其他地方

...
YourTreeWalker walker = new YourTreeWalker(...); 
CustomObject obj = walker.walk();  

这篇关于如何使用 ANTLR 树语法语法从树解析器中获取信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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