parser_context.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 usage of the parser_context template with
  10. // an explicit argument to declare rules with match results different from
  11. // nil_t. For better understanding, you should read the chapter "In-depth:
  12. // The Parser Context" in the documentation.
  13. //
  14. // The default context of non-terminals is the parser_context.
  15. // The parser_context is a template with one argument AttrT, which is the type
  16. // of match attribute.
  17. //
  18. // In this example int_rule is declared as rule with int match attribute's
  19. // type, so in int_rule variable we can hold any parser, which returns int
  20. // value. For example int_p or bin_p. And the most important is that we can
  21. // use returned value in the semantic action binded to the int_rule.
  22. //
  23. //-----------------------------------------------------------------------------
  24. #include <iostream>
  25. #include <boost/cstdlib.hpp>
  26. #include <boost/spirit/include/phoenix1.hpp>
  27. #include <boost/spirit/include/classic_core.hpp>
  28. using namespace std;
  29. using namespace boost;
  30. using namespace phoenix;
  31. using namespace BOOST_SPIRIT_CLASSIC_NS;
  32. //-----------------------------------------------------------------------------
  33. int main()
  34. {
  35. rule<parser_context<int> > int_rule = int_p;
  36. parse(
  37. "123",
  38. // Using a returned value in the semantic action
  39. int_rule[cout << arg1 << endl]
  40. );
  41. return exit_success;
  42. }
  43. //-----------------------------------------------------------------------------