提升::精神:基本"logical and"表达式解析
Boost::Spirit : Basic "logical and" expression parsing
我正在尝试学习Boost::Spirit的基础知识,但进展不顺利。我正在尝试解析用 c++ 语法编写的简单"逻辑和"表达式。由于某种原因,我无法让空间跳跃工作。
这是我到目前为止的代码
template <typename Iterator>
struct boolGrammar : public qi::grammar<Iterator, bool>
{
public:
boolGrammar() : boolGrammar::base_type(expression)
{
andExpr = (qi::lit(L"1") >> qi::lit(L"&&") >> qi::lit(L"1"))[qi::_val = true];
}
qi::rule<Iterator, bool> andExpr;
};
bool conditionEvalAndParse(std::wstring condition)
{
boolGrammar<std::wstring::iterator> g;
bool result = false;
std::wstring::iterator it = condition.begin();
bool parseResult = qi::phrase_parse(it, condition.end(), g, boost::spirit::standard_wide::space , result);
if (parseResult) {
return result;
}
else
{
std::wcout << L"Failed to parse condition " << condition << L". The following wasn't parsed : " << std::wstring(condition, it - condition.begin(), std::wstring::npos) << std::endl;
return false;
}
}
在我的测试代码中,我调用:
conditionEvalAndParse(L"1&&1");
conditionEvalAndParse(L"1 && 1");
果然,我得到了一个可爱的控制台输出:
"Failed to parse condition 1 && 1. The following wasn't parsed : 1 && 1"
有人愿意指出新手的错误吗? :)
通过将船长添加为模板参数来解决,如@Richard Hodges前面的问题所示:
template <typename Iterator, typename Skipper = boost::spirit::standard_wide::space_type>
struct boolGrammar : public qi::grammar<Iterator, bool, Skipper>
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 提升精神:解析布尔表达式并简化为规范范式
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 使用正则表达式regex_search在字符串中查找字符串
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 概念中的cv限定符需要表达式参数列表
- 为什么constexpr的性能比正常表达式差
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 对于结构,表达式必须是可修改的ivalue
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 为什么 Clang 不允许"and"作为函数名称?
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 将fold表达式与std::一起用于两个元组
- 断言中的Fold表达式在某些计算机上编译,但在其他计算机上不编译
- 标记 '","' 之前的预期主表达式
- 保证逻辑 AND 表达式中的函数调用
- 提升::精神:基本"logical and"表达式解析
- 关于正则表达式与Boost的逻辑AND和OR匹配的说明
- 在boost spirit布尔表达式解析器中支持隐含的AND操作