how can I debug treetop grammar?

Discussion in 'Ruby' started by Lex Williams, Aug 29, 2008.

  1. Lex Williams

    Lex Williams Guest

    Hi guys !

    Parse::RecDescent has RD_TRACE , which when is set to 1 , tells you
    which rule the parser it's trying , what it matches , what part of the
    input consumes , and so on . Does treetop have an equivalent to this ?
    I don't really find the documentation available for it to be that good ,
    and calling parser.failure_line and parser.failure_column returns 1,1 no
    matter what. Help !
    --
    Posted via http://www.ruby-forum.com/.
    Lex Williams, Aug 29, 2008
    #1
    1. Advertising

  2. Lex Williams wrote:
    > Parse::RecDescent has RD_TRACE , which when is set to 1 , tells you
    > which rule the parser it's trying , what it matches , what part of the
    > input consumes , and so on . Does treetop have an equivalent to this ?


    Each SyntaxNode has "interval", which is the range of byte-offsets matched,
    and text_value, which returns that interval from the input.

    You seem to have missed some fine diagnostic methods:

    SyntaxNode#inspect shows a wealth of information, including an indented
    node hierarchy, what input that node matched (abbreviated) and what
    accessor methods are added by the modules extended into each node.

    Parser#failure_reason tells you where it got to, and what tokens would
    have allowed it to get further.

    If that isn't enough, try calling your grammar from a lower-level rule
    and working up until you see where it's failing.

    Also don't forget that there's a "tt" command you can use to generate
    the grammar into a ruby file you can inspect.

    As for your earlier query, it's a known bug.

    Clifford Heath.
    Clifford Heath, Aug 29, 2008
    #2
    1. Advertising

  3. Lex Williams

    Lex Williams Guest

    > SyntaxNode#inspect shows a wealth of information, including an indented
    > node hierarchy, what input that node matched (abbreviated) and what
    > accessor methods are added by the modules extended into each node.
    >
    > Parser#failure_reason tells you where it got to, and what tokens would
    > have allowed it to get further.
    >
    > If that isn't enough, try calling your grammar from a lower-level rule
    > and working up until you see where it's failing.
    >
    > Also don't forget that there's a "tt" command you can use to generate
    > the grammar into a ruby file you can inspect.
    >
    > As for your earlier query, it's a known bug.
    >
    > Clifford Heath.


    Thank you Clifford , I really appreciate you taking the time to answer
    this .
    I will look into them , and post here whatever questions I will have !
    --
    Posted via http://www.ruby-forum.com/.
    Lex Williams, Aug 29, 2008
    #3
  4. Lex Williams

    Lex Williams Guest

    Would you please walk me through a debugging example ? Considering this
    grammar :

    grammar Dp
    rule all
    day space month {
    def value
    day.value.to_s + " " + month.value
    end
    }
    end

    rule day
    [1-9] [1-9] / "" {
    def value
    text_value.to_i
    end
    }
    end

    rule space
    ' '*
    end

    rule month
    "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" /
    "Aug" / "Sep" / "Oct" / "Nov" / "Dec" / "" {
    def value
    text_value
    end
    }
    end


    end

    and the following code :

    equire "rubygems"
    require "treetop"

    Treetop.load "dp"

    parser = DpParser.new
    node = parser.parse("12")
    another = parser.parse("Feb")
    lastone = parser.parse("12 Feb")
    puts node.value
    puts another.value
    puts lastone.value

    -------------------------------------------------------------------------------
    i'm getting back syntax nodes , but I can't call value on any of them .
    could you please show me how you would debug the rules ?
    hope I'm not asking for too much !
    Thanks ,
    Lex
    --
    Posted via http://www.ruby-forum.com/.
    Lex Williams, Aug 29, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phrogz
    Replies:
    18
    Views:
    231
    IƱaki Baz Castillo
    Apr 22, 2008
  2. David Rose

    when to use PEP treetop vs regexp

    David Rose, Jan 28, 2008, in forum: Ruby
    Replies:
    1
    Views:
    85
    Phrogz
    Jan 28, 2008
  3. Phrogz

    Treetop Email Parser

    Phrogz, Jan 28, 2008, in forum: Ruby
    Replies:
    13
    Views:
    197
    Phil Tomson
    Feb 1, 2008
  4. Chiyuan Zhang
    Replies:
    3
    Views:
    87
    Chiyuan Zhang
    Feb 4, 2008
  5. Robert Schaaf

    treetop grammar problem

    Robert Schaaf, Jun 10, 2009, in forum: Ruby
    Replies:
    2
    Views:
    84
    Robert Schaaf
    Jun 10, 2009
Loading...

Share This Page