[ragel-users] scanning boolean expressions
Brodie Thiesfield
brofield2 at jellycan.com
Thu Nov 18 06:51:52 UTC 2010
Hi,
Some advice please. I wanted to make a scanner/parser for a simple
grammar. Simple boolean expressions with a variety of comparison
operators.
I originally tried to combine the scanner with the parser, resulting
in something like:
============
Tag := |*
'%' [a-z]+ { tag(ts,te); };
space;
*|;
Op := |*
"<" { op(LT); };
"<=" { op(LE); };
space;
*|;
Val := |*
[0-9]+ { val(ts, te); };
space;
*|;
Expr := Tag Op Val ;
============
It seemed exactly what I wanted. I wanted to express the valid order
for things as in the state machine of Expr, but using the scanner to
find the actual tokens, limiting them to only what is valid. However
it results in the error "references to graph instantiations not
allowed in expressions".
I eventually split it into two parts, a complete scanner and a parser
based on token types. I take the results of the scanner (calls to
tag(), op(), val(), etc) and pipe them into an instance of the state
machine Expr, watching for state errors to see if the token wasn't
valid.
============
Scanner := |*
# Tag
'%' [a-z]+ { tag(ts,te); };
# Op
"<" { op(LT); };
"<=" { op(LE); };
# Val
[0-9]+ { val(ts, te); };
space;
*|;
Tag = 'T';
Op = 'O';
Val = 'V';
Expr := Tag Op Val ;
============
I originally made it with a straight state machine, similar to the
mongrel HTTP parser with lots of "mark" and field actions, but it
tripped up on the difference between the operators "<" and "<=" for
some reason giving me strange results there.
Although this works, it can't be the right way to do it. Would someone
please give me pointers to a better way of doing it?
Regards,
Brodie
_______________________________________________
ragel-users mailing list
ragel-users at complang.org
http://www.complang.org/mailman/listinfo/ragel-users
More information about the ragel-users
mailing list