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