opaque_rule_parser.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*==============================================================================
  2. Copyright (c) 2006 Tobias Schwinger
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. //------------------------------------------------------------------------------
  9. // This example demonstrates the opaque rule parser.
  10. // See boost/spirit/include/rule_parser.hpp for details.
  11. //------------------------------------------------------------------------------
  12. #include <iostream>
  13. #include <boost/typeof/typeof.hpp>
  14. #include <boost/spirit/include/classic_core.hpp>
  15. #include <boost/spirit/include/classic_typeof.hpp>
  16. #include <boost/spirit/include/classic_rule_parser.hpp>
  17. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  18. namespace my_project { namespace my_grammar {
  19. using namespace BOOST_SPIRIT_CLASSIC_NS;
  20. #define BOOST_SPIRIT__NAMESPACE (2,(my_project,my_grammar))
  21. BOOST_SPIRIT_OPAQUE_RULE_PARSER(word,
  22. (1,( ((char const *),str) )),
  23. -,
  24. lexeme_d[ str >> +space_p ]
  25. )
  26. BOOST_SPIRIT_OPAQUE_RULE_PARSER(main,
  27. -,-,
  28. *( word("dup") | word("swap") | word("drop") | word("rot") | word("tuck") )
  29. )
  30. #undef BOOST_SPIRIT__NAMESPACE
  31. } } // namespace my_project::my_grammar
  32. int main()
  33. {
  34. std::string str;
  35. while (std::getline(std::cin, str))
  36. {
  37. if (str.empty())
  38. break;
  39. str += '\n';
  40. if (BOOST_SPIRIT_CLASSIC_NS::parse(str.c_str(), my_project::my_grammar::main).full)
  41. std::cout << "\nOK." << std::endl;
  42. else
  43. std::cout << "\nERROR." << std::endl;
  44. }
  45. return 0;
  46. }