Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 1 | =============== |
Eli Bendersky | caa4c11 | 2018-09-19 05:21:20 -0700 | [diff] [blame] | 2 | pycparser v2.19 |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 3 | =============== |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 4 | |
Jon Dufresne | 1d86699 | 2018-06-26 13:49:35 -0700 | [diff] [blame] | 5 | :Author: `Eli Bendersky <https://eli.thegreenplace.net/>`_ |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 6 | |
| 7 | |
| 8 | .. contents:: |
| 9 | :backlinks: none |
| 10 | |
| 11 | .. sectnum:: |
| 12 | |
| 13 | |
| 14 | Introduction |
| 15 | ============ |
| 16 | |
| 17 | What is pycparser? |
| 18 | ------------------ |
| 19 | |
| 20 | **pycparser** is a parser for the C language, written in pure Python. It is a |
| 21 | module designed to be easily integrated into applications that need to parse |
| 22 | C source code. |
| 23 | |
| 24 | What is it good for? |
| 25 | -------------------- |
| 26 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 27 | Anything that needs C code to be parsed. The following are some uses for |
| 28 | **pycparser**, taken from real user reports: |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 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 | |
Eli Bendersky | e9f5bc7 | 2015-05-10 08:10:12 -0700 | [diff] [blame] | 36 | One of the most popular uses of **pycparser** is in the `cffi |
Adam Chainz | 8145344 | 2016-05-29 14:20:48 +0100 | [diff] [blame] | 37 | <https://cffi.readthedocs.io/en/latest/>`_ library, which uses it to parse the |
Eli Bendersky | e9f5bc7 | 2015-05-10 08:10:12 -0700 | [diff] [blame] | 38 | declarations of C functions and types in order to auto-generate FFIs. |
Eli Bendersky | d69771e | 2015-05-10 08:19:38 -0700 | [diff] [blame] | 39 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 40 | **pycparser** is unique in the sense that it's written in pure Python - a very |
| 41 | high level language that's easy to experiment with and tweak. To people familiar |
Eli Bendersky | d69771e | 2015-05-10 08:19:38 -0700 | [diff] [blame] | 42 | with Lex and Yacc, **pycparser**'s code will be simple to understand. It also |
| 43 | has no external dependencies (except for a Python interpreter), making it very |
| 44 | simple to install and deploy. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 45 | |
| 46 | Which version of C does pycparser support? |
| 47 | ------------------------------------------ |
| 48 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 49 | **pycparser** aims to support the full C99 language (according to the standard |
Eli Bendersky | 736c126 | 2015-04-18 09:00:19 -0700 | [diff] [blame] | 50 | ISO/IEC 9899). Some features from C11 are also supported, and patches to support |
| 51 | more are welcome. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 52 | |
Eli Bendersky | d69771e | 2015-05-10 08:19:38 -0700 | [diff] [blame] | 53 | **pycparser** supports very few GCC extensions, but it's fairly easy to set |
| 54 | things 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 Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 56 | |
| 57 | What grammar does pycparser follow? |
| 58 | ----------------------------------- |
| 59 | |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 60 | **pycparser** very closely follows the C grammar provided in Annex A of the C99 |
| 61 | standard (ISO/IEC 9899). |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 62 | |
| 63 | How is pycparser licensed? |
| 64 | -------------------------- |
| 65 | |
Eli Bendersky | 5dbe676 | 2016-10-31 05:27:45 -0700 | [diff] [blame] | 66 | `BSD license <https://github.com/eliben/pycparser/blob/master/LICENSE>`_. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 67 | |
| 68 | Contact details |
| 69 | --------------- |
| 70 | |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 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. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 74 | |
| 75 | |
| 76 | Installing |
| 77 | ========== |
| 78 | |
| 79 | Prerequisites |
| 80 | ------------- |
| 81 | |
Jon Dufresne | a301cbb | 2018-06-28 06:12:24 -0700 | [diff] [blame] | 82 | * **pycparser** was tested on Python 2.7, 3.4-3.6, on both Linux and |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 83 | Windows. It should work on any later version (in both the 2.x and 3.x lines) |
| 84 | as well. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 85 | |
Eli Bendersky | 2ecc87c | 2013-08-03 07:03:28 -0700 | [diff] [blame] | 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 |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 88 | 3.10, retrieved from `<http://www.dabeaz.com/ply/>`_ |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 89 | |
Eli Bendersky | 2fdaa98 | 2017-07-21 06:36:37 -0700 | [diff] [blame] | 90 | Note that **pycparser** (and PLY) uses docstrings for grammar specifications. |
| 91 | Python installations that strip docstrings (such as when using the Python |
| 92 | ``-OO`` option) will fail to instantiate and use **pycparser**. You can try to |
| 93 | work around this problem by making sure the PLY parsing tables are pre-generated |
| 94 | in normal mode; this isn't an officially supported/tested mode of operation, |
| 95 | though. |
| 96 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 97 | Installation process |
| 98 | -------------------- |
| 99 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 100 | Installing **pycparser** is very simple. Once you download and unzip the |
| 101 | package, you just have to execute the standard ``python setup.py install``. The |
| 102 | setup script will then place the ``pycparser`` module into ``site-packages`` in |
| 103 | your Python's installation library. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 104 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 105 | Alternatively, since **pycparser** is listed in the `Python Package Index |
Jon Dufresne | 2d717d4 | 2018-06-10 05:21:29 -0700 | [diff] [blame] | 106 | <https://pypi.org/project/pycparser/>`_ (PyPI), you can install it using your |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 107 | favorite Python packaging/distribution tool, for example with:: |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 108 | |
| 109 | > pip install pycparser |
| 110 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 111 | Known problems |
| 112 | -------------- |
| 113 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 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. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 120 | |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 121 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 122 | Using |
| 123 | ===== |
| 124 | |
| 125 | Interaction with the C preprocessor |
| 126 | ----------------------------------- |
| 127 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 128 | In order to be compilable, C code must be preprocessed by the C preprocessor - |
| 129 | ``cpp``. ``cpp`` handles preprocessing directives like ``#include`` and |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 130 | ``#define``, removes comments, and performs other minor tasks that prepare the C |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 131 | code for compilation. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 132 | |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 133 | For all but the most trivial snippets of C code **pycparser**, like a C |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 134 | compiler, must receive preprocessed C code in order to function correctly. If |
| 135 | you import the top-level ``parse_file`` function from the **pycparser** package, |
| 136 | it will interact with ``cpp`` for you, as long as it's in your PATH, or you |
| 137 | provide a path to it. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 138 | |
Eli Bendersky | 7c9daf4 | 2014-04-23 16:59:01 -0700 | [diff] [blame] | 139 | Note also that you can use ``gcc -E`` or ``clang -E`` instead of ``cpp``. See |
Eli Bendersky | d69771e | 2015-05-10 08:19:38 -0700 | [diff] [blame] | 140 | the ``using_gcc_E_libc.py`` example for more details. Windows users can download |
Eli Bendersky | b35f783 | 2014-08-19 05:41:11 -0700 | [diff] [blame] | 141 | and install a binary build of Clang for Windows `from this website |
| 142 | <http://llvm.org/releases/download.html>`_. |
Eli Bendersky | 7c9daf4 | 2014-04-23 16:59:01 -0700 | [diff] [blame] | 143 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 144 | What about the standard C library headers? |
| 145 | ------------------------------------------ |
| 146 | |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 147 | C code almost always ``#include``\s various header files from the standard C |
| 148 | library, like ``stdio.h``. While (with some effort) **pycparser** can be made to |
| 149 | parse the standard headers from any C compiler, it's much simpler to use the |
| 150 | provided "fake" standard includes in ``utils/fake_libc_include``. These are |
| 151 | standard C header files that contain only the bare necessities to allow valid |
| 152 | parsing of the files that use them. As a bonus, since they're minimal, it can |
| 153 | significantly improve the performance of parsing large C files. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 154 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 155 | The key point to understand here is that **pycparser** doesn't really care about |
| 156 | the semantics of types. It only needs to know whether some token encountered in |
| 157 | the source is a previously defined type. This is essential in order to be able |
| 158 | to parse C correctly. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 159 | |
Eli Bendersky | a1334e5 | 2015-05-18 06:24:05 -0700 | [diff] [blame] | 160 | See `this blog post |
Jon Dufresne | 1d86699 | 2018-06-26 13:49:35 -0700 | [diff] [blame] | 161 | <https://eli.thegreenplace.net/2015/on-parsing-c-type-declarations-and-fake-headers>`_ |
Eli Bendersky | a1334e5 | 2015-05-18 06:24:05 -0700 | [diff] [blame] | 162 | for more details. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 163 | |
| 164 | Basic usage |
| 165 | ----------- |
| 166 | |
B M Corser | 97e7464 | 2017-12-31 12:54:38 +0000 | [diff] [blame] | 167 | Take a look at the |examples|_ directory of the distribution for a few examples |
Eli Bendersky | 2e387d4 | 2018-04-12 20:24:55 -0700 | [diff] [blame] | 168 | of using **pycparser**. These should be enough to get you started. Please note |
| 169 | that most realistic C code samples would require running the C preprocessor |
| 170 | before passing the code to **pycparser**; see the previous sections for more |
| 171 | details. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 172 | |
B M Corser | 97e7464 | 2017-12-31 12:54:38 +0000 | [diff] [blame] | 173 | .. |examples| replace:: ``examples`` |
| 174 | .. _examples: examples |
| 175 | |
Eli Bendersky | 2e387d4 | 2018-04-12 20:24:55 -0700 | [diff] [blame] | 176 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 177 | Advanced usage |
| 178 | -------------- |
| 179 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 180 | The public interface of **pycparser** is well documented with comments in |
| 181 | ``pycparser/c_parser.py``. For a detailed overview of the various AST nodes |
| 182 | created by the parser, see ``pycparser/_c_ast.cfg``. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 183 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 184 | There's also a `FAQ available here <https://github.com/eliben/pycparser/wiki/FAQ>`_. |
| 185 | In any case, you can always drop me an `email <eliben@gmail.com>`_ for help. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 186 | |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 187 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 188 | Modifying |
| 189 | ========= |
| 190 | |
| 191 | There are a few points to keep in mind when modifying **pycparser**: |
| 192 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 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. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 200 | |
| 201 | |
| 202 | Package contents |
| 203 | ================ |
| 204 | |
Eli Bendersky | e242e4c | 2013-08-03 06:15:32 -0700 | [diff] [blame] | 205 | Once you unzip the ``pycparser`` package, you'll see the following files and |
| 206 | directories: |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 207 | |
| 208 | README.rst: |
| 209 | This README file. |
| 210 | |
Eli Bendersky | 736c126 | 2015-04-18 09:00:19 -0700 | [diff] [blame] | 211 | LICENSE: |
| 212 | The pycparser license |
| 213 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 214 | setup.py: |
| 215 | Installation script |
| 216 | |
| 217 | examples/: |
| 218 | A directory with some examples of using **pycparser** |
| 219 | |
| 220 | pycparser/: |
| 221 | The **pycparser** module source code. |
| 222 | |
| 223 | tests/: |
| 224 | Unit tests. |
| 225 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 226 | utils/fake_libc_include: |
| 227 | Minimal standard C library include files that should allow to parse any C code. |
| 228 | |
| 229 | utils/internal/: |
| 230 | Internal utilities for my own use. You probably don't need them. |
| 231 | |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 232 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 233 | Contributors |
| 234 | ============ |
| 235 | |
| 236 | Some people have contributed to **pycparser** by opening issues on bugs they've |
| 237 | found and/or submitting patches. The list of contributors is in the CONTRIBUTORS |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 238 | file in the source distribution. After **pycparser** moved to Github I stopped |
Eli Bendersky | d69771e | 2015-05-10 08:19:38 -0700 | [diff] [blame] | 239 | updating this list because Github does a much better job at tracking |
| 240 | contributions. |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 241 | |
Eli Bendersky | 17a0ba8 | 2017-07-04 15:07:00 -0700 | [diff] [blame] | 242 | |
Eli Bendersky | cd2605e | 2013-06-13 06:25:20 -0700 | [diff] [blame] | 243 | CI 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 Bendersky | 5759e5d | 2013-06-13 06:26:56 -0700 | [diff] [blame] | 251 | :target: https://travis-ci.org/eliben/pycparser |
| 252 | |
Eli Bendersky | 5494c08 | 2017-04-21 15:57:15 -0700 | [diff] [blame] | 253 | AppVeyor 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/ |