How can I get information out of a tree parser using the ANTLR tree grammar syntax?

╄→гoц情女王★ 提交于 2020-01-04 06:49:26

问题


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.

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?

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


回答1:


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:

grammar

tree grammar YourTreeWalker;

...

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

...

elsewhere in your code

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


来源:https://stackoverflow.com/questions/11909007/how-can-i-get-information-out-of-a-tree-parser-using-the-antlr-tree-grammar-synt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!