]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | [/============================================================================== |
2 | Copyright (C) 2001-2011 Joel de Guzman | |
3 | Copyright (C) 2001-2011 Hartmut Kaiser | |
4 | ||
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 | ===============================================================================/] | |
8 | ||
9 | [section Complex - Our first complex parser] | |
10 | ||
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. | |
13 | ||
14 | Here's a simple parser expression for complex numbers: | |
15 | ||
16 | '(' >> double_ >> -(',' >> double_) >> ')' | |
17 | | double_ | |
18 | ||
19 | What's new? Well, we have: | |
20 | ||
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. | |
24 | ||
25 | The complex parser presented above reads as: | |
26 | ||
27 | * One or two real numbers in parentheses, separated by comma (the second number is optional) | |
28 | * *OR* a single real number. | |
29 | ||
30 | This parser can parse complex numbers of the form: | |
31 | ||
32 | (123.45, 987.65) | |
33 | (123.45) | |
34 | 123.45 | |
35 | ||
36 | [import ../../example/qi/complex_number.cpp] | |
37 | ||
38 | Here goes, this time with actions: | |
39 | ||
40 | [tutorial_complex_number] | |
41 | ||
42 | The full cpp file for this example can be found here: [@../../example/qi/complex_number.cpp] | |
43 | ||
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 | |
49 | Spirit.] | |
50 | ||
51 | The `double_` parser attaches this action: | |
52 | ||
53 | ref(n) = _1 | |
54 | ||
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. | |
58 | ||
59 | [endsect] |