How can I return a tree via the return value mechanism (instead of the automatic AST construction mechanism) and access it in an invoking rule?

Terence Parr

If you use rule labels, you won't see the return value, you'll see what ANTLR generates for you: nothing.

assignment
  :  lhs:expr EQUALS rhs:expr // lhs,rhs NOT SET!!!!
  ;
expr returns [Tree t]
  :  ... {t = buildNode();}
  ;

The problem is that return values use normal language assignment rather than the labels. ANTLR labels only refer to elements ANTLR builds like tokens and its own ASTs. You need to use the '=' assignment operator to get return values just like in plan C++ / Java:

assignment
{Tree lhs=null, rhs=null;}
  :  lhs=expr EQUALS rhs=expr
  ;
expr returns [Tree t]
  :  ... {t = buildNode();}
  ;
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact