ANTLR表达式重写中间树

发布于 2024-08-28 14:29:23 字数 225 浏览 4 评论 0原文

对于像 3+4 这样的表达式,我想在中间表示树中使用值 7。

我无法弄清楚如何将返回值放入重写规则中。

表达式返回 [int v]: 等等。

如何将 expression.v 放入 WR 中?现在我得到 (+ 3 4),我想要 (7)

|^( WRITE c=表达式) -> ^(WRINT ^(INTC ^($c))

下一步是在汇编器中发出 7。

For expressions like 3+4 I would like to use the value 7 in an intermediate representation tree.

I cannot work out how to get the returns value into a rewrite rule.

expression returns [int v]:
etc.

How do I get expression.v into WR? At the moment I get (+ 3 4), I want (7)

|^( WRITE c=expression) -> ^(WRINT ^(INTC ^($c))

the next step is to emit 7 in an assembler.

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

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

发布评论

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

评论(1

新雨望断虹 2024-09-04 14:29:24

我想您想知道如何使用重写语法来构造一个值为 $c 的单个数字标记,而不是另一棵树?如果是这种情况,您可以

^(WRITE c=expression) -> INT[$c.v] ;

假设 INT 是整数的标记类型来执行此操作。

这假设您的表达式规则实际上计算并返回整数结果。如果没有,并且您想知道如何进行持续折叠,那么这是一个更大的话题。查看 ANTLR 示例中的多项式示例收藏;它展示了如何进行一些基本的简化。您可能可以使用树重写器使用以下规则来完成此操作

^('+' a=INT b=INT) -> INT[String.valueOf($a.int+$b.int)] ;

I think you want to know how to use the rewrite syntax to construct a single numeric token with the value of $c, rather than another tree? If that's the case, you can do this with

^(WRITE c=expression) -> INT[$c.v] ;

assuming that INT is the token type for integers.

That assumes that your expression rule actually evaluates and returns an integer result. If it doesn't and you want to know how to do constant folding, that's a much bigger topic. Take a look at the polynomial example in the ANTLR examples collection; it shows how to do some basic simplification. You could probably do it with a tree rewriter using rules like

^('+' a=INT b=INT) -> INT[String.valueOf($a.int+$b.int)] ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文