lexer_quickstart1.qbk 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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:lexer_quickstart1 Quickstart 1 - A word counter using __lex__]
  8. __lex__ is very modular, which follows the general building principle of the
  9. __spirit__ libraries. You never pay for features you don't use. It is nicely
  10. integrated with the other parts of __spirit__ but nevertheless can be used
  11. separately to build stand alone lexical analyzers.
  12. The first quick start example describes a stand alone application:
  13. counting characters, words, and lines in a file, very similar to what the well
  14. known Unix command `wc` is doing (for the full example code see here:
  15. [@../../example/lex/word_count_functor.cpp word_count_functor.cpp]).
  16. [import ../example/lex/word_count_functor.cpp]
  17. [heading Prerequisites]
  18. The only required `#include` specific to /Spirit.Lex/ follows. It is a wrapper
  19. for all necessary definitions to use /Spirit.Lex/ in a stand alone fashion, and
  20. on top of the __lexertl__ library. Additionally we `#include` two of the Boost
  21. headers to define `boost::bind()` and `boost::ref()`.
  22. [wcf_includes]
  23. To make all the code below more readable we introduce the following namespaces.
  24. [wcf_namespaces]
  25. [heading Defining Tokens]
  26. The most important step while creating a lexer using __lex__ is to define the
  27. tokens to be recognized in the input sequence. This is normally done by
  28. defining the regular expressions describing the matching character sequences,
  29. and optionally their corresponding token ids. Additionally the defined tokens
  30. need to be associated with an instance of a lexer object as provided by the
  31. library. The following code snippet shows how this can be done using __lex__.
  32. [wcf_token_definition]
  33. [heading Doing the Useful Work]
  34. We will use a setup, where we want the __lex__ library to invoke a given
  35. function after any of the generated tokens is recognized. For this reason
  36. we need to implement a functor taking at least the generated token as an
  37. argument and returning a boolean value allowing to stop the tokenization
  38. process. The default token type used in this example carries a token value of
  39. the type __boost_iterator_range__`<BaseIterator>` pointing to the matched
  40. range in the underlying input sequence.
  41. [wcf_functor]
  42. All what is left is to write some boilerplate code helping to tie together the
  43. pieces described so far. To simplify this example we call the `lex::tokenize()`
  44. function implemented in __lex__ (for a more detailed description of this
  45. function see here: __fixme__), even if we could have written a loop to iterate
  46. over the lexer iterators [`first`, `last`) as well.
  47. [heading Pulling Everything Together]
  48. [wcf_main]
  49. [heading Comparing __lex__ with __flex__]
  50. This example was deliberately chosen to be as much as possible similar to the
  51. equivalent __flex__ program (see below), which isn't too different from what
  52. has to be written when using __lex__.
  53. [note Interestingly enough, performance comparisons of lexical analyzers
  54. written using __lex__ with equivalent programs generated by
  55. __flex__ show that both have comparable execution speeds!
  56. Generally, thanks to the highly optimized __lexertl__ library and
  57. due its carefully designed integration with __spirit__ the
  58. abstraction penalty to be paid for using __lex__ is negligible.
  59. ]
  60. The remaining examples in this tutorial will use more sophisticated features
  61. of __lex__, mainly to allow further simplification of the code to be written,
  62. while maintaining the similarity with corresponding features of __flex__.
  63. __lex__ has been designed to be as similar to __flex__ as possible. That
  64. is why this documentation will provide the corresponding __flex__ code for the
  65. shown __lex__ examples almost everywhere. So consequently, here is the __flex__
  66. code corresponding to the example as shown above.
  67. [wcf_flex_version]
  68. [endsect]