I need to convert infix notations like the one below to n-ary prefix notation with Boost::Spirit, but I am failing at building on the answers from https://stackoverflow.com/a/8707598/1816477 et al.
This is what I am trying to parse:
not (xyz='a' or xyz='b' or xyz='c') and abc='s' xor (pqr ='v' and xyz='d')
and this LISP-styled format is what I am trying to provide as output (do not mind the indentation):
(xor (and (= pqr 'v') (= xyz 'd'))
(and (= abc 's')
(not (or (= xyz 'a')
(= xyz 'b')
(= xyz 'c')))))
So, the terms I try to parse consist of prefixed (not <expression>
) and infix expressions (<expression> and <expression> and ...
etc.), i.e.: assignments, negations and n-ary ands, ors, xors etc., implying operator precedence (or < xor < and < assignment < negation).
What I am failing at is getting the grammar right. Outputting to a suitable boost::variant
representing the parsed boolean expression I think I am able to accomplish. I am thinking of an output structure like this one:
struct prefixExpr;
struct infixExpr;
typedef boost::variant<
std::string, // identifiers, values etc.
boost::recursive_wrapper<prefixExpr>, // e.g. negation
boost::recursive_wrapper<infixExpr> // assignment, and, or, xor etc.
> expression;
struct prefixExpr {
std::string op; // currently only "not"
expression expr;
};
BOOST_FUSION_ADAPT_STRUCT(prefixExpr, op, expr)
struct infixExpr {
std::string op; // "and", "or", "xor", "="
std::vector<expression> exprs;
};
BOOST_FUSION_ADAPT_STRUCT(infixExpr, op, exprs)
What do I need to do to be able to parse expressions like the one mentioned above and convert them to a prefix notation?
I am using the boost 1.67.0 (the latest at the time of writing) and Visual Studio 15.7.3 (also the latest at the time of writing).