Re: Simple expression parser for PHP. [message #179784 is a reply to message #179781] |
Tue, 04 December 2012 18:52 ![Go to previous message Go to previous message](/forum/theme/default/images/up.png) ![Go to next message Go to previous message](/forum/theme/default/images/down.png) |
Anders Wegge Keller
Messages: 30 Registered: May 2012
Karma:
|
Member |
|
|
Daniel Pitts <newsgroup(dot)nospam(at)virtualinfinity(dot)net> writes:
> On 12/4/12 4:49 AM, Anders Wegge Keller wrote:
>> Daniel Pitts <newsgroup(dot)nospam(at)virtualinfinity(dot)net> writes:
>>
>>> My trouble here is not that I'm trying to look smarter, but that I'm
>>> trying to minimize exposing proprietary information of my employer.
>>
>> You are not going to get any real help, until you present an example
>> of what you need to parse. Or at least something close enough to the
>> real thing, as to make sense.
>>
>> I understand you desire to keep proprietary information private, but
>> as long as you are only able to provide vague generalities, you're not
>> going to get anything but vague generalities for answers.
>>
>> One general answer is to look at
>> http://pear.php.net/package/PHP_ParserGenerator and see if that is
>> suitable for you, even though it's unmaintained.
>>
> I actually did find that pear package. I was hoping to avoid creating
> my own grammar. The goal was to see if there was something we could
> use out-of-the-box.
>
> If I'm going to go through the trouble of creating my own grammar, I'd
> probably write it in C instead of PHP, and just plug that in.
>
> My "vague generalities" I've given are real use-cases. Perhaps I
> didn't explain them enough...
I miss at least one thing: How complex expressions do you need to
handle? If you can impose a limitation to very simple expressions, you
might be able to handle them with a set of regular expressions instead
of a tokenizer. But the complexity of that will grow exponentially, so
you'd pretty soon want to do something else.
> Given objects (or associative arrays) Foo and Bar. I'd like to have
> some call such as:
>
> $expression = "foo['somePropertyOfFoo'] * 3";
> $context = initContext(array("foo"=>$Foo, "bar"=>$Bar); // or whatever.
> $result = $expressionEvaluator->evaluate($expression, $context);
>
> This particular snippet would result in the same value as
> $Foo['somePropertyOfFoo'] * 3.
>
> where $expression is actually read from a configuration parameter somewhere.
>
> I also need some sort of basic function calling support, even it that
> requires setting up the available functions before hand.
>
> Hopefully that makes the most sense.
Apart from the complexity of the expressions, I'd say build your own
lexer and parser. *OR* if you have your own choice of DSL, see if you
can use Lua. <http://php.net/manual/en/book.lua.php>
By using lua, you can isolate your DSL from the rest of the
application, which I guess is one concern, since you started by asking
for a way to avoid eval().
--
/Wegge
Leder efter redundant peering af dk.*,linux.debian.*
|
|
|