# TreeTop help?

**URL:** <https://www.ruby-forum.com/t/treetop-help/128648>\
**Category:** Ruby\
**Created:** [February 9, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/treetop-help/128648 "2008-02-09T21:26:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Day](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dc4da7/32.png) [@Day](https://www.ruby-forum.com/u/Day)\
**Post date:** [February 9, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/treetop-help/128648/1 "2008-02-09T21:26:10Z")

</div>

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: [Parked at Loopia](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

---

<div class="post-metadata">

**Author:** ![Day](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dc4da7/32.png) [@Day](https://www.ruby-forum.com/u/Day)\
**Post date:** [February 10, 2008, 3:28am UTC](https://www.ruby-forum.com/t/treetop-help/128648/2 "2008-02-10T03:28:07Z")

</div>

On Feb 9, 2008 2:25 PM, Day [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

It looks like a simple typo to me

rule item\_name  
[a-zA-Z]+  
end

Daniel Brumbaugh K.

---

<div class="post-metadata">

**Author:** ![Day](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dc4da7/32.png) [@Day](https://www.ruby-forum.com/u/Day)\
**Post date:** [February 10, 2008, 6:02am UTC](https://www.ruby-forum.com/t/treetop-help/128648/3 "2008-02-10T06:02:49Z")

</div>

On Feb 9, 2008 8:27 PM, Daniel Brumbaugh K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It looks like a simple typo to me
> 
> rule item\_name  
> [a-zA-Z]+  
> end
> 
> Daniel Brumbaugh K.

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

---

<div class="post-metadata">

**Author:** ![Day](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dc4da7/32.png) [@Day](https://www.ruby-forum.com/u/Day)\
**Post date:** [February 10, 2008, 7:20am UTC](https://www.ruby-forum.com/t/treetop-help/128648/4 "2008-02-10T07:20:41Z")

</div>

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 H…

---

<div class="post-metadata">

**Author:** ![Day](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dc4da7/32.png) [@Day](https://www.ruby-forum.com/u/Day)\
**Post date:** [February 10, 2008, 12:08pm UTC](https://www.ruby-forum.com/t/treetop-help/128648/5 "2008-02-10T12:08:17Z")

</div>

On Feb 10, 2008 12:20 AM, Clifford H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 H…

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 K.
