syntax_diagram.qbk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. [/==============================================================================
  2. Copyright (C) 2001-2015 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 Syntax Diagram]
  8. In the next section, we will deal with Parsing Expression Grammars
  9. (PEG) [footnote Bryan Ford: Parsing Expression Grammars: A Recognition-Based
  10. Syntactic Foundation, [@http://pdos.csail.mit.edu/~baford/packrat/popl04/]],
  11. a variant of Extended Backus-Naur Form (EBNF) [footnote Richard E. Pattis: EBNF:
  12. A Notation to Describe Syntax, [@http://www.cs.cmu.edu/~pattis/misc/ebnf.pdf]]
  13. with a different interpretation. It is easier to understand PEG using Syntax
  14. Diagrams. Syntax diagrams represent a grammar graphically. It was used
  15. extensively by Niklaus Wirth [footnote Niklaus Wirth: The Programming Language
  16. Pascal. (July 1973)] in the "Pascal User Manual". Syntax Diagrams are easily
  17. understandable by programmers due to their similarity to flow charts. The
  18. isomorphism of the diagrams and functions make them ideal for representing
  19. __rd__ parsers which are essentially mutually recursive functions.
  20. [heading Elements]
  21. All diagrams have one entry and one exit point. Arrows connect all possible
  22. paths through the grammar from the entry point to the exit point.
  23. [:__sd_start_stop__]
  24. Terminals are represented by round boxes. Terminals are atomic and
  25. usually represent plain characters, strings or tokens.
  26. [:__sd_terminals__]
  27. Non-terminals are represented by boxes. Diagrams are modularized using
  28. named non-terminals. A complex diagram can be broken down into a set of
  29. non-terminals. Non-terminals also allow recursion (i.e. a non-terminal can call
  30. itself).
  31. [:__sd_non_terminals__]
  32. [heading Constructs]
  33. The most basic composition is the Sequence. B follows A:
  34. [:__sd_sequence__]
  35. The ordered choice henceforth we will call /alternatives/. In PEG, ordered
  36. choice and alternatives are not quite the same. PEG allows ambiguity of choice
  37. where one or more branches can succeed. In PEG, in case of ambiguity, the first
  38. one always wins.
  39. [:__sd_choice__]
  40. The optional (zero-or-one):
  41. [:__sd_optional__]
  42. Now, the loops. We have the zero-or-more and one-or-more:
  43. [:__sd_kleene__]
  44. [:__sd_plus__]
  45. Take note that, as in PEG, these loops behave greedily. If there is
  46. another 'A' just before the end-point, it will always fail because the
  47. preceding loop has already exhausted all 'A's and there is nothing more left.
  48. This is a crucial difference between PEG and general Context Free Grammars
  49. (CFGs). This behavior is quite obvious with syntax diagrams as they resemble
  50. flow-charts.
  51. [heading Predicates]
  52. Now, the following are Syntax Diagram versions of PEG predicates. These are not
  53. traditionally found in Syntax Diagrams. These are special extensions we invented
  54. to closely follow PEGs.
  55. First, we introduce a new element, the Predicate:
  56. [:__sd_predicate__]
  57. This is similar to the conditionals in flow charts where the 'No' branch is
  58. absent and always signals a failed parse.
  59. We have two versions of the predicate, the /And-Predicate/ and the
  60. /Not-Predicate/:
  61. [:__sd_and_predicate__]
  62. [:__sd_not_predicate__]
  63. The /And-Predicate/ tries the predicate, P, and succeeds if P succeeds,
  64. or otherwise fail. The opposite is true with the /Not-Predicate/. It
  65. tries the predicate, P, and fails if P succeeds, or otherwise succeeds. Both
  66. versions do a look-ahead but do not consume any input regardless if P succeeds
  67. or not.
  68. [endsect]