# Treetop problem

**URL:** <https://www.ruby-forum.com/t/treetop-problem/128092>\
**Category:** Ruby\
**Created:** [February 4, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/treetop-problem/128092 "2008-02-04T23:32:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [February 4, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/treetop-problem/128092/1 "2008-02-04T23:32:04Z")

</div>

Hello everyone,

I’ve run into a problem with Treetop:

[http://pastie.caboo.se/147490](http://pastie.caboo.se/147490)

It seems to appear when the first character of the string parsed is a  
valid match for both rules, and Treetop is unable to resolve the  
ambiguity. One of the tests will always fail, depending on which comes  
first in the ‘test’ rule.

Any ideas about what’s going on here?

Thanks,

MG

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [February 5, 2008, 12:20am UTC](https://www.ruby-forum.com/t/treetop-problem/128092/2 "2008-02-05T00:20:56Z")

</div>

Marco G. wrote:

> It seems to appear when the first character of the string parsed is a  
> valid match for both rules, and Treetop is unable to resolve the  
> ambiguity.

Packrat parsers are greedy. When a rule succeeds, or when a repeated  
element (\*, +) repeats too many times, or when an optional rule is  
taken, that decision will never be reversed - it’s been memoized.  
You must prevent it happening (perhaps by using lookahead) in the  
first place.

In this case, you can do that by writing:

rule string  
[A-z] [A-z0-9]\*  
end

BTW, did you realize that there are non-alphabetic characters in the  
range A-z? Did you mean to say [A-Za-z]?

Clifford H…

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [February 5, 2008, 4:14am UTC](https://www.ruby-forum.com/t/treetop-problem/128092/3 "2008-02-05T04:14:39Z")

</div>

Clifford H. wrote:

> You must prevent it happening (perhaps by using lookahead) in the  
> first place.
> 
> In this case, you can do that by writing:

Thanks for your reply - I had a sneaking suspicion this was a problem  
with the type of parser, not Treetop specifically. Your suggestion won’t  
solve my problem though, as I need to match strings where the first  
character is a digit.

Thanks for pointing out the non-alphabetical chars in A-z, didn’t know  
I’d missed that.

MG

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [February 5, 2008, 4:34am UTC](https://www.ruby-forum.com/t/treetop-problem/128092/4 "2008-02-05T04:34:52Z")

</div>

On Feb 4, 2008 9:14 PM, Marco G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Your suggestion won’t solve my problem though, as I need to match strings  
> where the first character is a digit.

You’ll need to define your String rule, then, to fail on things that are  
100% digits. I’ve never worked with Treetop, but you want something that  
matches /^\D$/ as well as [A-Za-z0-9]+. Someone with more regexp check  
me,  
but is my thinking clear, anyway? Headed in the right direction?

Ben

---

<div class="post-metadata">

**Author:** ![kreatix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/b4bc9f/32.png) [@kreatix](https://www.ruby-forum.com/u/kreatix)\
**Post date:** [February 5, 2008, 5:25am UTC](https://www.ruby-forum.com/t/treetop-problem/128092/5 "2008-02-05T05:25:58Z")

</div>

Day wrote:

> you want something that matches /^\D$/ as well as [A-Za-z0-9]+.

There’s no \D in Treetop character classes, unlike Ruby regexps.

> but is my thinking clear, anyway?

Your thinking is clear, but Marco’s isn’t.

Marco, think about what you’re asking. You want to match a  
string of digits as a string, but also as a number. How is  
Treetop supposed to know which? Is it that with a number,  
the following character is not alphanumeric? If so, you must  
tell Treetop that:

rule number  
[0-9.]+ & ![A-Za-z0-9.]  
end

Clifford H…
