complex.qbk 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ===============================================================================/]
  7. [section Complex - Our first complex parser]
  8. Well, not really a complex parser, but a parser that parses complex numbers.
  9. This time, we're using __phoenix__ to do the semantic actions.
  10. Here's a simple parser expression for complex numbers:
  11. '(' >> double_ >> -(',' >> double_) >> ')'
  12. | double_
  13. What's new? Well, we have:
  14. # Alternates: e.g. `a | b`. Try `a` first. If it succeeds, good. If not, try the
  15. next alternative, `b`.
  16. # Optionals: e.g. -p. Match the parser p zero or one time.
  17. The complex parser presented above reads as:
  18. * One or two real numbers in parentheses, separated by comma (the second number is optional)
  19. * *OR* a single real number.
  20. This parser can parse complex numbers of the form:
  21. (123.45, 987.65)
  22. (123.45)
  23. 123.45
  24. [import ../../example/qi/complex_number.cpp]
  25. Here goes, this time with actions:
  26. [tutorial_complex_number]
  27. The full cpp file for this example can be found here: [@../../example/qi/complex_number.cpp]
  28. [note Those with experience using __phoenix__ might be confused with the
  29. placeholders that we are using (i.e. `_1`, `_2`, etc.). Please be aware
  30. that we are not using the same placeholders supplied by Phoenix. Take
  31. note that we are pulling in the placeholders from namespace
  32. `boost::spirit::qi`. These placeholders are specifically tailored for
  33. Spirit.]
  34. The `double_` parser attaches this action:
  35. ref(n) = _1
  36. This assigns the parsed result (actually, the attribute of `double_`) to n.
  37. `ref(n)` tells Phoenix that `n` is a mutable reference. `_1` is a Phoenix
  38. placeholder for the parsed result attribute.
  39. [endsect]