1

I have the following grammar and when I compile I have many errors.

#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

template <typename It>
struct parser : qi::grammar<It, std::string()>
{
   parser() : parser::base_type(E)
   {
      using namespace qi;
      E = F >> E1;
      E1 = *( '+' >> F  | '-' >> F );
      F = ('(' >> E >> ')') | P | alnum;
      P = '@' >> +(~ char_('@') - V) >> V;
      V = string(".pv@") | string(".cv@");
   }

private:
   qi::rule<It, std::string()> E, E1, F, P, V;
};

Could you tell me What I have wrong? I know that the errors are with the * y alnum, but I don't know why?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • You mention `*y alnum` but this (besides it not being legal X++ syntax unless macros are involved) doesn't appear in you code? – sehe Jan 30 '14 at 21:43

1 Answers1

0

The code presented has no compilation issues.

I can imagine you're calling it in subtly wrong ways

  • if you're using qi::phrase_parse your grammar/rules will need to accept/specify the same skipper
  • the first iterator needs to be a reference on most qi::parse_* calls
  • if the first iterator is const_iterator, the second needs to be of the same type for things to work
  • etc.
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

template <typename It>
struct parser : qi::grammar<It, std::string()>
{
   parser() : parser::base_type(E)
   {
      using namespace qi;
      E = F >> E1;
      E1 = *( '+' >> F  | '-' >> F );
      F = ('(' >> E >> ')') | P | alnum;
      P = '@' >> +(~ char_('@') - V) >> V;
      V = string(".pv@") | string(".cv@");
   }

private:
   qi::rule<It, std::string()> E, E1, F, P, V;
};

int main()
{
    std::string const input("123");

    typedef std::string::const_iterator It;
    It f(input.begin()), l(input.end());
    std::string output;
    bool ok = qi::parse(f,l,parser<It>(),output);
    if (ok)
        std::cout << "Output: '" << output << "'\n";
}

See it Live On Coliru

sehe
  • 374,641
  • 47
  • 450
  • 633