24 Feb 2014, 10:58
Subhash_2_pragsmall

Subhash Chandran (3 posts)

I’m learning Visitor using the book. Based on the book example, I developed my own visitor:

https://github.com/wiztools/bc/blob/master/src/main/java/org/wiztools/bc/MyExprVisitor.java

My question on this code is: I had to implement:

private BigDecimal finalOut = null;

to maintain the result of the visitor. This is because visit(ParseTree pt) was finally returning null (I had to override that too).

Let me know if I am doing the correct thing by maintaining state across visitation by using finalOut, or is there a more elegant way to do things?

24 Feb 2014, 10:59
Subhash_2_pragsmall

Subhash Chandran (3 posts)

The code as a link: MyExprVisitor.java.

24 Feb 2014, 16:47
Australia-3_pragsmall

Terence Parr (37 posts)

Hi. I see nothing wrong with having a field record the final result.

24 Feb 2014, 17:06
Subhash_2_pragsmall

Subhash Chandran (3 posts)

Thanks! Somehow felt that having to assign finalOut in every method (visitNumber, visitMulDiv, visitAddSub and visitParens) seemed to bad practise. Thought there could be some simpler solution.

  You must be logged in to comment