我有一个解析器,它基本上打印出堆栈机的操作,并根据给定的运算符优先级给出一些表达式。我的目标是尽可能优化速度。我读过了一篇关于 qi 优化的文章 https://code.google.com/p/scribblings-by-apoch/wiki/OptimizingBoostSpirit提供这个示例代码 https://code.google.com/p/scribblings-by-apoch/wiki/BoostSpiritDeferredConstructor。我了解主文章中描述的优化要点,但我不清楚如何将其集成到我的代码中。
这是我的解析器的以下工作示例。我已经尝试过使用它来优化它raw[]
提供基本迭代器。 phoenix 操作调用必须给出字符串或迭代器,通过它们可以创建字符串;这些函数的真实版本并不简单,它们的功能尚无法在解析时评估:
#include <iostream>
#include <vector>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/phoenix_bind.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
using std::endl;
using std::cout;
using std::string;
using std::vector;
void fPushOp(const char* op){
cout << "PushOp: " << op << endl;
}
void fPushInt(const boost::iterator_range<string::const_iterator>& my_str){
cout << "PushInt: " << my_str << endl;
}
template<typename Iterator, typename Skipper = qi::space_type>
struct Calculator : public qi::grammar<Iterator, Skipper> {
qi::rule<Iterator, Skipper>
expression, logical_or_expression, logical_and_expression, negate_expression, series_expression,
single_expression, inclusive_or_expression, exclusive_or_expression, and_expression, equality_expression,
relational_expression, shift_expression, additive_expression, multiplicative_expression,
term, complement_factor, factor, result, integer, variable, variable_combo, word, prefix;
qi::rule<Iterator> number;
Calculator() : Calculator::base_type(result)
{
number =
qi::raw[
("0x" >> +qi::char_("0-9a-fA-F"))
| ("0b" >> +qi::char_("0-1"))
| ("0" >> +qi::char_("0-7"))
| (+qi::char_("0-9"))
] [phx::bind(&fPushInt, qi::_1)]
;
integer =
number
| ('-' >> number) [phx::bind(&fPushOp, "OP_UNARY_MINUS")]
;
variable =
((qi::alpha | qi::char_('_'))
>> *(qi::alnum | qi::char_('_'))
>> '['
>> (+(qi::alnum | qi::char_('_') | qi::char_(','))
| ('\'' >> *~qi::char_('\'') >> '\''))
>> ']')
| ((qi::alpha | qi::char_('_')) >> *(qi::alnum | qi::char_('_')))
;
variable_combo =
qi::raw [
variable >> *(qi::char_('.') >> variable)
] [phx::bind(&fPushInt, qi::_1)]
;
word =
qi::raw[
variable
] [phx::bind(&fPushInt, qi::_1)]
;
factor =
("ceil(" >> expression >> ')') [phx::bind(&fPushOp, "OP_CEIL")]
| ("wrap(" >> expression >> ')') [phx::bind(&fPushOp, "OP_WRAP")]
| ("abs(" >> expression >> ')') [phx::bind(&fPushOp, "OP_ABS")]
| ("count1(" >> expression >> ')') [phx::bind(&fPushOp, "OP_COUNT1")]
| ("pick(" >> expression >> ')') [phx::bind(&fPushOp, "OP_PICK")]
| ("defined(" >> expression >> ')') [phx::bind(&fPushOp, "OP_DEF")]
| ("string_equal(" >> word >> ',' >> word >> ')') [phx::bind(&fPushOp, "OP_STREQ")]
| ("string_contains(" >> word >> ',' >> word >> ')') [phx::bind(&fPushOp, "OP_STRCON")]
| ("lsl(" >> single_expression >> ',' >> single_expression >> ',' >> number >> ')') [phx::bind(&fPushOp, "OP_LSL")]
| ("lsr(" >> single_expression >> ',' >> single_expression >> ')') [phx::bind(&fPushOp, "OP_LSR")]
| ("asr(" >> single_expression >> ',' >> single_expression >> ',' >> number >> ')') [phx::bind(&fPushOp, "OP_ASR")]
| ("ror(" >> single_expression >> ',' >> single_expression >> ',' >> number >> ')') [phx::bind(&fPushOp, "OP_ROR")]
| ("rrx(" >> single_expression >> ',' >> single_expression >> ',' >> single_expression >> ',' >> number >> ')')[phx::bind(&fPushOp, "OP_RRX")]
| ('(' >> expression >> ')')
| variable_combo
| integer
;
complement_factor = factor
| ('~' >> factor) [phx::bind(&fPushOp, "OP_COMPLEMENT")]
;
term = complement_factor
>> *( (".." >> complement_factor) [phx::bind(&fPushOp, "OP_LEGER")]
| ('\\' >> complement_factor) [phx::bind(&fPushOp, "OP_MASK")]
);
multiplicative_expression = term
>> *( ('/' >> term) [phx::bind(&fPushOp, "OP_DIV")]
| ('%' >> term) [phx::bind(&fPushOp, "OP_MOD")]
| ('*' >> term) [phx::bind(&fPushOp, "OP_MUL")]
);
additive_expression = multiplicative_expression
>> *( ('+' >> multiplicative_expression) [phx::bind(&fPushOp, "OP_ADD")]
| ('-' >> multiplicative_expression) [phx::bind(&fPushOp, "OP_SUB")]
);
shift_expression = additive_expression
>> *( (">>" >> additive_expression) [phx::bind(&fPushOp, "OP_SRL")]
| ("<<" >> additive_expression) [phx::bind(&fPushOp, "OP_SLL")]
);
relational_expression = shift_expression
>> *( ('<' >> shift_expression) [phx::bind(&fPushOp, "OP_LT")]
| ('>' >> shift_expression) [phx::bind(&fPushOp, "OP_GT")]
| ("<=" >> shift_expression)[phx::bind(&fPushOp, "OP_LET")]
| (">=" >> shift_expression)[phx::bind(&fPushOp, "OP_GET")]
);
equality_expression = relational_expression
>> *( ("==" >> relational_expression)[phx::bind(&fPushOp, "OP_EQ")]
| ("!=" >> relational_expression)[phx::bind(&fPushOp, "OP_NEQ")]
);
and_expression = equality_expression
>> *(('&' >> equality_expression) [phx::bind(&fPushOp, "OP_AND")]);
exclusive_or_expression = and_expression
>> *(('^' >> and_expression) [phx::bind(&fPushOp, "OP_XOR")]);
inclusive_or_expression = exclusive_or_expression
>> *(('|' >> exclusive_or_expression) [phx::bind(&fPushOp, "OP_OR")]);
single_expression = inclusive_or_expression;
series_expression = inclusive_or_expression
>> *((',' >> inclusive_or_expression) [phx::bind(&fPushOp, "OP_SERIES")]);
negate_expression = series_expression
| ('!' >> series_expression) [phx::bind(&fPushOp, "OP_NEGATE")];
logical_and_expression = negate_expression
>> *(("&&" >> negate_expression) [phx::bind(&fPushOp, "OP_LOGICAL_AND")]);
logical_or_expression = logical_and_expression
>> *(("||" >> logical_and_expression) [phx::bind(&fPushOp, "OP_LOGICAL_OR")]);
expression = logical_or_expression;
result = expression;
}
};
int main(){
Calculator<string::const_iterator> calc;
const string expr("0xff0000 >> 3 && 3 + (!9) | (0,200)");
cout << "Expression: " << expr << endl;
string::const_iterator it = expr.begin();
phrase_parse(it, expr.end(), calc, qi::space);
cout << "Remaining: " << (string(it,expr.end())) << endl;
return 0;
}
另外,我读到此 pdf 中有关 utree 的幻灯片 https://github.com/boostcon/2011_presentations/raw/master/fri/utree_talk.pdf我正在尝试找出如果可能的话如何使用utree
输出而不是语义动作,因为显然这样的事情是邪恶的。有人可以提供或向我指出一个关于如何构建utree
然后可以将其送入堆栈机以按顺序打印出操作?