Home > Back-end >  The boost spirit for help
The boost spirit for help

Time:12-01

Source as follows, to based on boost: : the spirit of the library to write a logical expression of computing, following the spirit of the code from the Internet to find a mathematical expression of calculation of the source code change,
Mathematical expression of the original structure operator_ variable is a char in the operation, the corresponding mathematical expressions of + - */, now I need to support the operator_==, & gt;=, & lt;=, & gt; , <, & amp; & , and | | so char don't satisfy my demands, to replace the type that operator_ STD: : string, compiled by (g + + - STD==c + + 11 - g main. Cc - o main. O) but implementation will throw an exception,

The terminate called after throwing an instance of a 'boost: : exception_detail: : clone_impl & lt; Boost: : exception_detail: : error_info_injector & lt; Boost: : spirit: : qi: : expectation_failure & lt; __gnu_cxx: : __normal_iterator & lt; , char * const STD: : string>> '
What () : boost: : spirit: : qi: : expectation_failure
Aborted

Pray god help me and see how this type need to change the?

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# include & lt; The boost/config/warning_disable. Hpp>
# include & lt; Boost/spirit/include/qi. Hpp>
# include & lt; The boost/variant/recursive_variant. Hpp>
# include & lt; The boost/variant/apply_visitor. Hpp>
# include & lt; Boost/fusion/include/adapt_struct. Hpp>
# include & lt; Boost/spirit/include/phoenix. Hpp>
# include & lt; Boost/foreach. Hpp>

# include & lt; Iostream>
# include & lt; String>
# include & lt; Set>

The namespace client {
The namespace ast
{
Struct nil {};
Struct signed_;
Struct the program;

Typedef boost: : variant
Nil
, double
The boost: : recursive_wrapper & lt; Signed_ & gt;
The boost: : recursive_wrapper & lt; Program>

Operand;

Struct signed_
{
Char sign;
Operand operand_;
};

Struct operation
{
STD: : string operator_;
Operand operand_;
};

Struct program
{
Operand first;
STD: : list Rest;
};
}
}

BOOST_FUSION_ADAPT_STRUCT (
Client: : ast: : signed_,
(char, sign)
(client: : ast: : operand, operand_)
)

BOOST_FUSION_ADAPT_STRUCT (
Client: : ast: : operation,
(STD: : string, operator_)
(client: : ast: : operand, operand_)
)

BOOST_FUSION_ADAPT_STRUCT (
Client: : ast: : program,
(client: : ast: : operand, first)
(STD: : list , rest)
)

The namespace client {
The namespace ast
{
Struct eval
{
Typedef double result_type;

Double operator () (nil) const {BOOST_ASSERT (0); return 0; }
Double operator () (double n) const {return n. }

Double operator () (operation const& X, double LHS) const
{
Double RHS=boost: : apply_visitor (* this, x.o. perand_);

If (x.o. perator_====") return LHS==RHS;
If (x.o. perator_=="& amp; &" ) return LHS & amp; & RHS.
If (x.o. perator_=="| |") return LHS | | RHS.
If (x.o. perator_=="& gt;" ) return LHS & gt; RHS.
If (x.o. perator_=="& lt;" ) return LHS & lt; RHS.

BOOST_ASSERT (0);
return 0;
}

Double operator () (signed_ const& X) const
{
{
Double RHS=boost: : apply_visitor (* this, x.o. perand_);

If (x.o. perator_====") return LHS==RHS;
If (x.o. perator_=="& amp; &" ) return LHS & amp; & RHS.
If (x.o. perator_=="| |") return LHS | | RHS.
If (x.o. perator_=="& gt;" ) return LHS & gt; RHS.
If (x.o. perator_=="& lt;" ) return LHS & lt; RHS.

BOOST_ASSERT (0);
return 0;
}

Double operator () (signed_ const& X) const
{
Double RHS=boost: : apply_visitor (* this, x.o. perand_);

If (x.s ign=='-') return - RHS;
If (x.s ign=='+') return + RHS.

BOOST_ASSERT (0);
return 0;
}

Double operator () (the program const& X) const
{
Double state=boost: : apply_visitor (* this, x.f irst);
BOOST_FOREACH (operation const& Oper, x.r est)
{
State=(* this) (oper, state);
}
Return the state;
}
};
}
}

The namespace client
{
The namespace qi=boost: : spirit: : qi;
The namespace ASCII=boost: : spirit: : ASCII;

The template & lt; Typename Iterator>
Struct calculator: qi: : grammar{
The calculator (STD: : set & Collect_references) : the calculator: : base_type (expression)
{
Qi: : char_type char_;
Qi: : double_type doubleParser_;

SymbolTable. Add (" AD ", 0).
SymbolTable. Add (" unlock ", 1);
SymbolTable. Add (" lock ", 0).
SymbolTable. Add (" op ", 0).
SymbolTable. Add (" ena ", 1);

The namespace px=boost: : phoenix;

Expression=
The term
> * ((char_ (" & amp; &" ) & gt; Term)
| (char_ (" | | ") & gt; Term)
)
;

The term=
Factor
> * ((char_ ("==") & gt; Factor)
| (char_ (& gt; "" ) & gt; Factor)
| (char_ (& lt; "" ) & gt; Factor)
)
;

Variable %=
& Qi: : as_string [qi: : raw [symbolTable]]
[px: : insert (px: : ref (collect_references), qi: : _1)]
> SymbolTable
;

Factor=
DoubleParser_
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related