in reply to Re: Parsing Boolean expressions (hack)
in thread Parsing Boolean expressions

Nice. You can almost parse the expression with the same technique.
use strict; use warnings; use Data::Dump; my @exprs = ( "Y = A + (B*C)", "Y = A + (B' + (C*D)')", "Y = A*(B*(C'+D)')", ); for my $expr (@exprs) { print "$expr\n"; $expr =~ s/\(/[/g; $expr =~ s/\)/],/g; $expr =~ s/(\w+|[+*'=])/"$1",/g; my @parse = eval $expr; dd(\@parse); print "\n"; } __END__ Y = A + (B*C) ["Y", "=", "A", "+", ["B", "*", "C"]] Y = A + (B' + (C*D)') ["Y", "=", "A", "+", ["B", "'", "+", ["C", "*", "D"], "'"]] Y = A*(B*(C'+D)') ["Y", "=", "A", "*", ["B", "*", ["C", "'", "+", "D"], "'"]]

Replies are listed 'Best First'.
Re^3: Parsing Boolean expressions (hack)
by LanX (Saint) on Apr 23, 2017 at 15:50 UTC
    > You can almost parse the expression with the same technique.

    not really, for a syntax tree you would need an [OP, arg1, arg2] format.

    this

    ["B", "'", "+", ["C", "*", "D"], "'"]]

    doesn't help without further parsing because precedence is still not resolved.

    Rather

    [ "or", [ "not" , "B" ] , [ "not", [ "and", "C", "D"] ] ]

    without recursive parsing hard to achieve, I just delegated the hard part to Perl, deliberately ignoring what the OP didn't tell us yet.

    edit

    For instance if the = is not an assignment but a condition, he would want solutions for this equation system...

    (which I could produce at least by brute force for small variable sets, if we had a guarantied format)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Yeah, that's why I said "almost." You need to postprocess it a bit to get a proper syntax tree.
        > You need to postprocess it a bit to get a proper syntax tree.

        well ... a bit

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!