Library / SDK
eliben/pycparser avatar
eliben/pycparser

pycparser: a C99 parser in pure Python, and when to reach for it

:snake: Complete C99 parser in pure Python

3,584 stars657 forksPythonNOASSERTION

At a glance

What is it?
pycparser turns preprocessed C into a Python AST with no external dependencies. It is the parsing layer inside cffi, and it is a poor fit for anything that needs real type semantics or heavy GCC extension support.
Who is it for?
Adopt pycparser if you are building tooling that reads C declarations or statements and you can run the code through a preprocessor first: cffi-style FFI generation, obfuscators, static checkers, AST rewriting. Do not adopt it if you need semantic type analysis, C++ work, or code full of GCC extensions you cannot strip, because the README states only very few GCC extensions are supported and the parser only needs to know whether a token is a previously defined type.
Can I use it commercially?
Check first. The repository uses a licence we do not classify automatically, so read its LICENSE file before any commercial use.
Is it still maintained?
Yes. The repository last received commits 12 days ago.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 24, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • eliben/pycparser3,584 ★ · +13
Star history
Repository2026-09-242026-11-24
eliben/pycparser3.6k3.6k

13 Stars in the last 61 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The problem pycparser solves, and the people who hit it

C source is text, and most tools that want to reason about C source need a tree, not text. pycparser exists to produce that tree in Python. The README lists the uses it was built around, drawn from user reports: C code obfuscators, front-ends for specialized C compilers, static code checkers, automatic unit-test discovery, and tools that add specialized extensions to the C language. The common thread is a program that must read C and then do something with the structure it found.

The audience is therefore Python developers writing tooling, not C developers writing programs. If you are generating bindings, rewriting declarations, or scanning for patterns across a codebase, and you would rather do that in Python than in a C front-end, this is the shape of library you want. The README is explicit that pycparser is a module designed to be integrated into applications that need to parse C source code, not a standalone binary you point at a file and get output from.

One naming trap is worth clearing up before anything else. Python's own argparse module has a method called parse_args(). It has nothing to do with this project. pycparser exposes a CParser class and a top-level parse_file function, and that is the surface you work with.

Preprocessing is not optional, and the fake headers explain why

The single most important design fact is that pycparser consumes preprocessed C. The README states that for all but the most trivial snippets, pycparser, like a C compiler, must receive preprocessed C code in order to function correctly. A compatible cpp handles #include and #define, strips comments, and does the other minor work that prepares C for compilation. If you import parse_file from the top-level package, it will drive cpp for you as long as cpp is on your PATH or you hand it a path. You can substitute gcc -E or clang -E, and the README points at using_gcc_E_libc.py for that.

The reason a parser needs the preprocessor at all comes down to a quirk of C's grammar. pycparser does not care about the semantics of types. It only needs to know whether a token it encounters is a previously defined type. That single bit of information is what lets it tell a declaration apart from an expression, and it is exactly what #include and typedef supply. This is why the project ships fake standard includes in utils/fake_libc_include: minimal headers that carry just enough to make parsing succeed, and, being minimal, can speed up parsing of large files considerably.

The trade-off is obvious once stated. You get a fast, dependency-free parser, and in exchange you take on a preprocessing step and a header story. The README warns that the fake headers are not included in the pip package and are not installed by the package build, citing issue #224. So a pip install gives you the parser and not the headers. If you need them, they come from the repository. The fake headers also now contain C11 code, which the README notes may cause trouble depending on the preprocessor you use.

Installing pycparser and parsing your first file

The README gives one installation command. There are no external dependencies, only a Python interpreter, and the package metadata requires Python 3.10 or newer.

bash
pip install pycparser

After that, the shortest useful path is parse_file, which runs cpp for you. It expects the preprocessor to be on your PATH, or a path to it to be supplied. The README also notes that Windows users can install a binary build of Clang for Windows if they would rather use clang -E than cpp.

The README points at the examples directory of the distribution for a few examples of using pycparser, and says these should be enough to get you started. The repository's examples directory contains dump_ast.py, explore_ast.py, rewrite_ast.py and serialize_ast.py among others, and the README notes that most realistic C code samples would require running the C preprocessor before passing the code to pycparser.

If you are working from a checkout rather than from pip, the AST node code is generated, not hand-written. The README is direct about this: the code for pycparser's AST nodes is automatically generated from _c_ast.cfg by _ast_gen.py, and if you change the AST configuration you must re-run _ast_gen.py from the repository root or the pycparser directory. Editing the generated files directly is a dead end. The Makefile in the repository also shows how the project checks itself, running ruff and ty through uvx and executing the test suite with python3 -m unittest discover.

Where pycparser is the wrong tool

The README is unusually candid about scope, and the limits matter more than the features. pycparser aims at full C99 according to ISO/IEC 9899, follows the grammar in Annex A of that standard closely, and supports some features from C11. It supports very few GCC extensions. That last sentence is the one that decides most real projects.

