不能编译boost spirit example4.cpp
Can`t compile boost spirit example4.cpp
我正在继续学习Boost Spirit库,我有一些问题的例子,我无法编译。您可以在这里找到示例的源代码:源位置。您还可以查看此代码并在Coliru上编译结果。
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <fstream>
#include <string>
using namespace boost::spirit;
using boost::phoenix::val;
///////////////////////////////////////////////////////////////////////////////
// Token definition
///////////////////////////////////////////////////////////////////////////////
template <typename Lexer>
struct example4_tokens : lex::lexer<Lexer>
{
example4_tokens()
{
identifier = "[a-zA-Z_][a-zA-Z0-9_]*";
constant = "[0-9]+";
if_ = "if";
else_ = "else";
while_ = "while";
this->self = lex::token_def<>('(') | ')' | '{' | '}' | '=' | ';' | constant;
this->self += if_ | else_ | while_ | identifier;
this->self("WS")
= lex::token_def<>("[ \t\n]+")
| "\/\*[^*]*\*+([^/*][^*]*\*+)*\/"
;
}
lex::token_def<> if_, else_, while_;
lex::token_def<std::string> identifier;
lex::token_def<unsigned int> constant;
//]
};
template <typename Iterator, typename Lexer>
struct example4_grammar
: qi::grammar<Iterator, qi::in_state_skipper<Lexer> >
{
template <typename TokenDef>
example4_grammar(TokenDef const& tok)
: example4_grammar::base_type(program)
{
using boost::spirit::_val;
program
= +block
;
block
= '{' >> *statement >> '}'
;
statement
= assignment
| if_stmt
| while_stmt
;
assignment
= (tok.identifier >> '=' >> expression >> ';')
[
std::cout << val("assignment statement to: ") << _1 << "n"
]
;
if_stmt
= ( tok.if_ >> '(' >> expression >> ')' >> block
>> -(tok.else_ >> block)
)
[
std::cout << val("if expression: ") << _2 << "n"
]
;
while_stmt
= (tok.while_ >> '(' >> expression >> ')' >> block)
[
std::cout << val("while expression: ") << _2 << "n"
]
;
expression
= tok.identifier [ _val = _1 ]
| tok.constant [ _val = _1 ]
;
}
typedef boost::variant<unsigned int, std::string> expression_type;
qi::rule<Iterator, qi::in_state_skipper<Lexer> > program, block, statement;
qi::rule<Iterator, qi::in_state_skipper<Lexer> > assignment, if_stmt;
qi::rule<Iterator, qi::in_state_skipper<Lexer> > while_stmt;
qi::rule<Iterator, expression_type(), qi::in_state_skipper<Lexer> > expression;
};
///////////////////////////////////////////////////////////////////////////////
int main()
{
typedef std::string::iterator base_iterator_type;
typedef lex::lexertl::token<base_iterator_type, boost::mpl::vector<unsigned int, std::string> > token_type;
typedef lex::lexertl::lexer<token_type> lexer_type;
typedef example4_tokens<lexer_type> example4_tokens;
typedef example4_tokens::iterator_type iterator_type;
typedef example4_grammar<iterator_type, example4_tokens::lexer_def> example4_grammar;
example4_tokens tokens; // Our lexer
example4_grammar calc(tokens); // Our parser
std::string str;
std::string::iterator it = str.begin();
iterator_type iter = tokens.begin(it, str.end());
iterator_type end = tokens.end();
bool r = qi::phrase_parse(iter, end, calc, qi::in_state("WS")[tokens.self]);
if (r && iter == end)
{
std::cout << "-------------------------n";
std::cout << "Parsing succeededn";
std::cout << "-------------------------n";
}
else
{
std::cout << "-------------------------n";
std::cout << "Parsing failedn";
std::cout << "-------------------------n";
}
std::cout << "Bye... :-) nn";
return 0;
}
当我尝试编译它时,我收到了很多错误,请参阅Coliru上的完整列表。
这个例子有什么问题?编译它需要修改什么,为什么需要修改?如何确定哪些编译过程失败?
如Fsmv所述,应增加以下define
:
#define BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT
之后,可以编译示例。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 内置函数可查看CPP中的成员变量
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- Cpp-Tuple使用带有变量的get
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何通过cpp程序运行shell脚本
- 使用2个键的cpp-stl::优先级队列排序不正确
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我需要知道编译器如何在cpp中使用析构函数
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 无法在UE4中包含BP类到CPP类
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 不能编译boost spirit example4.cpp