Ticket #2510: introduction.patch
File introduction.patch, 8.4 KB (added by , 14 years ago) |
---|
-
libs/spirit/doc/introduction.qbk
8 8 9 9 [section Introduction] 10 10 11 Boost Spirit is an object 12 library for C++. It allows to write grammars and format descriptions using a13 format verysimilar to EBNF (Extended Backus Naur Form, see [4]) directly in11 Boost Spirit is an object-oriented, recursive-descent parser and output generation 12 library for C++. It allows you to write grammars and format descriptions using a 13 format similar to EBNF (Extended Backus Naur Form, see [4]) directly in 14 14 C++. These inline grammar specifications can mix freely with other C++ code and, 15 15 thanks to the generative power of C++ templates, are immediately executable. 16 16 In retrospect, conventional compiler-compilers or parser-generators have to 17 17 perform an additional translation step from the source EBNF code to C or C++ 18 18 code. 19 19 20 The syntax and semantics of the libraries API directly form domainspecific21 embedded languages (DSEL). In fact, Spirit exposes 3 different DSEL 's to the20 The syntax and semantics of the libraries' API directly form domain-specific 21 embedded languages (DSEL). In fact, Spirit exposes 3 different DSELs to the 22 22 user: 23 23 24 24 * one for creating parser grammars, … … 26 26 * and one for the description of the required output formats. 27 27 28 28 Since the target input grammars and output formats are written entirely in C++ 29 we do not need any separate tools to compile, preprocess ,or integrate those29 we do not need any separate tools to compile, preprocess or integrate those 30 30 into the build process. __spirit__ allows seamless integration of the parsing 31 31 and output generation process with other C++ code. Often this allows for 32 32 simpler and more efficient code. 33 33 34 Both , the created parsers and generators, are fully attributed which allowsto34 Both the created parsers and generators are fully attributed which allows you to 35 35 easily build and handle hierarchical data structures in memory. These data 36 36 structures resemble the structure of the input data and can directly be used to 37 generate arbitrarily 37 generate arbitrarily-formatted output. 38 38 39 39 The [link spirit.spiritstructure figure] below depicts the overall structure 40 of the Boost Spirit library. The library consists o ut of 4 major parts:40 of the Boost Spirit library. The library consists of 4 major parts: 41 41 42 * __classic__: This is the almost 42 * __classic__: This is the almost-unchanged code base taken from the 43 43 former Boost Spirit V1.8 distribution. It has been moved into the namespace 44 44 boost::spirit::classic. A special compatibility layer has been added to 45 45 ensure complete compatibility with existing code using Spirit V1.8. 46 * __qi__: This is the parser library allowing to build recursive47 descent parsers. The exposed domain 46 * __qi__: This is the parser library allowing you to build recursive 47 descent parsers. The exposed domain-specific language can be used to describe 48 48 the grammars to implement, and the rules for storing the parsed information. 49 * __lex__: This is the library usable to create tok inizers (lexers). The domain50 specific language exposed by __lex__51 * __karma__: This is the generator library allowing to create code for52 recursive descent, data type driven output formatting. The exposed domain53 specific language is almost equivalent to the parser description language49 * __lex__: This is the library usable to create tokenizers (lexers). The 50 domain-specific language exposed by __lex__ 51 * __karma__: This is the generator library allowing you to create code for 52 recursive descent, data type-driven output formatting. The exposed 53 domain-specific language is almost equivalent to the parser description language 54 54 used in __qi__, except that it is used to describe the required output 55 55 format to generate from a given data structure. 56 56 … … 67 67 68 68 The [link spirit.spiritkarmaflow figure] below shows the typical data flow of 69 69 some input being converted to some internal representation. After some 70 (optional) transformation th is data isconverted back into some different,71 external representation. The picture highlights Spirit's theplace in this data70 (optional) transformation these data are converted back into some different, 71 external representation. The picture highlights Spirit's place in this data 72 72 transformation flow. 73 73 74 74 [fig ./images/spiritkarmaflow.png..The place of __qi__ and __karma__ in a data transformation flow of a typical application..spirit.spiritkarmaflow] 75 75 76 [heading A quick overview aboutParsing with __qi__]76 [heading A Quick Overview of Parsing with __qi__] 77 77 78 78 __qi__ is Spirit's sublibrary dealing with generating parsers based on a given 79 79 target grammar (essentially a format description of the input data to read). … … 94 94 expression = term >> *(('+' >> term) | ('-' >> term)); 95 95 96 96 Through the magic of expression templates, this is perfectly valid and 97 executable C++ code. The production rule `expression` is in factan object that98 has a member function parsethat does the work given a source code written in97 executable C++ code. The production rule `expression` is, in fact, an object that 98 has a member function `parse` that does the work given a source code written in 99 99 the grammar that we have just declared. Yes, it's a calculator. We shall 100 100 simplify for now by skipping the type declarations and the definition of the 101 rule integer invoked by factor. Now, the production rule `expression` in our102 grammar specification, traditionally called the startsymbol, can recognize101 rule `integer` invoked by `factor`. Now, the production rule `expression` in our 102 grammar specification, traditionally called the `start` symbol, can recognize 103 103 inputs such as: 104 104 105 105 12345 … … 144 144 rule with the ubiquitous semi-colon, `;`. 145 145 146 146 147 [heading A quick overview aboutOutput Generation with __karma__]147 [heading A Quick Overview of Output Generation with __karma__] 148 148 149 Spirit not only allows to describe the structure of the input. Starting with149 Spirit not only allows you to describe the structure of the input. Starting with 150 150 Version 2.0 it enables the specification of the output format for your data 151 in a verysimilar way, and based on a single syntax and compatible semantics.151 in a similar way, and based on a single syntax and compatible semantics. 152 152 153 153 Let's assume we need to generate a textual representation from a simple data 154 structure as a `std::vector<int>`. Conventional code probably would look like:154 structure such as a `std::vector<int>`. Conventional code probably would look like: 155 155 156 156 std::vector<int> v (initialize_and_fill()); 157 157 std::vector<int>::iterator end = v.end(); … … 159 159 std::cout << *it << std::endl; 160 160 161 161 which is not very flexible and quite difficult to maintain when it comes to 162 changing the required output format. Spirit's sublibrary /Karma/ allows to162 changing the required output format. Spirit's sublibrary /Karma/ allows you to 163 163 specify output formats for arbitrary data structures in a very flexible way. 164 following snippet is the /Karma/ format description used to create the very165 Thesame output as the traditional code above:164 The following snippet is the /Karma/ format description used to create the 165 same output as the traditional code above: 166 166 167 167 *(int_ << eol) 168 168 … … 177 177 [ [`*(double_ << ',')`] [`1.0,8.0,10.0,`] [A list of floating point numbers] ] 178 178 ] 179 179 180 The syntax is verysimilar to /Qi/ with the exception that we use the `<<`180 The syntax is similar to /Qi/ with the exception that we use the `<<` 181 181 operator for output concatenation. This should be easy to understand as it 182 182 follows the conventions used in the Standard's I/O streams. 183 183 184 Another important feature of /karma/ is to allowto fully decouple the data184 Another important feature of /karma/ allows you to fully decouple the data 185 185 type from the output format. You can use the same output format with different 186 data types as long as these conform aconceptually. The next table gives some186 data types as long as these conform conceptually. The next table gives some 187 187 related examples. 188 188 189 189 [table Different data types usable with the output format `(*int_ << eol)`