rationale.qbk 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 Rationale]
  8. [heading Naming]
  9. Why use the name "Spirit", "Qi" and "Karma"? Because xpressive names
  10. have a better spirit, brings qi to your software and will enhance your
  11. karma so they can heal your (con)fusion and make you wave like a phoenix
  12. from the ashes. (Joachim Faulhaber)
  13. [heading Type Erasure: From static to dynamic C++]
  14. Rules straddle the border between static and dynamic C++. In effect, a
  15. rule transforms compile-time polymorphism (using templates) into
  16. run-time polymorphism (using virtual functions). This is necessary due
  17. to C++'s inability to automatically declare a variable of a type deduced
  18. from an arbitrarily complex expression in the right-hand side (rhs) of
  19. an assignment. Basically, we want to do something like:
  20. T rule = an_arbitrarily_complex_expression;
  21. without having to know or care about the resulting type of the
  22. right-hand side (rhs) of the assignment expression. This can be done
  23. with modern C++ 0x compilers using `auto`:
  24. auto rule = an_arbitrarily_complex_expression;
  25. Apart from this, we also need a facility to forward declare an unknown
  26. type:
  27. T rule;
  28. ...
  29. rule = a | b;
  30. These limitations lead us to this implementation of rules. This comes at
  31. the expense of the overhead of a type-erased call which is an indirect
  32. function call that connot be inlined, once through each invocation of a
  33. rule.
  34. [heading Multiple declaration]
  35. Some BNF variants allow multiple declarations of a rule. The
  36. declarations are taken as alternatives. Example:
  37. r = a;
  38. r = b;
  39. is equivalent to:
  40. r = a | b;
  41. Spirit v1.3 allowed this behavior. However, the current version of
  42. Spirit no longer allows this because experience shows that this behavior
  43. leads to unwanted gotchas (for instance, it does not allow rules to be
  44. held in containers). In the current release of Spirit, a second
  45. assignment to a rule will simply redefine it. The old definition is
  46. destructed. This follows more closely C++ semantics and is more in line
  47. with what the user expects the rule to behave.
  48. [heading Sequencing Syntax]
  49. The comma operator as in `a, b` seems to be a better candidate,
  50. syntax-wise. But then the problem is with its precedence. It has the
  51. lowest precedence in C/C++, which makes it virtually useless.
  52. Bjarne Stroustrup, in his article "Generalizing Overloading for C++2000"
  53. talks about overloading whitespace. Such a feature would allow
  54. juxtapositioning of parser objects exactly as we do in (E)BNF (e.g. a b
  55. | c instead of a >> b | c). Unfortunately, the article was dated April
  56. 1, 1998. Oh well.
  57. [heading Forward iterators]
  58. In general, the expected iterator is at least a standard conforming
  59. forward iterator. Forward iterators are needed for backtracking where
  60. the iterator needs to be saved and restored later. Generally speaking,
  61. Spirit is a backtracking parser. The implication of this is that at some
  62. point, the iterator position will have to be saved to allow the parser
  63. to backtrack to a previous point. Thus, for backtracking to work, the
  64. framework requires at least a forward iterator.
  65. There are remedies of course. In cases where we need to use input
  66. iterators, you can use the __multi_pass__ iterator to make the forward
  67. iterators.
  68. Some parsers might require more specialized iterators (bi-directional or
  69. even random access). Perhaps in the future, deterministic parsers when
  70. added to the framework, will perform no backtracking and will need just
  71. a single token lookahead, hence will require input iterators only.
  72. [heading Exhaustive backtracking and greedy RD]
  73. Spirit doesn't do exhaustive backtracking like regular expressions are
  74. expected to. For example:
  75. *char_('a') >> char_('a');
  76. will always fail to match because Spirit's Kleene star does not back off
  77. when the rest of the rule fails to match.
  78. Actually, there's a solution to this greedy RD problem. Such a scheme is
  79. discussed in section 6.6.2 of Parsing Techniques: A Practical Guide. The
  80. trick involves passing a tail parser (in addition to the scanner) to
  81. each parser. The start parser will then simply be:
  82. start >> end;
  83. (end is the start's tail).
  84. Spirit is greedy --using straight forward, naive RD. It is certainly
  85. possible to implement the fully backtracking scheme presented above, but
  86. there will be also certainly be a performance hit. The scheme will
  87. always try to match all possible parser paths (full parser hierarchy
  88. traversal) until it reaches a point of certainty, that the whole thing
  89. matches or fails to match.
  90. [:Backtracking and Greedy RD
  91. Spirit is quite consistent and intuitive about when it backtracks and to
  92. where, although it may not be obvious to those coming from different
  93. backgrounds. In general, any (sub)parser will, given the same input,
  94. always match the same portion of the input (or fail to match the input
  95. at all). This means that Spirit is inherently greedy. Spirit will only
  96. backtrack when a (sub)parser fails to match the input, and it will
  97. always backtrack to the next choice point upward (not backward) in the
  98. parser structure. In other words abb|ab will match `"ab"`, as will
  99. `a(bb|b)`, but `(ab|a)b` won't because the `(ab|a)` subparser will
  100. always match the `'b'` after the `'a'` if it is available.
  101. --Rainer Deyke]
  102. This is the very nature of __peg__.
  103. There's a strong preference on "simplicity with all the knobs when you
  104. need them" approach, right now. On the other hand, the flexibility of
  105. Spirit makes it possible to have different optional schemes available.
  106. It might be possible to implement an exhaustive backtracking RD scheme
  107. as an optional feature in the future.
  108. [endsect]