I have an expression in the following form (EBNF):
<expression> ::= ["("]<base>[")"][{<modifier>"("<expression>")"}]
<base> ::= <logical>[{<modifier><logical>}]
<logical> ::= "1" | "0"
<modifier> ::= "&" | "|"
An example of a string in this format would be:
(1|(1&0))|(1&(1|0))
Or alternatively I can get this in postfix notation:
110&|110|&|
Is there an easy way, in PHP, to evaluate this? (i.e. the result for this particular example should be 1). I want to avoid using eval function due to security issues.
If there isn’t an easy way to achieve this in PHP without eval function, what would be the best approach for writing a custom parser?
This is not easy to achieve in PHP without the eval function.
You’re going to have to use preg_match or something like it to evaluate your code and then convert it.