blob: df9025c5eb7e52897568cc0ac1f9ce313d0eddba [file] [log] [blame]
Eli Benderskye242e4c2013-08-03 06:15:32 -07001===============
Eli Benderskycaa4c112018-09-19 05:21:20 -07002pycparser v2.19
Eli Benderskye242e4c2013-08-03 06:15:32 -07003===============
Eli Benderskycd2605e2013-06-13 06:25:20 -07004
Jon Dufresne1d866992018-06-26 13:49:35 -07005:Author: `Eli Bendersky <https://eli.thegreenplace.net/>`_
Eli Benderskycd2605e2013-06-13 06:25:20 -07006
7
8.. contents::
9 :backlinks: none
10
11.. sectnum::
12
13
14Introduction
15============
16
17What is pycparser?
18------------------
19
20**pycparser** is a parser for the C language, written in pure Python. It is a
21module designed to be easily integrated into applications that need to parse
22C source code.
23
24What is it good for?
25--------------------
26
Eli Benderskye242e4c2013-08-03 06:15:32 -070027Anything that needs C code to be parsed. The following are some uses for
28**pycparser**, taken from real user reports:
Eli Benderskycd2605e2013-06-13 06:25:20 -070029
30* C code obfuscator
31* Front-end for various specialized C compilers
32* Static code checker
33* Automatic unit-test discovery
34* Adding specialized extensions to the C language
35
Eli Benderskye9f5bc72015-05-10 08:10:12 -070036One of the most popular uses of **pycparser** is in the `cffi
Adam Chainz81453442016-05-29 14:20:48 +010037<https://cffi.readthedocs.io/en/latest/>`_ library, which uses it to parse the
Eli Benderskye9f5bc72015-05-10 08:10:12 -070038declarations of C functions and types in order to auto-generate FFIs.
Eli Benderskyd69771e2015-05-10 08:19:38 -070039
Eli Benderskye242e4c2013-08-03 06:15:32 -070040**pycparser** is unique in the sense that it's written in pure Python - a very
41high level language that's easy to experiment with and tweak. To people familiar
Eli Benderskyd69771e2015-05-10 08:19:38 -070042with Lex and Yacc, **pycparser**'s code will be simple to understand. It also
43has no external dependencies (except for a Python interpreter), making it very
44simple to install and deploy.
Eli Benderskycd2605e2013-06-13 06:25:20 -070045
46Which version of C does pycparser support?
47------------------------------------------
48
Eli Benderskye242e4c2013-08-03 06:15:32 -070049**pycparser** aims to support the full C99 language (according to the standard
Eli Bendersky736c1262015-04-18 09:00:19 -070050ISO/IEC 9899). Some features from C11 are also supported, and patches to support
51more are welcome.
Eli Benderskycd2605e2013-06-13 06:25:20 -070052
Eli Benderskyd69771e2015-05-10 08:19:38 -070053**pycparser** supports very few GCC extensions, but it's fairly easy to set
54things up so that it parses code with a lot of GCC-isms successfully. See the
55`FAQ <https://github.com/eliben/pycparser/wiki/FAQ>`_ for more details.
Eli Benderskycd2605e2013-06-13 06:25:20 -070056
57What grammar does pycparser follow?
58-----------------------------------
59
Eli Bendersky17a0ba82017-07-04 15:07:00 -070060**pycparser** very closely follows the C grammar provided in Annex A of the C99
61standard (ISO/IEC 9899).
Eli Benderskycd2605e2013-06-13 06:25:20 -070062
63How is pycparser licensed?
64--------------------------
65
Eli Bendersky5dbe6762016-10-31 05:27:45 -070066`BSD license <https://github.com/eliben/pycparser/blob/master/LICENSE>`_.
Eli Benderskycd2605e2013-06-13 06:25:20 -070067
68Contact details
69---------------
70
Eli Bendersky17a0ba82017-07-04 15:07:00 -070071For reporting problems with **pycparser** or submitting feature requests, please
72open an `issue <https://github.com/eliben/pycparser/issues>`_, or submit a
73pull request.
Eli Benderskycd2605e2013-06-13 06:25:20 -070074
75
76Installing
77==========
78
79Prerequisites
80-------------
81
Jon Dufresnea301cbb2018-06-28 06:12:24 -070082* **pycparser** was tested on Python 2.7, 3.4-3.6, on both Linux and
Eli Benderskye242e4c2013-08-03 06:15:32 -070083 Windows. It should work on any later version (in both the 2.x and 3.x lines)
84 as well.
Eli Benderskycd2605e2013-06-13 06:25:20 -070085
Eli Bendersky2ecc87c2013-08-03 07:03:28 -070086* **pycparser** has no external dependencies. The only non-stdlib library it
87 uses is PLY, which is bundled in ``pycparser/ply``. The current PLY version is
Eli Bendersky17a0ba82017-07-04 15:07:00 -070088 3.10, retrieved from `<http://www.dabeaz.com/ply/>`_
Eli Benderskycd2605e2013-06-13 06:25:20 -070089
Eli Bendersky2fdaa982017-07-21 06:36:37 -070090Note that **pycparser** (and PLY) uses docstrings for grammar specifications.
91Python installations that strip docstrings (such as when using the Python
92``-OO`` option) will fail to instantiate and use **pycparser**. You can try to
93work around this problem by making sure the PLY parsing tables are pre-generated
94in normal mode; this isn't an officially supported/tested mode of operation,
95though.
96
Eli Benderskycd2605e2013-06-13 06:25:20 -070097Installation process
98--------------------
99
Eli Benderskye242e4c2013-08-03 06:15:32 -0700100Installing **pycparser** is very simple. Once you download and unzip the
101package, you just have to execute the standard ``python setup.py install``. The
102setup script will then place the ``pycparser`` module into ``site-packages`` in
103your Python's installation library.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700104
Eli Benderskye242e4c2013-08-03 06:15:32 -0700105Alternatively, since **pycparser** is listed in the `Python Package Index
Jon Dufresne2d717d42018-06-10 05:21:29 -0700106<https://pypi.org/project/pycparser/>`_ (PyPI), you can install it using your
Eli Benderskye242e4c2013-08-03 06:15:32 -0700107favorite Python packaging/distribution tool, for example with::
Eli Benderskycd2605e2013-06-13 06:25:20 -0700108
109 > pip install pycparser
110
Eli Benderskycd2605e2013-06-13 06:25:20 -0700111Known problems
112--------------
113
Eli Benderskye242e4c2013-08-03 06:15:32 -0700114* Some users who've installed a new version of **pycparser** over an existing
115 version ran into a problem using the newly installed library. This has to do
116 with parse tables staying around as ``.pyc`` files from the older version. If
117 you see unexplained errors from **pycparser** after an upgrade, remove it (by
118 deleting the ``pycparser`` directory in your Python's ``site-packages``, or
119 wherever you installed it) and install again.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700120
Eli Bendersky17a0ba82017-07-04 15:07:00 -0700121
Eli Benderskycd2605e2013-06-13 06:25:20 -0700122Using
123=====
124
125Interaction with the C preprocessor
126-----------------------------------
127
Eli Benderskye242e4c2013-08-03 06:15:32 -0700128In order to be compilable, C code must be preprocessed by the C preprocessor -
129``cpp``. ``cpp`` handles preprocessing directives like ``#include`` and
Eli Bendersky17a0ba82017-07-04 15:07:00 -0700130``#define``, removes comments, and performs other minor tasks that prepare the C
Eli Benderskye242e4c2013-08-03 06:15:32 -0700131code for compilation.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700132
Eli Bendersky17a0ba82017-07-04 15:07:00 -0700133For all but the most trivial snippets of C code **pycparser**, like a C
Eli Benderskye242e4c2013-08-03 06:15:32 -0700134compiler, must receive preprocessed C code in order to function correctly. If
135you import the top-level ``parse_file`` function from the **pycparser** package,
136it will interact with ``cpp`` for you, as long as it's in your PATH, or you
137provide a path to it.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700138
Eli Bendersky7c9daf42014-04-23 16:59:01 -0700139Note also that you can use ``gcc -E`` or ``clang -E`` instead of ``cpp``. See
Eli Benderskyd69771e2015-05-10 08:19:38 -0700140the ``using_gcc_E_libc.py`` example for more details. Windows users can download
Eli Benderskyb35f7832014-08-19 05:41:11 -0700141and install a binary build of Clang for Windows `from this website
142<http://llvm.org/releases/download.html>`_.
Eli Bendersky7c9daf42014-04-23 16:59:01 -0700143
Eli Benderskycd2605e2013-06-13 06:25:20 -0700144What about the standard C library headers?
145------------------------------------------
146
Eli Bendersky17a0ba82017-07-04 15:07:00 -0700147C code almost always ``#include``\s various header files from the standard C
148library, like ``stdio.h``. While (with some effort) **pycparser** can be made to
149parse the standard headers from any C compiler, it's much simpler to use the
150provided "fake" standard includes in ``utils/fake_libc_include``. These are
151standard C header files that contain only the bare necessities to allow valid
152parsing of the files that use them. As a bonus, since they're minimal, it can
153significantly improve the performance of parsing large C files.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700154
Eli Benderskye242e4c2013-08-03 06:15:32 -0700155The key point to understand here is that **pycparser** doesn't really care about
156the semantics of types. It only needs to know whether some token encountered in
157the source is a previously defined type. This is essential in order to be able
158to parse C correctly.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700159
Eli Benderskya1334e52015-05-18 06:24:05 -0700160See `this blog post
Jon Dufresne1d866992018-06-26 13:49:35 -0700161<https://eli.thegreenplace.net/2015/on-parsing-c-type-declarations-and-fake-headers>`_
Eli Benderskya1334e52015-05-18 06:24:05 -0700162for more details.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700163
164Basic usage
165-----------
166
B M Corser97e74642017-12-31 12:54:38 +0000167Take a look at the |examples|_ directory of the distribution for a few examples
Eli Bendersky2e387d42018-04-12 20:24:55 -0700168of using **pycparser**. These should be enough to get you started. Please note
169that most realistic C code samples would require running the C preprocessor
170before passing the code to **pycparser**; see the previous sections for more
171details.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700172
B M Corser97e74642017-12-31 12:54:38 +0000173.. |examples| replace:: ``examples``
174.. _examples: examples
175
Eli Bendersky2e387d42018-04-12 20:24:55 -0700176
Eli Benderskycd2605e2013-06-13 06:25:20 -0700177Advanced usage
178--------------
179
Eli Benderskye242e4c2013-08-03 06:15:32 -0700180The public interface of **pycparser** is well documented with comments in
181``pycparser/c_parser.py``. For a detailed overview of the various AST nodes
182created by the parser, see ``pycparser/_c_ast.cfg``.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700183
Eli Benderskye242e4c2013-08-03 06:15:32 -0700184There's also a `FAQ available here <https://github.com/eliben/pycparser/wiki/FAQ>`_.
185In any case, you can always drop me an `email <eliben@gmail.com>`_ for help.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700186
Eli Bendersky17a0ba82017-07-04 15:07:00 -0700187
Eli Benderskycd2605e2013-06-13 06:25:20 -0700188Modifying
189=========
190
191There are a few points to keep in mind when modifying **pycparser**:
192
Eli Benderskye242e4c2013-08-03 06:15:32 -0700193* The code for **pycparser**'s AST nodes is automatically generated from a
194 configuration file - ``_c_ast.cfg``, by ``_ast_gen.py``. If you modify the AST
195 configuration, make sure to re-generate the code.
196* Make sure you understand the optimized mode of **pycparser** - for that you
197 must read the docstring in the constructor of the ``CParser`` class. For
198 development you should create the parser without optimizations, so that it
199 will regenerate the Yacc and Lex tables when you change the grammar.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700200
201
202Package contents
203================
204
Eli Benderskye242e4c2013-08-03 06:15:32 -0700205Once you unzip the ``pycparser`` package, you'll see the following files and
206directories:
Eli Benderskycd2605e2013-06-13 06:25:20 -0700207
208README.rst:
209 This README file.
210
Eli Bendersky736c1262015-04-18 09:00:19 -0700211LICENSE:
212 The pycparser license
213
Eli Benderskycd2605e2013-06-13 06:25:20 -0700214setup.py:
215 Installation script
216
217examples/:
218 A directory with some examples of using **pycparser**
219
220pycparser/:
221 The **pycparser** module source code.
222
223tests/:
224 Unit tests.
225
Eli Benderskycd2605e2013-06-13 06:25:20 -0700226utils/fake_libc_include:
227 Minimal standard C library include files that should allow to parse any C code.
228
229utils/internal/:
230 Internal utilities for my own use. You probably don't need them.
231
Eli Bendersky17a0ba82017-07-04 15:07:00 -0700232
Eli Benderskycd2605e2013-06-13 06:25:20 -0700233Contributors
234============
235
236Some people have contributed to **pycparser** by opening issues on bugs they've
237found and/or submitting patches. The list of contributors is in the CONTRIBUTORS
Eli Bendersky17a0ba82017-07-04 15:07:00 -0700238file in the source distribution. After **pycparser** moved to Github I stopped
Eli Benderskyd69771e2015-05-10 08:19:38 -0700239updating this list because Github does a much better job at tracking
240contributions.
Eli Benderskycd2605e2013-06-13 06:25:20 -0700241
Eli Bendersky17a0ba82017-07-04 15:07:00 -0700242
Eli Benderskycd2605e2013-06-13 06:25:20 -0700243CI Status
244=========
245
246**pycparser** has automatic testing enabled through the convenient
247`Travis CI project <https://travis-ci.org>`_. Here is the latest build status:
248
249.. image:: https://travis-ci.org/eliben/pycparser.png?branch=master
250 :align: center
Eli Bendersky5759e5d2013-06-13 06:26:56 -0700251 :target: https://travis-ci.org/eliben/pycparser
252
Eli Bendersky5494c082017-04-21 15:57:15 -0700253AppVeyor also helps run tests on Windows:
254
255.. image:: https://ci.appveyor.com/api/projects/status/wrup68o5y8nuk1i9?svg=true
256 :align: center
257 :target: https://ci.appveyor.com/project/eliben/pycparser/