METADATA 8.0 KB

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