在 ANTLR 规则中返回多个值 [英] Returning multiple values in ANTLR rule

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

问题描述

我有一个这样的 ANTLR 规则

I have an ANTLR rule like this

receive returns[Evaluator e,String message]
  : RECEIVE FILENAME {$e= new ReceiveEvaluator($FILENAME.text);}
  ;

我添加了一条新的返回消息,我想将文件内容放入其中.我可以做的一种方法是在我通过调用evaluate() 方法遍历树时让求值器返回字符串.

I have added a new return message and I want to put the file content in that. One way I could do is make the evaluator return the String when I walk the tree by calling the evaluate() method.

我想知道我是否可以在这里直接完成 - 但我不知道如何设置多个返回值并在以后访问它们.

I was wondering if I could do it strightaway here - but am not aware how to set multiple return values and access them later.

谢谢哈里

推荐答案

以下是设置和使用多个返回值的方法:

Here's how to set- and use multiple return values:

parse
  :  r=receive {
       Evaluator e = $r.evaluator;
       String m = $r.message;
     }
  ;

receive returns[Evaluator evaluator, String message]
  :  RECEIVE f=FILENAME {
       $evaluator = new ReceiveEvaluator($f.text);
       $message = "Some message here...";
     }
  ;

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

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