• Home
Name
Date
Size
#Lines
LOC

..--

examples/03-May-2024-1,467710

pycparser/03-May-2024-10,5316,960

tests/03-May-2024-8,9463,624

utils/03-May-2024-1,117870

.gitignoreD03-May-2024139 1816

.travis.ymlD03-May-202499 87

CHANGESD03-May-20249.8 KiB260190

CONTRIBUTORSD03-May-2024598 2622

LICENSED03-May-20241.5 KiB2823

MANIFEST.inD03-May-2024335 1311

METADATAD03-May-2024485 2019

MODULE_LICENSE_MITD03-May-20240

NOTICED03-May-20241.5 KiB2823

README.rstD03-May-20248.7 KiB258182

TEST_MAPPINGD03-May-202488 98

TODO.txtD03-May-20241.4 KiB4433

_clean_tables.pyD03-May-2024871 2821

appveyor.ymlD03-May-2024213 139

setup.cfgD03-May-202463 64

setup.pyD03-May-20242.3 KiB6754

tox.iniD03-May-202488 75

README.rst

1===============
2pycparser v2.19
3===============
4
5:Author: `Eli Bendersky <https://eli.thegreenplace.net/>`_
6
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
27Anything that needs C code to be parsed. The following are some uses for
28**pycparser**, taken from real user reports:
29
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
36One of the most popular uses of **pycparser** is in the `cffi
37<https://cffi.readthedocs.io/en/latest/>`_ library, which uses it to parse the
38declarations of C functions and types in order to auto-generate FFIs.
39
40**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
42with 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.
45
46Which version of C does pycparser support?
47------------------------------------------
48
49**pycparser** aims to support the full C99 language (according to the standard
50ISO/IEC 9899). Some features from C11 are also supported, and patches to support
51more are welcome.
52
53**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.
56
57What grammar does pycparser follow?
58-----------------------------------
59
60**pycparser** very closely follows the C grammar provided in Annex A of the C99
61standard (ISO/IEC 9899).
62
63How is pycparser licensed?
64--------------------------
65
66`BSD license <https://github.com/eliben/pycparser/blob/master/LICENSE>`_.
67
68Contact details
69---------------
70
71For reporting problems with **pycparser** or submitting feature requests, please
72open an `issue <https://github.com/eliben/pycparser/issues>`_, or submit a
73pull request.
74
75
76Installing
77==========
78
79Prerequisites
80-------------
81
82* **pycparser** was tested on Python 2.7, 3.4-3.6, on both Linux and
83  Windows. It should work on any later version (in both the 2.x and 3.x lines)
84  as well.
85
86* **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
88  3.10, retrieved from `<http://www.dabeaz.com/ply/>`_
89
90Note 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
97Installation process
98--------------------
99
100Installing **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.
104
105Alternatively, since **pycparser** is listed in the `Python Package Index
106<https://pypi.org/project/pycparser/>`_ (PyPI), you can install it using your
107favorite Python packaging/distribution tool, for example with::
108
109    > pip install pycparser
110
111Known problems
112--------------
113
114* 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.
120
121
122Using
123=====
124
125Interaction with the C preprocessor
126-----------------------------------
127
128In order to be compilable, C code must be preprocessed by the C preprocessor -
129``cpp``. ``cpp`` handles preprocessing directives like ``#include`` and
130``#define``, removes comments, and performs other minor tasks that prepare the C
131code for compilation.
132
133For all but the most trivial snippets of C code **pycparser**, like a C
134compiler, 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.
138
139Note also that you can use ``gcc -E`` or ``clang -E`` instead of ``cpp``. See
140the ``using_gcc_E_libc.py`` example for more details. Windows users can download
141and install a binary build of Clang for Windows `from this website
142<http://llvm.org/releases/download.html>`_.
143
144What about the standard C library headers?
145------------------------------------------
146
147C 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.
154
155The 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.
159
160See `this blog post
161<https://eli.thegreenplace.net/2015/on-parsing-c-type-declarations-and-fake-headers>`_
162for more details.
163
164Basic usage
165-----------
166
167Take a look at the |examples|_ directory of the distribution for a few examples
168of 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.
172
173.. |examples| replace:: ``examples``
174.. _examples: examples
175
176
177Advanced usage
178--------------
179
180The 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``.
183
184There'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.
186
187
188Modifying
189=========
190
191There are a few points to keep in mind when modifying **pycparser**:
192
193* 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.
200
201
202Package contents
203================
204
205Once you unzip the ``pycparser`` package, you'll see the following files and
206directories:
207
208README.rst:
209  This README file.
210
211LICENSE:
212  The pycparser license
213
214setup.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
226utils/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
232
233Contributors
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
238file in the source distribution. After **pycparser** moved to Github I stopped
239updating this list because Github does a much better job at tracking
240contributions.
241
242
243CI 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
251  :target: https://travis-ci.org/eliben/pycparser
252
253AppVeyor 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/
258