METADATA 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. Metadata-Version: 2.4
  2. Name: pycparser
  3. Version: 3.11
  4. Summary: C parser in Python
  5. Author-email: Eli Bendersky <eliben@gmail.com>
  6. Maintainer-email: Eli Bendersky <eliben@gmail.com>
  7. License-Expression: BSD-3-Clause
  8. Project-URL: Homepage, https://github.com/eliben/pycparser
  9. Classifier: Development Status :: 5 - Production/Stable
  10. Classifier: Programming Language :: Python :: 3
  11. Classifier: Programming Language :: Python :: 3.10
  12. Classifier: Programming Language :: Python :: 3.11
  13. Classifier: Programming Language :: Python :: 3.12
  14. Classifier: Programming Language :: Python :: 3.13
  15. Classifier: Programming Language :: Python :: 3.14
  16. Classifier: Programming Language :: Python :: 3.15
  17. Requires-Python: >=3.10
  18. Description-Content-Type: text/x-rst
  19. License-File: LICENSE
  20. Dynamic: license-file
  21. =========
  22. pycparser
  23. =========
  24. .. image:: https://github.com/eliben/pycparser/workflows/pycparser-tests/badge.svg
  25. :align: center
  26. :target: https://github.com/eliben/pycparser/actions
  27. ----
  28. .. contents::
  29. :backlinks: none
  30. .. sectnum::
  31. Introduction
  32. ============
  33. What is pycparser?
  34. ------------------
  35. **pycparser** is a parser for the C language, written in pure Python. It is a
  36. module designed to be easily integrated into applications that need to parse
  37. C source code.
  38. What is it good for?
  39. --------------------
  40. Anything that needs C code to be parsed. The following are some uses for
  41. **pycparser**, taken from real user reports:
  42. * C code obfuscator
  43. * Front-end for various specialized C compilers
  44. * Static code checker
  45. * Automatic unit-test discovery
  46. * Adding specialized extensions to the C language
  47. One of the most popular uses of **pycparser** is in the `cffi
  48. <https://cffi.readthedocs.io/en/latest/>`_ library, which uses it to parse the
  49. declarations of C functions and types in order to auto-generate FFIs.
  50. **pycparser** is unique in the sense that it's written in pure Python - a very
  51. high level language that's easy to experiment with and tweak. To people familiar
  52. with Lex and Yacc, **pycparser**'s code will be simple to understand. It also
  53. has no external dependencies (except for a Python interpreter), making it very
  54. simple to install and deploy.
  55. Which version of C does pycparser support?
  56. ------------------------------------------
  57. **pycparser** aims to support the full C99 language (according to the standard
  58. ISO/IEC 9899). Some features from C11 are also supported, and patches to support
  59. more are welcome.
  60. **pycparser** supports very few GCC extensions, but it's fairly easy to set
  61. things up so that it parses code with a lot of GCC-isms successfully. See the
  62. `FAQ <https://github.com/eliben/pycparser/wiki/FAQ>`_ for more details.
  63. What grammar does pycparser follow?
  64. -----------------------------------
  65. **pycparser** very closely follows the C grammar provided in Annex A of the C99
  66. standard (ISO/IEC 9899).
  67. How is pycparser licensed?
  68. --------------------------
  69. `BSD license <https://github.com/eliben/pycparser/blob/main/LICENSE>`_.
  70. Contact details
  71. ---------------
  72. For reporting problems with **pycparser** or submitting feature requests, please
  73. open an `issue <https://github.com/eliben/pycparser/issues>`_, or submit a
  74. pull request.
  75. Installing
  76. ==========
  77. Prerequisites
  78. -------------
  79. **pycparser** is being tested with modern versions of Python on
  80. Linux, macOS and Windows. See `the CI dashboard <https://github.com/eliben/pycparser/actions/workflows/ci.yml>`__
  81. for details.
  82. **pycparser** has no external dependencies.
  83. Installation process
  84. --------------------
  85. The recommended way to install **pycparser** is with ``pip``::
  86. > pip install pycparser
  87. Using
  88. =====
  89. Interaction with the C preprocessor
  90. -----------------------------------
  91. In order to be compilable, C code must be preprocessed by the C preprocessor -
  92. ``cpp``. A compatible ``cpp`` handles preprocessing directives like ``#include`` and
  93. ``#define``, removes comments, and performs other minor tasks that prepare the C
  94. code for compilation.
  95. For all but the most trivial snippets of C code **pycparser**, like a C
  96. compiler, must receive preprocessed C code in order to function correctly. If
  97. you import the top-level ``parse_file`` function from the **pycparser** package,
  98. it will interact with ``cpp`` for you, as long as it's in your PATH, or you
  99. provide a path to it.
  100. Note also that you can use ``gcc -E`` or ``clang -E`` instead of ``cpp``. See
  101. the ``using_gcc_E_libc.py`` example for more details. Windows users can download
  102. and install a binary build of Clang for Windows `from this website
  103. <http://llvm.org/releases/download.html>`_.
  104. What about the standard C library headers?
  105. ------------------------------------------
  106. C code almost always ``#include``\s various header files from the standard C
  107. library, like ``stdio.h``. While (with some effort) **pycparser** can be made to
  108. parse the standard headers from any C compiler, it's much simpler to use the
  109. provided "fake" standard includes for C11 in ``utils/fake_libc_include``. These
  110. are standard C header files that contain only the bare necessities to allow
  111. valid parsing of the files that use them. As a bonus, since they're minimal, it
  112. can significantly improve the performance of parsing large C files.
  113. The key point to understand here is that **pycparser** doesn't really care about
  114. the semantics of types. It only needs to know whether some token encountered in
  115. the source is a previously defined type. This is essential in order to be able
  116. to parse C correctly.
  117. See `this blog post
  118. <https://eli.thegreenplace.net/2015/on-parsing-c-type-declarations-and-fake-headers>`_
  119. for more details.
  120. Note that the fake headers are not included in the ``pip`` package nor installed
  121. via the package build (`#224 <https://github.com/eliben/pycparser/issues/224>`_).
  122. Basic usage
  123. -----------
  124. Take a look at the |examples|_ directory of the distribution for a few examples
  125. of using **pycparser**. These should be enough to get you started. Please note
  126. that most realistic C code samples would require running the C preprocessor
  127. before passing the code to **pycparser**; see the previous sections for more
  128. details.
  129. .. |examples| replace:: ``examples``
  130. .. _examples: examples
  131. Advanced usage
  132. --------------
  133. The public interface of **pycparser** is well documented with comments in
  134. ``pycparser/c_parser.py``. For a detailed overview of the various AST nodes
  135. created by the parser, see ``pycparser/_c_ast.cfg``.
  136. There's also a `FAQ available here <https://github.com/eliben/pycparser/wiki/FAQ>`_.
  137. In any case, you can always drop me an `email <eliben@gmail.com>`_ for help.
  138. Modifying
  139. =========
  140. There are a few points to keep in mind when modifying **pycparser**:
  141. * The code for **pycparser**'s AST nodes is automatically generated from a
  142. configuration file - ``_c_ast.cfg``, by ``_ast_gen.py``. If you modify the AST
  143. configuration or the generator, run ``make regen-ast`` from the repository
  144. root to re-generate ``pycparser/c_ast.py`` and run ``make format``.
  145. * Read the docstring in the constructor of the ``CParser`` class for details
  146. on configuration and compatibility arguments.
  147. Package contents
  148. ================
  149. Once you unzip the ``pycparser`` package, you'll see the following files and
  150. directories:
  151. README.rst:
  152. This README file.
  153. LICENSE:
  154. The pycparser license
  155. setup.py:
  156. Legacy installation script (build metadata lives in ``pyproject.toml``).
  157. pyproject.toml:
  158. Package metadata and build configuration.
  159. examples/:
  160. A directory with some examples of using **pycparser**
  161. pycparser/:
  162. The **pycparser** module source code.
  163. tests/:
  164. Unit tests.
  165. utils/fake_libc_include:
  166. Minimal standard C library include files that should allow to parse any C code.
  167. Note that these headers now include C11 code, so they may not work when the
  168. preprocessor is configured to an earlier C standard (like ``-std=c99``).
  169. utils/internal/:
  170. Internal utilities for my own use. You probably don't need them.
  171. Contributors
  172. ============
  173. Some people have contributed to **pycparser** by opening issues on bugs they've
  174. found and/or submitting patches. The list of contributors is in the CONTRIBUTORS
  175. file in the source distribution. After **pycparser** moved to Github I stopped
  176. updating this list because Github does a much better job at tracking
  177. contributions.