[antlr-interest] Parse trace?

West Jay - Toronto-MROC - external JWest at MROC.COM
Tue May 19 09:06:08 PDT 2009


That was exactly what I was looking for!
I added "-trace" to the "ANTLR options" section of ANTLRWorks/Preferences and started getting all the output I was looking for.

I found some documentation on the -trace option in the v2 section, but none in the v3 area.  Of note is that -trace works in v3, but -traceParser and -traceLexer don't do anything at all.  Looks like they've been deprecated.  No matter, it was pretty easy to get what I was looking for without them.

The method of getting similar behavior to those two options is as follows:

To turn off Lexer trace, override TraceIn and TraceOut (the 2 arg versions) in your lexer class, like so:

    public partial class PLSQL31Lexer
    {
        public override void TraceIn( string ruleName, int ruleIndex )
        {
            // Create no trace output for lexing
        }

        public override void TraceOut( string ruleName, int ruleIndex )
        {
            // Create no trace output for lexing
        }
    }

That leaves me with just parser trace, exactly what I was looking for.
I then override the same methods to redirect the trace from the stdout to the debugout:

using trace = System.Diagnostics.Trace;   // use this instead of private TextWriter trace!

    public partial class PLSQL31Parser
    {
        //private TextWriter trace = Console.Out; // This mirrors the behaviour we're overloading

        // Override the parser trace method so we can control where the information goes
        public override void TraceIn( string ruleName, int ruleIndex )
        {
            TraceIn( ruleName, ruleIndex, input.LT( 1 ) );
        }

        // Override the parser trace method so we can control where the information goes
        public override void TraceIn( string ruleName, int ruleIndex, object inputSymbol )
        {
            trace.Write( is_sql ? "(SQL)" : "(PL/)" );
            trace.Write( "enter " + ruleName + " " + inputSymbol );
            if (state.backtracking > 0)
            {
                trace.Write( " backtracking=" + state.backtracking );
            }
            trace.WriteLine( string.Empty );
        }

        // Override the parser trace method so we can control where the information goes
        public override void TraceOut( string ruleName, int ruleIndex, object inputSymbol )
        {
            trace.Write( is_sql ? "(SQL)" : "(PL/)" );
            trace.Write( "exit " + ruleName + " " + inputSymbol );
            if (state.backtracking > 0)
            {
                trace.Write( " backtracking=" + state.backtracking );
                trace.WriteLine( state.failed ? " FAILED!" : " succeeded" );
            }
            else
            {
                trace.WriteLine( string.Empty );
            }
        }

        // Override the parser trace method so we can control where the information goes
        public override void TraceOut( string ruleName, int ruleIndex )
        {
            TraceOut( ruleName, ruleIndex, input.LT( 1 ) );
        }
    }

Note that I can comment out my using statement and uncomment the private to easily switch from debugout to any other TextWriter stream I want.  Very handy!
With this setup it's a one-line change to generate a file that traces my parsing if I want it.

And of course, to get a -traceLexer behavior I'd just reverse my implementation.

Thanks Ter, very handy!
J

-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Terence Parr
Sent: Thursday, May 14, 2009 5:48 PM
To: Tilman Bender
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Parse trace?

-trace option will work great for you. recompile and when you run your  
parser traceIn/traceOut methods will be called. you can override them  
or use the default which spits out information.

Ter
On May 14, 2009, at 11:34 AM, Tilman Bender wrote:

> Hi Jay,
>
> I am sure what you mean by "annotatin my grammar with trace  
> statements".
> If you mean doing this manually for every rule/alternative: you need  
> not do that.
>
> As far as I see you can have ANTLR add such trace information for  
> you, so you do not have to do
> it manually. I've seen this as a config option in the antlr3-maven- 
> plugin and I am sure there is a corresponding
> ANTLR-Tool option for this.
>
> Another (maybe weird) idea: Isn't this a typical use case for AOP?  
> Would it be feasible to weave the logging
> into the whole thing? Just an idea ;-)
>
> Tilman Bender
> Student des Software Engineering
> Hochschule Heilbronn
> tbender at stud.hs-heilbronn.de
>
>
>
> Am 14.05.2009 um 20:07 schrieb West Jay - Toronto-MROC - external:
>
>> So far, the only option I've seen is annotating my grammar with  
>> trace statements that'll execute during parsing.  Ick.  Is there  
>> not a better way?
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list