Here is the details
I am working on a parser which can parse logic input from user
basic format will be like this
Opening Brace* + Relational operator + Digit + Opening Brace* + (Logical Operator + Opening Brace + Relational operator + Digit + Opening Brace)* ...
Key:
*=optional
Brace= (,)
Relational operator= >, >=,<=,<,<>,=
Logical Operator= &,|
eg1
User input: >4&<10
Meaning: to find out a product with price > 4 and < 10
eg2
User input: >1000|(<10&>5)
Meaning: to find out a product with price > 1000 or (price < 10 but >5)
I have finish a draft parser but it doesn't work very well.
/\s*(\(?)\s*(>|<|>=|<=|=|<>)\s*(\d+)\s*(\)?)\s*(?:(&|\|)\s*(\(?)\s*(>|<|>=|<=|=|<>)\s*(\d+)\s*(\)?))*/
For eg1, it works
[0] => >4&<10
[1] =>
[2] => >
[3] => 4
[4] =>
[5] => &
[6] =>
[7] => <
[8] => 10
[9] =>
For eg2, the input is found to be a full match, but for unknown reason, it doesn't capture "<10" as subelements
[0] => >1000|(<10&>5)
[1] =>
[2] => >
[3] => 1000
[4] =>
[5] => &
[6] =>
[7] => >
[8] => 5
[9] => )
I know this parser is far from perfect, any comments are welcome.