epsilon.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*=============================================================================
  2. Copyright (c) 2003 Vaclav Vesely
  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 behaviour of epsilon_p when used as parser
  10. // generator.
  11. //
  12. // The "r" is the rule, which is passed as a subject to the epsilon_p parser
  13. // generator. The "r" is the rule with binded semantic actions. But epsilon_p
  14. // parser generator is intended for looking forward and we don't want to
  15. // invoke semantic actions of subject parser. Hence the epsilon_p uses
  16. // the no_actions policy.
  17. //
  18. // Because we want to use the "r" rule both in the epsilon_p and outside of it
  19. // we need the "r" to support two differant scanners, one with no_actions
  20. // action policy and one with the default action policy. To achieve this
  21. // we declare the "r" with the no_actions_scanner_list scanner type.
  22. //
  23. //-----------------------------------------------------------------------------
  24. #define BOOST_SPIRIT_RULE_SCANNERTYPE_LIMIT 2
  25. #include <boost/assert.hpp>
  26. #include <iostream>
  27. #include <boost/cstdlib.hpp>
  28. #include <boost/spirit/include/classic_core.hpp>
  29. #include <boost/spirit/include/phoenix1.hpp>
  30. using namespace std;
  31. using namespace boost;
  32. using namespace BOOST_SPIRIT_CLASSIC_NS;
  33. using namespace phoenix;
  34. //-----------------------------------------------------------------------------
  35. int main()
  36. {
  37. rule<
  38. // Support both the default phrase_scanner_t and the modified version
  39. // with no_actions action_policy
  40. no_actions_scanner_list<phrase_scanner_t>::type
  41. > r;
  42. int i(0);
  43. r = int_p[var(i) += arg1];
  44. parse_info<> info = parse(
  45. "1",
  46. // r rule is used twice but the semantic action is invoked only once
  47. epsilon_p(r) >> r,
  48. space_p
  49. );
  50. BOOST_ASSERT(info.full);
  51. // Check, that the semantic action was invoked only once
  52. BOOST_ASSERT(i == 1);
  53. return exit_success;
  54. }
  55. //-----------------------------------------------------------------------------