无法编译用 Boost::Spirit 库编写的简单解析器
Not able to compile a simple parser written in Boost::Spirit library
下面是一个最小尺寸的程序,我无法在Boost::spirit库中编译。
#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/lexical_cast.hpp>
using namespace boost::spirit;
template <typename Iterator>
struct z3_exp_grammars : qi::grammar<Iterator, std::string(), ascii::space_type>
{
z3_exp_grammars() : z3_exp_grammars::base_type(text)
{
text = qi::int_[qi::_val = boost::lexical_cast<std::string>(qi::_1)];
}
qi::rule<Iterator, std::string(), ascii::space_type> text;
};
int main(){
std::string test("3");
std::string result;
std::string::const_iterator beg = test.begin();
std::string::const_iterator end = test.end();
typedef z3_exp_grammars<std::string::const_iterator> z3_exp_grammars;
z3_exp_grammars par;
qi::phrase_parse(beg,end,par,result);
std::cout<<"Result is "<<result<<std::endl;
}
我希望在变量结果中看到字符串 3,但代码没有编译。与其查看错误日志(由于模板而非常具有威胁性),如果有人可以在代码中发现我的错误,那就太好了。感谢您的帮助。
使用编译与T.C 给出的相同代码后收到的编译错误更新问题。
Test.cpp:9:11: error: expected nested-name-specifier before ‘result_type’
Test.cpp:9:11: error: using-declaration for non-member at class scope
Test.cpp:9:23: error: expected ‘;’ before ‘=’ token
Test.cpp:9:23: error: expected unqualified-id before ‘=’ token
In file included from /home/jaganmohini/Downloads/boost_1_58_0/boost/proto/proto_fwd.hpp:28:0,
from /home/jaganmohini/Downloads/boost_1_58_0/boost/phoenix/core/limits.hpp:26,
from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/include/phoenix_limits.hpp:11,
from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/support/meta_compiler.hpp:16,
from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/qi/meta_compiler.hpp:14,
from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/qi/action/action.hpp:14,
from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/qi/action.hpp:14,
from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/home/qi.hpp:14,
from /home/jaganmohini/Downloads/boost_1_58_0/boost/spirit/include/qi.hpp:16,
from Test.cpp:3:
/home/jaganmohini/Downloads/boost_1_58_0/boost/utility/result_of.hpp: In instantiation of ‘boost::detail::result_of_nested_result<const to_string_, const to_string_(int&)>’:
/home/jaganmohini/Downloads/boost_1_58_0/boost/utility/result_of.hpp:197:1: instantiated from ‘boost::detail::tr1_result_of_impl<const to_string_, const to_string_(int&), false>’
两个独立的问题:
-
phrase_parse
期待船长作为其第四个论点。因此,您应该使用qi::phrase_parse(beg, end, par, ascii::space, result);
-
你不能在这样的凤凰占位符上使用
boost::lexical_cast
。它被热切地评估。你需要创建一个惰性函数:struct to_string_ { using result_type = std::string; template<class T> std::string operator()(const T& arg) const { return boost::lexical_cast<std::string>(arg); } }; boost::phoenix::function<to_string_> to_string;
并做
text = qi::int_[qi::_val = to_string(qi::_1)];
演示。
你不能像qi::_1
这样对"懒惰"的演员执行"渴望"的功能。
qi::_1
的类型很简单qi::_1_type
:它是一个占位符。
要么你必须
- 调整功能
- 创建/包装一个惰性函子(
boost::phoenix::function<>
或BOOST_PHOENIX_ADAPT_FUNCTION
等) - 使用
phx::bind
例如与λ一起使用 - 传递可调用的原始语义操作(请参阅文档)
在这种情况下,我不会做上述任何一项,因为在实现解析器时使用lexical_cast
是可疑的(这就像将银行帐户的收据放在床下的袜子里一样。或者像开车去自行车棚一样)。
我会在这里解析为 int(因为这正是qi::int_
旨在匹配和公开的内容)。
如果要将匹配的输入字符串作为 int,请使用
text = qi::raw [ qi::int_ ];
如果你坚持"重新格式化",我会做:
住在科里鲁
#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/lexical_cast.hpp>
namespace qi = boost::spirit::qi;
int main()
{
using It = std::string::const_iterator;
std::string const test("3");
It beg = test.begin(), end = test.end();
int result;
if (qi::phrase_parse(beg, end, qi::int_, qi::space, result))
std::cout << "Result is " << boost::lexical_cast<std::string>(result) << std::endl;
}
笔记:
- 单独的关注点(解析与表示)
- 在
phrase_parse
:)通过船长
相关文章:
- 使用简单类型列表实现的指数编译时间.为什么
- 如何使用cmake和Visual Studio 2019编译简单的Windows应用程序?
- 无法编译简单的C++17程序
- boost::spirit--试图编译大多数简单代码的编译器错误
- Cmake无法在qt-creater/collect2上编译简单的测试程序:错误:ld
- 编译简单的多文件类程序时出现未定义的引用错误
- macos GCC 工具链编译简单 hello 世界的问题
- 使用提升过程编译简单的应用程序会生成错误
- 无法使用crfsuite的C++ API编译一个非常简单的Tagger示例
- CMake 和 Visual Studio 2015 - C 编译器 cl.exe 无法编译简单的测试程序
- Centos6 gcc6:编译简单的 c++11 测试文件时,默认的 ABI 不拾取
- C++编译器 g++.exe 无法编译简单的测试程序 - 确定 CXX 编译器是否工作失败
- 具有编译问题的简单(递归)可变参数模板"accumulate_for"函数
- 为什么一个简单的"Hello World"风格的程序不能用Turbo C++编译?
- 简单的 SFINAE 示例以重载函数不编译
- 未经GCC优化而编译的简单C++程序不会产生预期的结果
- 为什么我不能编译这个简单的线程测试?
- 简单的 C++11 哈希函数无法编译
- C++简单编译错误
- 为什么我的简单编译会失败?