如何手动重写Antlr4解析树? [英] How to rewrite Antlr4 Parse Tree manually?

查看:255
本文介绍了如何手动重写Antlr4解析树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个简单的Xquery处理器,并使用Antlr4解析语法.我使用访客模式来浏览解析树.现在,如果查询满足某些条件,我想重写查询.如果查询直接使用诸如"join"之类的关键字并满足"join"语法,则处理器现在可以处理查询.

I am working on a simple Xquery processor and using Antlr4 to parse the grammar. I use the visitor pattern to walk through the parse tree. Now I want to rewrite a query if the query meet the some condition. The processor now can process a query if the query directly use the keyword like "join" and meet the "join" grammar.

如果查询可以更改为联接查询,我想先重写解析树,否则请什么都不做.有没有办法手动操作解析树?像添加规则上下文还是构造新的解析树?

I want to first rewrite the parse tree if the query can be changed to a join query or do nothing if not. Is there a way to manually manipulate the parse tree? Like adding a rule context or construct a new parse tree?

推荐答案

对于Antlr4,惯用的方法是用分析产品来装饰树节点,而不是使树结构变异.也就是说,可以使用一个或多个树遍历来标识和标记可以合并到联接和最终遍历中的节点,以输出结果.

For Antlr4, the idiomatic approach is to decorate tree nodes with analysis products, rather than mutating the tree structure. That is, one or more tree walks can be uses to identify and mark the nodes that could be merged into a join and a final walk to output the results.

当然,可以对分析树进行遍历以生成单独的AST,然后可以对它进行遍历并在结构上进行进一步的修改. Antlr4不支持此类AST的构建和行走.

Of course, the parse-tree could be walked to generate a separate AST that, in turn, could be walked and further structurally modified. Antlr4 does not provide support for the building and walking of such an AST.

这篇关于如何手动重写Antlr4解析树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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