[antlr-interest] tree walking and semantic analysis

Carter Cheng carter_cheng at yahoo.com
Mon Feb 2 13:35:53 PST 2009


Hi Andy and the others,

Thanks for the reply. I am currently reworking my tree code and doing it somewhat manually because I was having some difficulties with working with the C runtime in C++ mode. The difficulty I was having was something along these lines- 

program[int pass] 
: (declarations[pass])*
; 

declarations[int pass]
: /* some CFG rule case */ { switch(pass) { /* do something depending on the pass */ } }
;

This obviously seems like quite a mess. Especially when in different passes you need to percolate different types of data up the tree and left to right depending on the pass. 

A possible alternative is to using some sort of visitor or function object which behaves differently on each pass. However I am not quite sure how to get this to work properly. The issue primarily is with the return value types in my language I have certain expression constants which need to be checked and if possible reduced to their actual values in certain descents of the grammar and in other cases return the whole expression tree- 

declaration[Functor f] returns [??? retval ]
: /* some CFG rule case */ { $retval = f(e); }
;

The return type can be quite different depending on the functor in question making it somewhat difficult to determine what the type of retval should be. 

This is getting a bit long I suspect with the error situation with multiple errors the only thing that can be done is return something... pass in some sort of functor or I/O stream which can be used to dump data back to the user etc.

I hope that sort of makes sense.

Regards,

Carter.


      


More information about the antlr-interest mailing list