[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Treetop problem

Marco Guiseppe

2/4/2008 10:32:00 PM

Hello everyone,

I've run into a problem with Treetop:

http://pastie.caboo...

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
--
Posted via http://www.ruby-....

4 Answers

Clifford Heath

2/4/2008 11:17:00 PM

0

Marco Guiseppe 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 Heath.

Marco Guiseppe

2/5/2008 3:15:00 AM

0

Clifford Heath 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


--
Posted via http://www.ruby-....

Day

2/5/2008 3:34:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On Feb 4, 2008 9:14 PM, Marco Guiseppe <kreatix@gmail.com> 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

Clifford Heath

2/5/2008 4:23:00 AM

0

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