[ragel-users] scanning boolean expressions

Adrian Thurston adrian.thurston at esentire.com
Fri Nov 19 17:26:35 UTC 2010


Hi Brodie,

In general, Ragel is not made for parsing context-free languages. Also 
speaking generally, expression languages are normally context free.

There are exceptions to both of these rules. It's possible to write 
recursive parsers using ragel and not all 'expression' languages have to 
be context-free, however I would suggest sticking with the conventional 
methods unless you really know what you are doing. Use Ragel for 
scanning and some parser generator for parsing.

-Adrian

program some recursive language constructs into

On 10-11-17 10:51 PM, Brodie Thiesfield 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