[ragel-users] scanning boolean expressions
Wesley Moore ✆
wjmoore at gmail.com
Thu Nov 18 11:03:31 UTC 2010
I was Was thinking about using Ragel to build a compiler for a programming language today and after looking at the clang sample linked on the website and the full C grammar I came to the conclusion that a scanner as the lever that fed tokens into a state machine with the grammar was the way I'd need to do it. In other words pretty much exactly what you described here. So I too would be interested to know if this is a sensible way to go about it.
Regards,
Wes
On 18/11/2010, at 5:51 PM, Brodie Thiesfield <brofield2 at jellycan.com> wrote:
> 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
_______________________________________________
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