Re: [pcre-dev] Need help with implementing a parser (binary …

Top Page

Reply to this message
Author: Zoltán Herczeg
Date:  
To: pcre-dev@exim.org, sasho648
Subject: Re: [pcre-dev] Need help with implementing a parser (binary operators) - pcre2
Hi,

in my experiences this is the easiest way to parse expressions:

main-expression = pre-primary-expression primary-expression post-primary-expression

pre-primary-expression - mostly unary operators, new operator, etc.
primary-expression - identifiers, keywords (e.g. this/true/null), expression in brackets, etc.
post-primary-expression - function calls, array item access, ++/--, etc.

expression = main-expression (binary-operator main-expression)*

As you can see, no inifite recursion here.

If you want to include precedence, create an expression for each precedence level. If you only have '+' and '*' operators, you get something like this:

multiplicative_expr = expr ('*' multiplicative_expr | )
additive_expr = expr ('*' multiplicative_expr | '+' additive_expr | )

Regards,
Zoltan

-------- Eredeti levél --------
Feladó: sasho648 via Pcre-dev < pcre-dev@??? (Link -> mailto:pcre-dev@exim.org) >
Dátum: 2020 december 23 22:51:57
Tárgy: [pcre-dev] Need help with implementing a parser (binary operators) - pcre2
Címzett: pcre-dev@??? (Link -> mailto:pcre-dev@exim.org)
Hello,
First and foremost I want to thank you all developers for your amazing work
- it's really astonishing how all this does complex things without much
effort. Well actually effort is needed but it's definitely better than
writing a parser by hand.
Anyway so I've created some sort of C parser with your library that for the
moment handles unary and cast operators very well.
However I wanted to add binary ones too - currently for some reason this:
(?<addition>(?&addition)(\+\d)|\d)
Inhibits recursive loop. It's basically the exact same way it's written in
the standard - the idea is that the left hand side can contain infinite
times itself until it contains the right hand side.
My target is basically if I have multiple binary operators, with using
callouts in the right place, to get the operators in the right order, for
example if I have:
6 + 7 * 8 to give:
6
7
8
*
+
Because of priority. I basically had something going but I'm completely
rehauling it now (primarily because it wasn't working properly with
assignment).
Here is my github project <https://github.com/bsld/cparser>.
Thank you so much.
Happy holidays,
Alexander NIkolov
--
## List details at https://lists.exim.org/mailman/listinfo/pcre-dev