I use spirit in boost_1.59 to parse a c-like language(named stone originally written by java). But I have trouble in using boost::spirit::qi to parse the c-style string in stone. The whole code is at coliru.
The parser rule I wrote is listed below.
template <typename Iterator, typename Lexer>
struct StoneGrammar
: qi::grammar<Iterator, qi::in_state_skipper<Lexer> >
{
template <typename TokenDef>
StoneGrammar(TokenDef const& tok)
: StoneGrammar::base_type(program)
{
using boost::spirit::_val;
using boost::spirit::no_skip;
/* Grammar for Stone:
primary : "(" expr ")" | NUMBER | IDENTIFIER | STRING
factor : "-" primary | primary
expr : factor { OP factor }
block : "{" [ statement ] { (";" | EOL) [ statement ] } "}"
//block : "{" [ statement ] { (";" | EOL) [ statement ] } "}"
simple : expr
statement : "if" expr block [ "else" block ]
| "while" expr block
| simple
program : { statement [";"] }
*/
program
= -statement >> (qi::lit(';') | '\n')
;
statement
= if_stmt
| while_stmt
| simple_stmt
;
if_stmt
= "if" >> expression >> block >> -("else" >> block)
;
while_stmt
= "while" >> expression >> block;
simple_stmt
= expression
;
block
= '{' >> -statement >> *((qi::lit(';') | '\n') >> -statement) >> '}'
;
expression
= factor >> *(op >> factor)
;
factor
= '-' >> primary
| primary
;
op
= qi::lit('+')
| '-'
| '*'
| '/'
| qi::lit('=')[std::cout << _1 << "= parserd" << std::endl]
| qi::lit("||")[std::cout << _1 << " || parsed" << std::endl]
;
primary
= '(' >> expression >> ')'
| tok.identifier[std::cout << _1 << std::endl]
| tok.number[std::cout << _1 << std::endl]
| unesc_str
;
unesc_char.add("\\a", '\a')("\\b", '\b')("\\f", '\f')("\\n", '\n')
("\\r", '\r')("\\t", '\t')("\\v", '\v')
("\\\\", '\\')("\\\'", '\'')("\\\"", '\"')
;
unesc_str = qi::lit('"') >> (qi::alnum | ("\\x" >> qi::hex)) >> '"';
}
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, qi::in_state_skipper<Lexer> > simple_stmt;
qi::rule<Iterator, qi::in_state_skipper<Lexer> > op;
qi::rule<Iterator, qi::in_state_skipper<Lexer> > factor;
qi::rule<Iterator, qi::in_state_skipper<Lexer> > primary;
qi::rule<Iterator, std::string(), qi::in_state_skipper<Lexer> > string_literal;
qi::rule<Iterator, std::string(), qi::in_state_skipper<Lexer> > unesc_str;
qi::symbols<char const, char const> unesc_char;
// the expression is the only rule having a return value
qi::rule<Iterator, expression_type(), qi::in_state_skipper<Lexer> > expression;
};
The parser use some token definition is listed below.
template <typename Lexer>
struct StoneToken : lex::lexer<Lexer>
{
StoneToken()
{
// define the tokens to match
identifier = "[a-zA-Z_][a-zA-Z0-9_]*";
number = "[0-9]+";
// !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
// associate the tokens and the token set with the lexer
this->self
= lex::token_def<>('(') | ')' | '{' | '}' | '[' | ']'
| '+' | '-' | '*' | '/'
| '='
| "==" | "<=" | ">="
| '>' | '<' | "&&"
//| "\||"
| '!' | '^' | '|' | '~' | '&'
| '%'
| '\'' | ',' | '"' | '\n' | ';' | '.' | '_'
| "kkk"
;
//this->self += number | if_ | else_ | while_ | identifier;
this->self += number | identifier;
// define the whitespace to ignore (spaces, tabs, and C-style
// comments)
this->self("WS")
= lex::token_def<>("[ \\t]+")
| R"(\/\*[^*]*\*+([^/*][^*]*\*+)*\/)"
| R"(\/\/[^\n]*\n)"
;
}
lex::token_def<std::string> identifier, op;
lex::token_def<unsigned int> number;
};
However, the compiler cannot compile the unesc_str rule. The beginning part of error report from clang++-3.6 is listed below.
/usr/include/boost/type_traits/make_unsigned.hpp:38:4: error: static_assert failed
"(::boost::type_traits::ice_or< ::boost::is_integral<T>::value,
::boost::is_enum<T>::value>::value)"
BOOST_STATIC_ASSERT(
^
/usr/include/boost/static_assert.hpp:78:41: note: expanded from macro 'BOOST_STATIC_ASSERT'
# define BOOST_STATIC_ASSERT( ... ) static_assert(__VA_ARGS__, #__VA_ARGS__)
^
/usr/include/boost/type_traits/make_unsigned.hpp:146:70: note: in instantiation of template class
'boost::detail::make_unsigned_imp<boost::spirit::lex::lexertl::token<__gnu_cxx::__normal_iterator<char
*, std::basic_string<char> >, boost::mpl::vector<unsigned int, std::basic_string<char>, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>,
mpl_::bool_<true>, unsigned long> >' requested here
BOOST_TT_AUX_TYPE_TRAIT_DEF1(make_unsigned,T,typename boost::detail::make_unsigned_imp<T>::type)
^
/usr/include/boost/type_traits/detail/type_trait_def.hpp:21:13: note: expanded from macro
'BOOST_TT_AUX_TYPE_TRAIT_DEF1'
typedef result type; \
^
/usr/include/boost/spirit/home/support/char_class.hpp:51:34: note: in instantiation of template class
'boost::make_unsigned<boost::spirit::lex::lexertl::token<__gnu_cxx::__normal_iterator<char *,
std::basic_string<char> >, boost::mpl::vector<unsigned int, std::basic_string<char>, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na,
mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>,
mpl_::bool_<true>, unsigned long> >' requested here
typedef typename make_unsigned<SourceChar>::type USourceChar;
Howerver, I found out that if i deleted the qi::alnum in unesc_str rule, the compilation was successful.
Waiting for answers!