---
title: "pycparser | pypx"
description: "C parser in Python"
canonical_url: "https://pypx.app/packages/pycparser"
meta:
  "og:description": "C parser in Python"
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_pycparser%2Cversion_3.12%2Csummary_C%2Bparser%2Bin%2BPython%2Clicense_BSD-3-Clause%2Cp_Ii9wYWNrYWdlcy9weWNwYXJzZXIi%2Cs_4VQCOXLVvGP-jnCL.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "pycparser 3.12"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_pycparser%2Cversion_3.12%2Csummary_C%2Bparser%2Bin%2BPython%2Clicense_BSD-3-Clause%2Cp_Ii9wYWNrYWdlcy9weWNwYXJzZXIi%2Cs_4VQCOXLVvGP-jnCL.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **pycparser**

v3.12

C parser in Python

61 KB Python 3.11+ py3-none-anyBSD-3-Clause

[** Diff **](https://pypx.app/packages/pycparser/diff)

`$ uv add pycparser`

=========

# pycparser

\----

# Introduction

## What is pycparser?

**pycparser** is a parser for the C language, written in pure Python. It is a module designed to be easily integrated into applications that need to parse C source code.

## What is it good for?

Anything that needs C code to be parsed. The following are some uses for **pycparser**, taken from real user reports:

- C code obfuscator
- Front-end for various specialized C compilers
- Static code checker
- Automatic unit-test discovery
- Adding specialized extensions to the C language

One of the most popular uses of **pycparser** is in the [cffi](https://cffi.readthedocs.io/en/latest/) library, which uses it to parse the declarations of C functions and types in order to auto-generate FFIs.

**pycparser** is unique in the sense that it's written in pure Python - a very high level language that's easy to experiment with and tweak. To people familiar with Lex and Yacc, **pycparser**'s code will be simple to understand. It also has no external dependencies (except for a Python interpreter), making it very simple to install and deploy.

## Which version of C does pycparser support?

**pycparser** aims to support the full C11 language, including earlier revisions like C99.

C23 support is conditional on a dialect="c23" setting, since this revision introduces new keywords like bool that would conflict with existing C code if not handled properly.

**pycparser** supports very few GCC extensions, but it's fairly easy to set things up so that it parses code with a lot of GCC-isms successfully. See the [FAQ](https://github.com/eliben/pycparser/wiki/FAQ) for more details.

## What grammar does pycparser follow?

**pycparser** very closely follows the C grammar provided in Annex A of the C99 standard (ISO/IEC 9899).

## How is pycparser licensed?

[BSD license](https://github.com/eliben/pycparser/blob/main/LICENSE).

## Contact details

For reporting problems with **pycparser** or submitting feature requests, please open an [issue](https://github.com/eliben/pycparser/issues), or submit a pull request.

# Installing

## Prerequisites

**pycparser** is being tested with modern versions of Python on Linux, macOS and Windows. See [the CI dashboard](https://github.com/eliben/pycparser/actions/workflows/ci.yml)\_ for details.

**pycparser** has no external dependencies.

## Installation process

The recommended way to install **pycparser** is with pip:

```
> pip install pycparser
```

# Using

## Interaction with the C preprocessor

In order to be compilable, C code must be preprocessed by the C preprocessor - cpp. A compatible cpp handles preprocessing directives like #include and #define, removes comments, and performs other minor tasks that prepare the C code for compilation.

For all but the most trivial snippets of C code **pycparser**, like a C compiler, must receive preprocessed C code in order to function correctly. If you import the top-level parse\_file function from the **pycparser** package, it will interact with cpp for you, as long as it's in your PATH, or you provide a path to it.

Note also that you can use gcc -E or clang -E instead of cpp. See the using\_gcc\_E\_libc.py example for more details. Windows users can download and install a binary build of Clang for Windows [from this website](http://llvm.org/releases/download.html).

## What about the standard C library headers?

C code almost always #include\\s various header files from the standard C library, like stdio.h. While (with some effort) **pycparser** can be made to parse the standard headers from any C compiler, it's much simpler to use the provided "fake" standard includes for C11 in utils/fake\_libc\_include. These are standard C header files that contain only the bare necessities to allow valid parsing of the files that use them. As a bonus, since they're minimal, it can significantly improve the performance of parsing large C files.

The key point to understand here is that **pycparser** doesn't really care about the semantics of types. It only needs to know whether some token encountered in the source is a previously defined type. This is essential in order to be able to parse C correctly.

See [this blog post](https://eli.thegreenplace.net/2015/on-parsing-c-type-declarations-and-fake-headers) for more details.

Note that the fake headers are not included in the pip package nor installed via the package build ([#224](https://github.com/eliben/pycparser/issues/224)).

## Basic usage

Take a look at the |examples|\_ directory of the distribution for a few examples of using **pycparser**. These should be enough to get you started. Please note that most realistic C code samples would require running the C preprocessor before passing the code to **pycparser**; see the previous sections for more details.

For example, to parse C23 source with a preprocessor that supports -std=c23:

```
from pycparser import CParser, parse_file

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

The parser's dialect does not configure the external preprocessor; select its language mode separately through cpp\_args.

## Advanced usage

The public interface of **pycparser** is well documented with comments in pycparser/c\_parser.py. For a detailed overview of the various AST nodes created by the parser, see pycparser/\_c\_ast.cfg.

The root node is a FileAST, whose ext attribute is a list of top-level declarations and function definitions. Explore nodes through their named fields, and use show() on a subtree to inspect just that part of the AST::

from pycparser import CParser

ast = CParser().parse("int x; int f(void) { return x; }") print(len(ast.ext)) # 2 func = ast.ext\[1] func.body.show()

For generic exploration, call children() to get a tuple of (field\_name, child\_node) pairs for the immediate children. The tuple supports length and indexing directly::

children = func.children() print(len(children)) # 2: decl and body name, child = children\[0] print(name) # decl child.show()

Iterating over a node directly (for child in node) yields just the child nodes, without their names. For a guided walkthrough, see [examples/explore\_ast.py](https://pypx.app/examples/explore_ast.py); for recursive traversal, see the NodeVisitor example in [examples/func\_defs.py](https://pypx.app/examples/func_defs.py).

There's also a [FAQ available here](https://github.com/eliben/pycparser/wiki/FAQ). In any case, you can always drop me an [email](https://pypx.app/eliben@gmail.com) for help.

# Modifying

There are a few points to keep in mind when modifying **pycparser**:

- The code for **pycparser**'s AST nodes is automatically generated from a

configuration file - \_c\_ast.cfg, by \_ast\_gen.py. If you modify the AST configuration or the generator, run make regen-ast from the repository root to re-generate pycparser/c\_ast.py and run make format.

- Read the docstring in the constructor of the CParser class for details

on configuration and compatibility arguments.

# Package contents

Once you unzip the pycparser package, you'll see the following files and directories:

README.rst: This README file.

LICENSE: The pycparser license

setup.py: Legacy installation script (build metadata lives in pyproject.toml).

pyproject.toml: Package metadata and build configuration.

examples/: A directory with some examples of using **pycparser**

pycparser/: The **pycparser** module source code.

tests/: Unit tests.

utils/fake\_libc\_include: Minimal standard C library include files that should allow to parse any C code. Note that these headers now include C11 code, so they may not work when the preprocessor is configured to an earlier C standard (like -std=c99).

utils/internal/: Internal utilities for my own use. You probably don't need them.

# Contributors

Some people have contributed to **pycparser** by opening issues on bugs they've found and/or submitting patches. The list of contributors is in the CONTRIBUTORS file in the source distribution. After **pycparser** moved to Github I stopped updating this list because Github does a much better job at tracking contributions.

## **Details **

<dl>

<dt>Version</dt>
<dd>3.12</dd>

<dt>License</dt>
<dd>BSD-3-Clause</dd>

<dt>Python</dt>
<dd>&gt;=3.11</dd>

<dt>Maintainer</dt>
<dd>Eli Bendersky &lt;eliben@gmail.com&gt;</dd></dl>

## **Links**

- [Homepage](https://github.com/eliben/pycparser)
- [View on PyPI](https://pypi.org/project/pycparser/)

## **Release Cadence **

**4**

releases in the past year

avg 220 days between releases

## **Maintainers **

- Eli Bendersky · [eliben@gmail.com](mailto:eliben@gmail.com)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/pycparser#webpage","@type":"WebPage","description":"C parser in Python","name":"pycparser","url":"https://pypx.app/packages/pycparser","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/pycparser"]}]},{"@id":"https://pypx.app/packages/pycparser#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"C parser in Python","license":"BSD-3-Clause","name":"pycparser","softwareVersion":"3.12"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
