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.
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]?
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.
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?
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…
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.