Debian Codegraph @

pycparser223/README.rst

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