[antlr-interest] Suppress output of org.antlr.runtime.tree.TreeRewriter
Stefan Bischof
stefan.bischof at deri.org
Tue Jun 30 10:10:20 PDT 2009
Hey,
On Tue, 2009-06-30 at 08:19 -0700, Jim Idle wrote:
> Stefan Bischof wrote:
> > Hi guys,
> >
> > I'm currently using a rewriting tree grammar (rewrite=true, filter=true)
> > which means my parser is extending org.antlr.runtime.tree.TreeRewriter.
> >
> > I want to feed output of my program directly to another program with an
> > unnamed pipe | on the command line. Unfortunately that's not possible
> > now because the TreeRewriter shows every rewriting on stdout (like a ->
> > b).
> >
> > So, is there a possibility to suppress this output (yes, it's using
> > System.out.println)?
> >
> Hmmm - I wonder if it is supposed to be doing that:
>
> if ( r!=null && !t.equals(r.getTree()) && r.getTree()!=null
> ) { // show any transformations
> System.out.println(((CommonTree)t).toStringTree()+" -> "+
>
> ((CommonTree)r.getTree()).toStringTree());
> }
I'm wondering too :)
> Anyway, tell ANTLR to use a superclass say AbstractMyTreeRewriter
> (options { superClass=AbstractMyTreeRewriter; } ) and in that class:
>
> public abstract class AbstractMyTreeRewriter extends TreeRewriter {
>
> public TreeRewriter(TreeNodeStream input) {
> super(input);
> }
> public TreeRewriter(TreeNodeStream input, RecognizerSharedState state) {
> super(input, state);
> }
>
> public Object applyOnce(Object t, fptr whichRule) {
> if ( t==null ) return null;
> try {
> // share TreeParser object but not parsing-related state
> state = new RecognizerSharedState();
> input = new CommonTreeNodeStream(originalAdaptor, t);
>
> ((CommonTreeNodeStream)input).setTokenStream(originalTokenStream);
> setBacktrackingLevel(1);
> TreeRuleReturnScope r = (TreeRuleReturnScope)whichRule.rule();
> setBacktrackingLevel(0);
> if ( failed() ) return t;
>
> //
> -------------------------------------------------------------------------
> // Commented out the output
> //if ( r!=null && !t.equals(r.getTree()) &&
> r.getTree()!=null ) { // show any transformations
> // System.out.println(((CommonTree)t).toStringTree()+" -> "+
> //
> ((CommonTree)r.getTree()).toStringTree());
> //}
>
> if ( r!=null && r.getTree()!=null ) return r.getTree();
> else return t;
> }
> catch (RecognitionException e) { ; }
> return t;
> }
>
> }
Thanks a lot! That sounds like a practical solution.
Do you plan to remove that in the future? Because IMO that console
output should at least be packed in some logging framework or
suppressible by some switch.
Thanks again!
regards
stefan bischof
More information about the antlr-interest
mailing list