[ragel-users] Parsing of names with spaces in them

M Conrad silverdirk-rgl at silverdirk.com
Mon Jan 23 16:07:55 UTC 2012

On Mon, 23 Jan 2012 03:26:54 -0500, Victor Khimenko <khim at chromium.org>

> On Mon, Jan 23, 2012 at 11:11 AM, Gerald Gutierrez <
> gerald.gutierrez at gmail.com> wrote:
>> Hello folks,
>> I recently found Ragel and have discovered that it is a very pleasant
>> piece of software. That said, I've run into a problem that I was
>> hoping is common and a solution available.
> Sadly the problem is common but solution is quite explicitly is NOT
> available.

I think it is misleading to say that.  All he needs to do is alter the
rules to remove the ambiguity.  A scanner is certainly not required,
though may be desirable.

Gerald- Ragel operates on a principle where all state machines (meaning
all smaller machines within the larger machine) are continuously matching
the input.  Unless you make the machine decide which state to go into, it
will go into *both*, in kind of an uncertainty-principle theme.  It will
run any applicable actions for any transition that occurs, even if it
hasn't decided which state it is really in.

Writing actions for Ragel is different than for a typical backtracking
scanner.  You need to first make sure that it is possible to write your
machine without backtracking.  (If you need backtracking, Ragel also
provides a syntax for defining scanners, which is in the manual.)

Next, if your problem is that you want certain characters to indicate the
end of a previous loop (like ':' in your case), check out the priority
system.  If staying in a machine has equal priority with leaving it to go
to the next, Ragel will do both.  But, if one is prioritized, Ragel will
pick the transition with higher priority.

Finally, if you just need a tiny little bit of backtracking, but don't
want a full-blown scanner, you can try using actions that record character
positions, then other actions that use them.  For instance, every time you
see a non-space during 'name', you record the position "{lastAlpha=p;}",
then add priority to your rule to not leave 'name' (until it sees
something that can't be a name, like a ":"), then in the action for
leaving 'name' you can trim off "p-lastAlpha" number of characters,
knowing they are trailing spaces.

Hopefully that helps.  I would write an example but I'm a bit rusty on it
and short on time.  Let us know if you need one.


ragel-users mailing list
ragel-users at complang.org

More information about the ragel-users mailing list