TreeTop help?

Discussion in 'Ruby' started by Day, Feb 9, 2008.

  1. Day

    Day Guest

    So I'm building a parser, and I thought I'd give TreeTop a go. I
    recently got Practical Ruby Projects from Apress and worked through
    the last chapter, so I feel like I have somewhat of an understanding
    of what's going on, but I'm having a weird issue. It is apparent to me
    that the order you define rules in TreeTop matters.

    The stuff I'll be parsing (somewhat simplified) looks like this:

    Item, name(value), name(value), name(value)...etc.

    Here's my grammar and test script: http://pastie.caboo.se/149766

    This fails. If I comment out the item and item_name rules and then
    change the string to be parsed to just 'bar(1), baz(14)' it works. i
    can't figure out what the order of the rules might imply (if I reorder
    them, it breaks again). Any help or pointing at some documentation
    more in-depth than that found on the TreeTop site would be much
    appreciated. Thanks.


    Ben
     
    Day, Feb 9, 2008
    #1
    1. Advertising

  2. On Feb 9, 2008 2:25 PM, Day <> wrote:

    It looks like a simple typo to me

    rule item_name
    [a-zA-Z]+
    end


    Daniel Brumbaugh Keeney
     
    Daniel Brumbaugh Keeney, Feb 10, 2008
    #2
    1. Advertising

  3. Day

    Day Guest

    On Feb 9, 2008 8:27 PM, Daniel Brumbaugh Keeney
    <> wrote:
    > It looks like a simple typo to me
    >
    > rule item_name
    > [a-zA-Z]+
    > end
    >
    >
    > Daniel Brumbaugh Keeney


    Wow. That's how awesome I am. At life. Heh. Thanks man. Good eye.

    The ordering of things DOES matter, right? Or am I hallucinating?


    Ben
     
    Day, Feb 10, 2008
    #3
  4. Day wrote:
    > The ordering of things DOES matter, right? Or am I hallucinating?


    The ordering of rules in a grammar doesn't matter at all.
    The compiler processes each rule into a method (and some
    modules that extend objects created in that method), and
    it does that without considering the existence of any other
    rule. Just as order of def's in a class doesn't matter, the
    order of rules in a grammar doesn't matter.

    On the other hand, the order of alternatives within a rule
    does matter. Once one alternative succeeds, even if it leaves
    the input at a point where the calling rule cannot succeed,
    no other alternative will ever be tried. You may need to use
    lookahead to prevent this happening.

    Finally, the success of one item in a sequence is final. If
    the path chosen prevents following items in that sequence to
    fail, the previous item will never be revisited, and the whole
    sequence will fail. Again, you might need to use lookahead
    to prevent this - especially to place limits on repetition.

    This is looking like becoming an FAQ, and I hope I'm getting
    better at stating it succinctly. I also hope Google starts
    finding it, so I don't have to :).

    Clifford Heath.
     
    Clifford Heath, Feb 10, 2008
    #4
  5. On Feb 10, 2008 12:20 AM, Clifford Heath <> wrote:

    > This is looking like becoming an FAQ, and I hope I'm getting
    > better at stating it succinctly. I also hope Google starts
    > finding it, so I don't have to :).
    >
    > Clifford Heath.
    >


    There's been quite a bit of talk about Treetop on this list lately,
    seeing as it has no mailing list, feel free to join
    irc://irc.freenode.net/#treetop


    Daniel Brumbaugh Keeney
     
    Daniel Brumbaugh Keeney, Feb 10, 2008
    #5
    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:
    244
    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:
    102
    Phrogz
    Jan 28, 2008
  3. Phrogz

    Treetop Email Parser

    Phrogz, Jan 28, 2008, in forum: Ruby
    Replies:
    13
    Views:
    216
    Phil Tomson
    Feb 1, 2008
  4. Replies:
    1
    Views:
    82
    Clifford Heath
    Feb 2, 2008
  5. JCLL
    Replies:
    1
    Views:
    128
Loading...

Share This Page