README.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. PyParsing -- A Python Parsing Module
  2. ====================================
  3. |Version| |Build Status| |Coverage| |License| |Python Versions| |Snyk Score|
  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 __init__.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
  56. .. |Version| image:: https://img.shields.io/pypi/v/pyparsing?style=flat-square
  57. :target: https://pypi.org/project/pyparsing/
  58. :alt: Version
  59. .. |License| image:: https://img.shields.io/pypi/l/pyparsing.svg?style=flat-square
  60. :target: https://pypi.org/project/pyparsing/
  61. :alt: License
  62. .. |Python Versions| image:: https://img.shields.io/pypi/pyversions/pyparsing.svg?style=flat-square
  63. :target: https://pypi.org/project/python-liquid/
  64. :alt: Python versions
  65. .. |Snyk Score| image:: https://snyk.io//advisor/python/pyparsing/badge.svg
  66. :target: https://snyk.io//advisor/python/pyparsing
  67. :alt: pyparsing