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