FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Simple expression parser for PHP.
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Simple expression parser for PHP. [message #179784 is a reply to message #179781] Tue, 04 December 2012 18:52 Go to previous messageGo to previous message
Anders Wegge Keller is currently offline  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.*
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to run program from php..?
Next Topic: Old version trouble with $_SERVER variables?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Feb 17 08:57:53 GMT 2025

Total time taken to generate the page: 0.03666 seconds