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

Top Page

Reply to this message
Author: Philip Hazel
Date:  
To: Zoltán Herczeg
CC: pcre-dev@exim.org, sasho648
Subject: Re: [pcre-dev] Need help with implementing a parser (binary operators) - pcre2
As an aside, note that if you have a BNF definition of some syntax, it is
almost trivial to translate it into a PCRE/Perl pattern using named groups.
There are some examples near the end of the testdata/testinput1 file in the
PCRE2 distribution (line 5890); one matches email addresses, the other
matches valid regex patterns.
Regards,
Philip


On Fri, 25 Dec 2020 at 09:23, Zoltán Herczeg <hzmester@???> wrote:

> 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@???) >
> 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
>
> --
> ## List details at https://lists.exim.org/mailman/listinfo/pcre-dev
>