如何处理ANTLR中的列表返回值 [英] How to deal with list return values in ANTLR

查看:27
本文介绍了如何处理ANTLR中的列表返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ANTLR中解决这个问题的正确方法是什么:

What is the correct way to solve this problem in ANTLR:

我有一个简单的语法规则,比如一个包含任意数量元素的列表.

I have a simple grammar rule, say for a list with an arbitrary number of elements.

list
: '[]' 
| '[' value (COMMA value)* ']'

如果我想为列表分配一个返回值,并使该值成为生产中返回值的实际列表,那么正确的方法是什么?我正在娱乐的替代方案是:

If I wanted to assign a return value for list, and have that value be the actual list of returned values from the production, what is the proper way to do it? The alternatives I'm entertaining are:

  • 在全局范围内创建我自己的堆栈以跟踪这些列表
  • 尝试检查我下面的树节点并以这种方式提取信息
  • 以某种巧妙而酷炫的方式访问它,我希望能从中找到可以从与规则关联的操作中轻松访问此类列表的方法.

我想问题是:酷孩子是怎么做到的?

I guess the question is: How do the cool kids do it?

(仅供参考,我正在为 ANTLR 使用 python API,但如果你用另一种语言打我,我可以处理)

(FYI I'm using the python API for ANTLR, but if you hit me with another language, I can handle that)

推荐答案

在 C# 中,它可能如下所示:

In C# it might look like this:

list returns [ List<string> ValueList ]
    @init
    {
        $ValueList = new List<string>();
    }
    : '[]'
    | '[' value {$ValueList.Add(value);} (COMMA value {$ValueList.Add(value);})* ']'
    ;

这篇关于如何处理ANTLR中的列表返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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