Returning multiple values in ANTLR rule

霸气de小男生 提交于 2020-01-02 03:29:10

问题


I have an ANTLR rule like this

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

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.

Thanks Hari


回答1:


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...";
     }
  ;


来源:https://stackoverflow.com/questions/3272041/returning-multiple-values-in-antlr-rule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!