README.rst 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. PyParsing -- A Python Parsing Module
  2. ====================================
  3. |Build Status| |Coverage|
  4. Introduction
  5. ============
  6. The pyparsing module is an alternative approach to creating and
  7. executing simple grammars, vs. the traditional lex/yacc approach, or the
  8. use of regular expressions. The pyparsing module provides a library of
  9. classes that client code uses to construct the grammar directly in
  10. Python code.
  11. *[Since first writing this description of pyparsing in late 2003, this
  12. technique for developing parsers has become more widespread, under the
  13. name Parsing Expression Grammars - PEGs. See more information on PEGs*
  14. `here <https://en.wikipedia.org/wiki/Parsing_expression_grammar>`__
  15. *.]*
  16. Here is a program to parse ``"Hello, World!"`` (or any greeting of the form
  17. ``"salutation, addressee!"``):
  18. .. code:: python
  19. from pyparsing import Word, alphas
  20. greet = Word(alphas) + "," + Word(alphas) + "!"
  21. hello = "Hello, World!"
  22. print(hello, "->", greet.parseString(hello))
  23. The program outputs the following::
  24. Hello, World! -> ['Hello', ',', 'World', '!']
  25. The Python representation of the grammar is quite readable, owing to the
  26. self-explanatory class names, and the use of '+', '|' and '^' operator
  27. definitions.
  28. The parsed results returned from ``parseString()`` is a collection of type
  29. ``ParseResults``, which can be accessed as a
  30. nested list, a dictionary, or an object with named attributes.
  31. The pyparsing module handles some of the problems that are typically
  32. vexing when writing text parsers:
  33. - extra or missing whitespace (the above program will also handle ``"Hello,World!"``, ``"Hello , World !"``, etc.)
  34. - quoted strings
  35. - embedded comments
  36. The examples directory includes a simple SQL parser, simple CORBA IDL
  37. parser, a config file parser, a chemical formula parser, and a four-
  38. function algebraic notation parser, among many others.
  39. Documentation
  40. =============
  41. There are many examples in the online docstrings of the classes
  42. and methods in pyparsing. You can find them compiled into `online docs <https://pyparsing-docs.readthedocs.io/en/latest/>`__. Additional
  43. documentation resources and project info are listed in the online
  44. `GitHub wiki <https://github.com/pyparsing/pyparsing/wiki>`__. An
  45. entire directory of examples can be found `here <https://github.com/pyparsing/pyparsing/tree/master/examples>`__.
  46. License
  47. =======
  48. MIT License. See header of the `pyparsing.py <https://github.com/pyparsing/pyparsing/blob/master/pyparsing/__init__.py#L1-L23>`__ file.
  49. History
  50. =======
  51. See `CHANGES <https://github.com/pyparsing/pyparsing/blob/master/CHANGES>`__ file.
  52. .. |Build Status| image:: https://github.com/pyparsing/pyparsing/actions/workflows/ci.yml/badge.svg
  53. :target: https://github.com/pyparsing/pyparsing/actions/workflows/ci.yml
  54. .. |Coverage| image:: https://codecov.io/gh/pyparsing/pyparsing/branch/master/graph/badge.svg
  55. :target: https://codecov.io/gh/pyparsing/pyparsing