small medium large xlarge

24 Feb 2014, 10:58
Subhash Chandran (3 posts)

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

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, 16:47
Terence Parr (53 posts)

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

24 Feb 2014, 17:06
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