[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Ignored alternatives



Pawel Szymanski wrote:
> 
> Hi,
> I have a question concerning generating simpler AST trees:
> Is there any way to force SableCC to omit some parts alternative (i.e.
> tokens that are obvious) ? There is ignored alternatives mechanism, but it
> seemed not to impact shape of generated AST tree.
> 
> For example:
> I have foolowing production:
> 
> compiler_directive = {temp} pragma  [pragma_name]:identifier l_brack
> [pragma_parameter]:string_value r_brack ;
> 
> I want that only pragma_name and pragma_parameter tokens will be included to
> AST.
> 
> Pawel

For an example of how to do this, look at the SimpleC example.  More
precisely, you need to look at two files:
1- the grammar file (simple.txt)
2- the class ModifyAST.java

You'll find some ignored alternatives in the grammar.  In the ModifyAST
class, the AST is modified to use the ignored alternatives.  For more
robustness, Walker.java is used to walk the transformed AST, throwing
exceptions if any "replaced" alternative is found.

If your goal is to build a smaller AST to reduce memory consumption, you
should look at appendix D of the thesis.  It shows how to make use of
hidden alternatives in a customized parser.  BEWARE: This approach is
dangerous, as errors will not be caught at compile time.  Furthermore,
if you get runtime errors, they will be very difficult to debug, as
exceptions will be thrown long after the fact.  You've been warned.

Have fun!

Etienne
-- 
----------------------------------------------------------------------
Etienne M. Gagnon, M.Sc.                     e-mail: egagnon@j-meg.com
Author of SableCC:                 http://www.sable.mcgill.ca/sablecc/
----------------------------------------------------------------------