dynamic_rule.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*=============================================================================
  2. Copyright (c) 2003 Jonathan de Halleux (dehalleux@pelikhan.com)
  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 shows how the assign operator can be used to modify
  10. // rules with semantic actions
  11. //
  12. // First we show the basic spirit without (without any dynamic feature),
  13. // then we show how to use assign_a to make it dynamic,
  14. //
  15. // the grammar has to parse abcabc... sequences
  16. ///////////////////////////////////////////////////////////////////////////////
  17. #include <iostream>
  18. #define BOOST_SPIRIT_DEBUG
  19. #include <boost/spirit.hpp>
  20. #include <boost/spirit/include/classic_assign_actor.hpp>
  21. int main()
  22. {
  23. using namespace BOOST_SPIRIT_CLASSIC_NS;
  24. using namespace std;
  25. rule<> a,b,c,next;
  26. const char* str="abcabc";
  27. parse_info<> hit;
  28. BOOST_SPIRIT_DEBUG_NODE(next);
  29. BOOST_SPIRIT_DEBUG_NODE(a);
  30. BOOST_SPIRIT_DEBUG_NODE(b);
  31. BOOST_SPIRIT_DEBUG_NODE(c);
  32. // basic spirit gram
  33. a = ch_p('a') >> !b;
  34. b = ch_p('b') >> !c;
  35. c = ch_p('c') >> !a;
  36. hit = parse(str, a);
  37. cout<<"hit :"<<( hit.hit ? "yes" : "no")<<", "
  38. <<(hit.full ? "full": "not full")
  39. <<endl;
  40. // using assign_a
  41. a = ch_p('a')[ assign_a( next, b)] >> !next;
  42. b = ch_p('b')[ assign_a( next, c)] >> !next;
  43. c = ch_p('c')[ assign_a( next, a)] >> !next;
  44. hit = parse(str, a);
  45. cout<<"hit :"<<( hit.hit ? "yes" : "no")<<", "
  46. <<(hit.full ? "full": "not full")
  47. <<endl;
  48. return 0;
  49. }