I'm trying to write a parser for a C-like language with boost::spirit that uses inherited attributes to transfer information about variable scope. For instance, "namespace a { var b }" would pass "a" as an attribute to the parser for "var b".
I'm having trouble getting a basic parser using inherited attributes to compile this code:
#ifndef CPARSER_DEF_HPP
#define CPARSER_DEF_HPP
#include <string>
#include <boost/spirit/include/qi.hpp>
namespace encoding = boost::spirit::ascii;
using boost::spirit::unused_type;
using boost::spirit::qi::rule;
template <typename Iterator>
struct cparser : boost::spirit::qi::grammar<
Iterator,
std::string(std::string),
encoding::space_type
>
{
rule<Iterator, std::string(std::string), encoding::space_type> start;
rule<Iterator, std::string(std::string), encoding::space_type> sym_list;
cparser() :
cparser::base_type(start)
{
sym_list = encoding::string(boost::spirit::qi::_r1);
start = sym_list(boost::spirit::qi::_r1);
}
};
#endif
This parser is instantiated in main() with cparser<std::string::const_iterator> parser
.
I believe this parser should accept a std::string as its inherited attribute, parse input matching this string, then return the string as a synthesised attribute. This example code does not compile, and I cannot figure out why. I have been compiling with GCC and Clang with C++11 enabled. The output from either compiler is huge (approx. 1000 lines), and I cannot make any sense of it. Is there a problem with the use of boost::spirit::qi::_r1
? A problem with std::string(std::string)
in the rule declarations?
Thanks in advance for your help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…