Real-world C is full of __attribute__, inline assembly, compiler-specific pragmas and vendor keywords. The README suggests it is fairly easy to set things up so that pycparser parses code with a lot of GCC-isms successfully, and points at the FAQ for details, but that setup work is yours. If your input is a header-heavy codebase you do not control, budget for it.

The second limit is semantic. Because the parser only needs to know whether a token is a previously defined type, it is not a type checker and does not resolve what those types mean. If your tool needs to know that a struct field is 4 bytes wide, or that two declarations refer to the same underlying type, pycparser will hand you the syntax and stop there. That is a deliberate boundary, not a defect, and it is why the fake headers can be so thin.

The third limit is language. C++ is out of scope entirely, and C11 support is partial by the project's own description, with patches welcome. If you need modern C features beyond what is implemented, you are extending the grammar yourself.

pycparser against the libclang approach

The realistic alternative for most of these tasks is libclang, the Clang Python bindings. The difference in approach is not a matter of polish, it is architectural. libclang wraps a full C and C++ compiler front-end written in C++, so it gives you a real type system, full GCC and Clang extension handling, and a parser that has already seen every header your code includes. It also requires you to install and ship a native library, and the API you program against is a set of bindings over someone else's data structures.

pycparser makes the opposite bet. It is pure Python with no external dependencies beyond the interpreter, which the README presents as the reason it is simple to install and deploy. The code is readable in the way Lex and Yacc grammars are readable, and the README frames that as a strength for anyone who wants to experiment with and tweak the parser. Modifying the grammar is a realistic option here in a way it is not with a compiled front-end.

So the choice comes down to what you need from the tree. If you need to know that a token is a type and then rewrite or inspect declarations, pycparser is lighter and easier to ship. If you need to know what the type means, libclang is doing work pycparser deliberately does not attempt. Neither is a superset of the other.

Maintenance, releases and the licence question

The repository is not archived, and the last push was on 2026-09-15. The most recent release is release_v3.00, tagged on 2026-01-21, following release_v2.23 on 2025-09-09 and release_v2.22 on 2024-03-30. The spacing suggests a project that ships when there is something to ship rather than on a schedule, and the version jump from 2.23 to 3.00 is worth reading the release notes for before you upgrade, since the package metadata now requires Python 3.10 or newer.

Upgrade cost is low by construction. There are no external dependencies, so a version bump does not drag a dependency tree along with it. The real upgrade risk is the AST surface: if you consume the node structure, changes to _c_ast.cfg propagate into the generated node classes and can affect code that inspects or constructs nodes. The repository keeps an OLDER_CHANGES file alongside the changelog, which is where to look before moving a pinned version.

On licensing, the README states the project is under a BSD license, and the LICENSE file is in the repository root. The package metadata in pyproject.toml declares license = "BSD-3-Clause". The repository's machine-readable licence field is reported as NOASSERTION, which is a metadata classification rather than a statement about the terms. If you are embedding pycparser in a product, read the LICENSE file itself and get your own advice on attribution; nothing here should be read as legal guidance.

Editorial conclusion

Adopt pycparser if you are building tooling that reads C declarations or statements and you can run the code through a preprocessor first: cffi-style FFI generation, obfuscators, static checkers, AST rewriting. Do not adopt it if you need semantic type analysis, C++ work, or code full of GCC extensions you cannot strip, because the README states only very few GCC extensions are supported and the parser only needs to know whether a token is a previously defined type. Before committing, verify two things against your own codebase: that the fake headers in utils/fake_libc_include cover the system headers your files include, and that your preprocessing step is cpp, gcc -E or clang -E as the README describes. Note also that the fake headers are not shipped in the pip package.

Frequently asked questions

How do I install pycparser?

The README recommends pip install pycparser. There are no external dependencies, and the package requires Python 3.10 or newer according to its metadata.

How do I use pycparser to parse a C file?

Import parse_file from the pycparser package and pass it a file path; it will drive cpp for you if the preprocessor is on your PATH or you supply a path to it. The README stresses that all but the most trivial snippets must be preprocessed first.

Can pycparser run C code from Python?

No. pycparser parses C source into an AST; the README describes it as a module for applications that need to parse C source code, not an execution engine. Running C from Python is a different problem.

Why does pycparser need the C preprocessor?

The README states that pycparser, like a C compiler, must receive preprocessed C code to function correctly, because it only needs to know whether a token is a previously defined type. The preprocessor supplies those definitions through #include and typedef.

Does pycparser support C11 and GCC extensions?

The README says pycparser aims at full C99, supports some C11 features, and supports very few GCC extensions, though it notes that setup can make heavily GCC-flavoured code parse successfully. The FAQ on the project wiki covers that setup.

Where do the fake libc headers come from?

They live in utils/fake_libc_include in the repository. The README states they are not included in the pip package and are not installed by the package build, so a pip install does not bring them.

Official sources

  1. eliben/pycparser on GitHub
  2. Issues
  3. README
  4. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/eliben-pycparser.svg)](https://hysenlabs.com/projects/eliben-pycparser)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/eliben-pycparser.svg)](https://hysenlabs.com/projects/eliben-pycparser)

Was this useful?