# pycparser — card for AI agents

C language parser written in pure Python for language tools.

Category: Languages, types and runtimes · PyPI package `pycparser` · latest version 3.12 (checked 2026-11-01)
Install: `pip install pycparser`
License: BSD-3-Clause (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Numbers (AgentGid data)
- Gid Score: 73.1 (#270 of all tools, 5 of 70 in its category)
- Used in 39 public projects set up for AI coding agents (0.6%)
- Downloads last week: 282,297,879 (as of 2026-11-24), +17.2% over 4 weeks
- GitHub stars: 3,583 (as of 2026-11-24)
- Last push: 2026-11-12

## Often used with
CFFI 97.4%, Pydantic 94.9%, Click 89.7%, urllib3 89.7%, cryptography 87.2%

## Checked alternatives (same job)
- Cython: https://agentgid.com/tools/cython/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/pycparser/
- Homepage: https://github.com/eliben/pycparser
- Repository: https://github.com/eliben/pycparser


## pycparser: read C code from Python scripts

pycparser is a free Python library that reads C source code and turns it into a tree of parts a script can walk, like every function, type and parameter. It is for tools that check, document or change C code. Most people get it without asking, because CFFI uses it.

### Where to use it
- List the functions in a C header: Your script reads a library's header file and prints every function with its parameters, for docs or a wrapper.
- Check C code for house rules: A small checker flags code that breaks your team's rules, such as a banned function or a missing return type, before review.
- Find tests in C projects: Scan C test files for functions named test_* and build the list your test runner calls.
- Generate bindings for other languages: Read a header once and write matching declarations for Python, Rust or JavaScript code that calls the same C library.
- Rewrite C code automatically: Change the tree, for example rename a function everywhere, and print it back as C source with the built-in code generator. (idea, not from the official pages)
- Teach how C declarations read: Show students or teammates how a confusing declaration like a pointer to an array of functions breaks down, part by part. (idea, not from the official pages)

### How to start
- Install it with pip into your project's environment; it has no other packages of its own.

```
> pip install pycparser
```

- Parse a short piece of C directly, or a real file through the C preprocessor with parse_file(use_cpp=True).

```
from pycparser import CParser, parse_file

ast = parse_file(
    "example.c",
    use_cpp=True,
    cpp_args=["-std=c23"],
    parser=CParser(dialect="c23"),
)
```

- Walk the tree with a NodeVisitor subclass and pick the parts you need.

### License
Free for any use, including paid products, under the BSD 3-Clause license. Keep the copyright notice and conditions when you ship its files, and do not use the author's name to promote your product.

### Good to know
- Real C files must go through the C preprocessor first; plain text with include or define lines or comments fails to parse.
- It reads C only, up to C11 by default, with C23 behind a setting; it does not read C++.
- Code full of GCC-specific extras needs extra setup before it parses.
- The small stand-in system headers that make parsing easier are in the source code, not in the installed package.
- The current version needs Python 3.11 or newer; 3.12 came out on 22 October 2026.

Checked 2026-11-11 against: https://github.com/eliben/pycparser/blob/main/README.rst, https://github.com/eliben/pycparser/blob/main/LICENSE, https://pypi.org/pypi/pycparser/json, https://github.com/eliben/pycparser/releases/tag/release_v3.12, https://github.com/eliben/pycparser/releases/tag/release_v3.00

### Mistakes to avoid
- Don't: CParser().parse(open("file.c").read()) on a real file with #include, #define or comments → Do: parse_file("file.c", use_cpp=True), or run gcc -E / clang -E first and parse the output (In our run the raw header failed with "ParseError - :1:1: Directives not supported yet"; through cpp it parsed.)
- Don't: Preprocessing with the system's real headers (stdio.h and friends) and hitting errors on compiler extras → Do: Point the preprocessor at the fake headers from the project's utils/fake_libc_include with -I (The real system headers are full of compiler extensions; the fake ones hold only what parsing needs.)
- Don't: Expecting pip install pycparser to bring utils/fake_libc_include → Do: Fetch that folder from the project's source (a release archive) and keep it in your repository (The fake headers are not part of the pip package.)
- Don't: Parsing C23 code with bool or other new keywords using the default parser → Do: CParser(dialect="c23") together with cpp_args=["-std=c23"] (C23 support is off by default because its new keywords clash with older code.)
- Don't: pip install pycparser==3.12 on Python 3.10 → Do: Use Python 3.11+ for 3.x, or let pip pick an older 2.x release on Python 3.10 (pycparser 3.12 declares Python 3.11 as the minimum.)

### Questions
- Why did I get pycparser without installing it? CFFI uses pycparser to read C declarations, so installing CFFI (or a package built on it) brings it in.
- Does it need anything else installed? No other Python packages. For real files you also need a C preprocessor such as cpp, gcc -E or clang -E on your PATH.
- Did version 3 change how I use it? Version 3.00 rewrote the parser by hand and dropped the PLY dependency; the release notes say the same tree comes out.
- Can it read C23 code? Yes, when you create the parser with dialect="c23" and pass -std=c23 to the preprocessor yourself.
- Can I use it in a commercial product? Yes. The BSD 3-Clause license allows it if you keep the notice and do not use the author's name for promotion.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
