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 Roman Numerals]
11 This example demonstrates:
17 [heading Symbol Table]
19 The symbol table holds a dictionary of symbols where each symbol is a sequence
20 of characters (a `char`, `wchar_t`, `int`, enumeration etc.) . The template
21 class, parameterized by the character type, can work efficiently with 8, 16, 32
22 and even 64 bit characters. Mutable data of type T are associated with each
25 Traditionally, symbol table management is maintained separately outside the BNF
26 grammar through semantic actions. Contrary to standard practice, the Spirit
27 symbol table class `symbols` is a parser. An object of which may be used
28 anywhere in the EBNF grammar specification. It is an example of a dynamic
29 parser. A dynamic parser is characterized by its ability to modify its behavior
30 at run time. Initially, an empty symbols object matches nothing. At any time,
31 symbols may be added or removed, thus, dynamically altering its behavior.
33 Each entry in a symbol table has an associated mutable data slot. In this
34 regard, one can view the symbol table as an associative container (or map) of
35 key-value pairs where the keys are strings.
37 The symbols class expects two template parameters. The first parameter specifies
38 the character type of the symbols. The second specifies the data type associated
39 with each symbol: its attribute.
41 Here's a parser for roman hundreds (100..900) using the symbol table. Keep in
42 mind that the data associated with each slot is the parser's attribute (which is
43 passed to attached semantic actions).
45 [import ../../example/qi/roman.cpp]
47 [tutorial_roman_hundreds]
49 Here's a parser for roman tens (10..90):
53 and, finally, for ones (1..9):
57 Now we can use `hundreds`, `tens` and `ones` anywhere in our parser expressions.
62 Up until now, we've been inlining our parser expressions, passing them directly
63 to the `phrase_parse` function. The expression evaluates into a temporary,
64 unnamed parser which is passed into the `phrase_parse` function, used, and then
65 destroyed. This is fine for small parsers. When the expressions get complicated,
66 you'd want to break the expressions into smaller easier-to-understand pieces,
67 name them, and refer to them from other parser expressions by name.
69 A parser expression can be assigned to what is called a "rule". There are
70 various ways to declare rules. The simplest form is:
74 At the very least, the rule needs to know the iterator type it will be working
75 on. This rule cannot be used with `phrase_parse`. It can only be used with the
76 `parse` function -- a version that does not do white space skipping (does not
77 have the skipper argument). If you want to have it skip white spaces, you need
78 to pass in the type skip parser, as in the next form:
80 rule<Iterator, Skipper> r;
84 rule<std::string::iterator, space_type> r;
86 This type of rule can be used for both `phrase_parse` and `parse`.
88 For our next example, there's one more rule form you should know about:
90 rule<Iterator, Signature> r;
94 rule<Iterator, Signature, Skipper> r;
96 [tip All rule template arguments after Iterator can be supplied in any order.]
98 The Signature specifies the attributes of the rule. You've seen that our parsers
99 can have an attribute. Recall that the `double_` parser has an attribute of
100 `double`. To be precise, these are /synthesized/ attributes. The parser
101 "synthesizes" the attribute value. Think of them as function return values.
103 There's another type of attribute called "inherited" attribute. We won't need
104 them for now, but it's good that you be aware of such attributes. You can think
105 of them as function arguments. And, rightly so, the rule signature is a function
106 signature of the form:
108 result(argN, argN,..., argN)
110 After having declared a rule, you can now assign any parser expression to it.
113 r = double_ >> *(',' >> double_);
117 A grammar encapsulates one or more rules. It has the same template parameters as
118 the rule. You declare a grammar by:
120 # deriving a struct (or class) from the `grammar` class template
121 # declare one or more rules as member variables
122 # initialize the base grammar class by giving it the start rule (its the first
123 rule that gets called when the grammar starts parsing)
124 # initialize your rules in your constructor
126 The roman numeral grammar is a very nice and simple example of a grammar:
128 [tutorial_roman_grammar]
130 Things to take notice of:
132 * The grammar and start rule signature is `unsigned()`. It has a synthesized
133 attribute (return value) of type `unsigned` with no inherited attributes
136 * We did not specify a skip-parser. We don't want to skip in between the
139 * `roman::base_type` is a typedef for `grammar<Iterator, unsigned()>`. If
140 `roman` was not a template, you could simply write: base_type(start)
142 * It's best to make your grammar templates such that they can be reused
143 for different iterator types.
145 * `_val` is another __phoenix__ placeholder representing the rule's synthesized
148 * `eps` is a special spirit parser that consumes no input but is always
149 successful. We use it to initialize `_val`, the rule's synthesized
150 attribute, to zero before anything else. The actual parser starts at
151 `+lit('M')`, parsing roman thousands. Using `eps` this way is good
152 for doing pre and post initializations.
154 * The expression `a || b` reads: match a or b and in sequence. That is, if both
155 `a` and `b` match, it must be in sequence; this is equivalent to `a >> -b | b`,
158 [heading Let's Parse!]
160 [tutorial_roman_grammar_parse]
162 `roman_parser` is an object of type `roman`, our roman numeral parser. This time
163 around we are using the no-skipping version of the parse functions. We do not
164 want to skip any spaces! We are also passing in an attribute, `unsigned result`,
165 which will receive the parsed value.
167 The full cpp file for this example can be found here: [@../../example/qi/roman.cpp]