我有一个如下的词位,用于表示字母数字的单词。
属性 = lexeme[+(boost::spirit::qi::alpha|boost::spirit::qi::digit)];
我想要一个语法规则,它会跳过不适合该规则的任何其他字符,并将这些字符放入向量中。
例如:输入:STR1 + STR2 % STR3() STR4 = STR5+ STR6
output: (STR1, STR2, STR3, STR4, STR6)
Run Code Online (Sandbox Code Playgroud)
我尝试过下面的语法,但在解析字符串中的第一个单词后它会跳过所有内容。我怎样才能改变它来解析我所描述的?
typedef std::vector<std::wstring> Attributes;
template <typename It, typename Skipper=boost::spirit::qi::space_type>
struct AttributeParser : boost::spirit::qi::grammar<It, Attributes(), Skipper>
{
AttributeParser() : AttributeParser::base_type(expression)
{
expression =
*( attributes [phx::push_back(qi::_val, qi::_1)])
>> qi::omit[*qi:char_]
;
attributes = qi::lexeme[+(boost::spirit::qi::alpha|qi::boost::spirit::qi::digit)];
BOOST_SPIRIT_DEBUG_NODE(expression);
BOOST_SPIRIT_DEBUG_NODE(attributes);
}
private:
boost::spirit::qi::rule<It, std::wstring() , Skipper> attributes;
boost::spirit::qi::rule<It, Attributes() , Skipper> expression;
};
Run Code Online (Sandbox Code Playgroud) 我有两种类型的表达式,我想解析并计算结果.
拟合表达式:+, - ,*,/和sqrt()函数; 例如:"2 + 3*sqrt(100*25)" - >应计算为152
功能:GetSubString()和ConcatenateStrings()如: "GetSubString( '100str1',0,3)" - >应被计算为100
我有2个单独的语法来解析这些表达式类型.现在我想结合这两个语法,并可以将这些表达式一起定义.
例如:
我试图通过使用置换运算符组合下面的2个语法.但它没有编译.
expr_ =
( *( (function_call_ ^ arithmeticexpression_)| string_ ));
Run Code Online (Sandbox Code Playgroud)
那么这是一个合并我的function_call_和arithmeticexpression_规则的正确方法,或者我该怎么做?
typedef boost::variant<int, float, double, std::wstring> RetValue;
RetValue CTranslationFunctions::GetSubString(RetValue const& str, RetValue position, RetValue len)
{
std::wstring strToCut;
size_t posInt = 0;
size_t lenInt = 0;
try
{
strToCut = boost::get<std::wstring>(str);
posInt = boost::get<int>(position);
lenInt = boost::get<int>(len); …Run Code Online (Sandbox Code Playgroud) 我想用boost精神来解析像这样的表达式
function1(arg1,arg2,function2(arg1,arg2,arg3),function3(arg1,arg2))
并调用相应的c ++函数.什么应该是语法解析上面的表达式并通过phoneix :: bind()调用相应的c ++函数?
我有两种类型的函数可以调用
1)字符串函数;
wstring GetSubString(wstring stringToCut,int position,int length); wstring GetStringToken(wstring stringToTokenize,wstring seperators,int tokenNumber);
2)返回整数的函数;
int GetCount();
int GetId(wstring srcId,wstring srcType);