Hello-
I'm just starting out with PRD here, but I think I'm on the right track. I'm attempting to write a parser that evaluates a language similar to SQL's WHERE syntax. I'd also like to evaluate the result of this boolean expression with some given input data. The language is a reduced version of SQL's WHERE syntax. Here are some valid expressions (and they function as you would imagine): nb: `=' is comparison, not assignment.


(has_access = 1 && is_disabled = 0) || has_override = 1
has_override = 1
likes_pie = 1 && likes_apples = 1
(num_oranges > 5 && num_apples > 5) || (num_grapes < 2 && num_berries < 3)

The item to the left of the operand is always the variable and the item to the right is always some integer or floating point value. Here's the grammar I've come up with:

--snip--
start       : coupon(s /(?:&&|\|\|)/) /^\Z/

coupon      : expression
            | '(' expression ')'
expression  : statement boolean statement
            | statement
statement   : variable comparison number

boolean     : /(?:&&|\|\|)/
comparison  : /(?:=|!=|>|>=|<|<=)/
number      : /[-+]?\d+(\.\d+)?/

variable    : /[a-z_]+/i
              | '.'
--snip--

It appears to properly parse things, so I'm pretty happy about that. The question here is that I'd like to be able to evaluate the above statements by passing in some values for num_oranges, num_apples, has_override, etc. Idealy, I'd like to be able to give it a hashref with all of these variables and then have PRD spit out true or false for the final result. I was thinking about using the $arg facility and calling it as such:
my $result = $parser->start($expression, 1, $parameters);
but passing $arg around seems to be a fairly messy way to do this. In fact, do I even want to be attempting to evaluate this from within PRD, or should I be building a parsetree and doing it separately? Some guidance would be appreciated. Thanks.

Reply via email to