if in a qi::grammar I use this base rule
expression = (boost::spirit::ascii::string("aaa"));
it will parse "aaa" and nothing else
when I use this one ( notice the ! ) it parses nothing at all while I expect it to be successful on everything but "aaa"
expression = !(boost::spirit::ascii::string("aaa"));
Could I be missing some include? I'm using boost 1.54.0.
EDIT:
Sorry this is a bit drafty I modified the calculator example for my first trials...
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <iostream>
#include <string>
#include <boost/spirit/include/qi_lit.hpp>
#include <boost/spirit/include/qi_not_predicate.hpp>
/*
* \
__grammar_calculator.cpp
HEADERS += \
__grammar_calculator.h
*/
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////
// Our calculator grammar
///////////////////////////////////////////////////////////////////////////
template <typename Iterator>
struct calculator : qi::grammar<Iterator, int(), ascii::space_type>
{
calculator() : calculator::base_type(expression)
{
using qi::_val;
using qi::_1;
using qi::uint_;
using boost::spirit::qi::lit;
using boost::spirit::ascii::string;
expression = !(boost::spirit::ascii::string("aaa"));
}
qi::rule<Iterator, int(), ascii::space_type> expression, term, factor;
};
}
///////////////////////////////////////////////////////////////////////////////
// Main program
///////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
using boost::spirit::ascii::space;
typedef std::string::const_iterator iterator_type;
typedef client::calculator<iterator_type> calculator;
calculator calc; // Our grammar
std::string str;
int result;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, calc, space, result);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
//std::cout << "result = " << result << std::endl;
std::cout << "-------------------------\n";
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
//std::cout << "stopped at: \": " << rest << "\"\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}
EDIT 2:
Same one a bit cleaner:
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
#include <string>
#include <boost/spirit/include/qi_not_predicate.hpp>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
template <typename Iterator>
struct test : qi::grammar<Iterator>
{
test() : test::base_type(expression)
{
using boost::spirit::ascii::string;
expression = (boost::spirit::ascii::string("aaa"));
}
qi::rule<Iterator> expression;
};
}
int main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Expression parser...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "Type an expression...or [q or Q] to quit\n\n";
using boost::spirit::ascii::space;
typedef std::string::const_iterator iterator_type;
typedef client::test<iterator_type> test;
test tester; // Our grammar
std::string str;
while (std::getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, tester, space);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "-------------------------\n";
}
else
{
std::string rest(iter, end);
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}
ANSWER:
the problem came from the test:
if (r && iter == end)
as pointed out operator consumed nothing so iter!=end
in the below, sehe provided some alternatives.
For what it's worth:
operator&
and operator!
are zero-width lookahead assertions (they don't match but 'peek' and either succeed or fail on match).You'll want to know about
negating character-sets:
qi::char_("a-z") // matches lowercase letters
~qi::char_("a-z") // matches anything but lowercase letters
'parser subtraction' - think of exceptions:
qi::char_ - qi::char_("a-z") // equivalent to ~qi::char_("a-z")
qi::char_("a-z") - "keyword" // any lowercase letters, but not if it spells "keyword"
Edit so to scan forward to the next "%{" you'd do something like
qi::omit [ qi::char_ - "{%" ] >> "{%"
Note that you don't always need to 'wrap' literals in qi::lit
unless the expression doesn't already involve proto-expressions in the Qi domain.
Edit 2 An example of how to use !
and &
:
&qi::int_ >> +qi::char_ // parse a string, **iff** it starts with an integer
or
!keyword_list >> identifier // parse any identifier that's not a known keyword