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

查看:196
本文介绍了如何处理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天全站免登陆