Advertisement
chadjoan

Unitary and Nonunitary expressions.

Nov 4th, 2013
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. From the top of parser_builder.d, which can be found at https://github.com/chadjoan/xdc/blob/master/src/xdc/parser_builder/parser_builder.d
  2.  
  3. Unitary Expression: an expression that, given an input string, has only one
  4. possible match (only one match.end is possible).
  5. PEG rules are unitary expressions.
  6. Terminals are unitary expressions as well.
  7. Ex: aa/a on the input "aaa" has only one match: [aa]a
  8.  
  9. Nonunitary Expression: an expression that, given an input string, has many
  10. possible matches.
  11. Regular expression operators are nonunitary expressions.
  12. Ex: aa|a on the input "aaa" may match [a]aa or [aa]a depending on whether the
  13. code driving the expression requests 1 or 2 characters.
  14.  
  15. Both unitary and nonunitary expressions may reside within regular expressions.
  16. Only unitary expressions are allowed in PEGs.
  17. Nonunitary expressions can be converted into unitary expressions by using some
  18. manner of rule to disambiguate which match to take. Intuitively, we define
  19. "longest" and "shortest" rules to accomplish this.
  20. We can thus create unitary expressions with nonunitary subexpressions, and
  21. gain some of the convenience of regular expressions within more powerful
  22. constructs like PEGs.
  23.  
  24. Nonunitary expressions are not reentrant without an explosion of algorithmic
  25. complexity. This prevents PEGs from having things like unordered choice:
  26. A <- b*aB
  27. B <- (A|b*)
  28. In this case, the rule called within the unordered choice (the | operator)
  29. will invoke B again and reenter the choice without first resolving the succuss
  30. of the first unordered choice entry. This ambiguity can result in repeated
  31. backtracking.
  32. (TODO: is this example good? maybe use a better one that will obviously create
  33. a lot of backtracking.)
  34.  
  35. The concatenation of a nonunitary expression and a unitary expression is nonunitary.
  36.  
  37. PEG elements may not follow nonunitary expressions in concatenation without
  38. suffering a large penalty in algorithmic complexity. This arrangement
  39. requires nonlocal backtracing out of failed matches of the PEG until a place
  40. on the input is found where the nonunitary expression before it matches and
  41. the PEG also matches the text ahead of it. To maintain linear complexity,
  42. such nonlinear expressions preceding PEGs must be disambiguated with some
  43. rule like longest-match or shortest-match.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement