Registry / database / tree-sitter-c

tree-sitter-c

JSON →
library0.24.2pypypi✓ verified 26d ago

tree-sitter-c is a Python package that provides the C language grammar for the Tree-sitter parsing library. It enables Python applications to parse C code into concrete syntax trees for advanced analysis, tooling, and transformation. The library is actively maintained, with frequent minor releases ensuring ongoing compatibility with the latest `tree-sitter` core and C language specifications.

pip install tree-sitter tree-sitter-c
INSTALL
IMPORT
SIG · TREE-SITTER-C
T
tree-sitter-c
databasepythonv0.24.2
Install
1.6s avg
Import
11ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.24.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.012s · 20MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.008s · 21MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Language
from tree_sitter import Language, Parser
from tree_sitter.binding import Language
The core classes are directly importable from the top-level `tree_sitter` package.
language
import tree_sitter_c as tsc C_LANGUAGE = Language(tsc.language())
Language.build_library('/path/to/c.so', ['/path/to/tree-sitter-c-repo'])
As of `tree-sitter` version ~0.21.x, `Language.build_library` has been removed. Pre-compiled grammars are now distributed as separate Python packages (e.g., `tree-sitter-c`) and loaded via `Language(tree_sitter_c.language())`.

This quickstart demonstrates how to import the `tree-sitter-c` grammar, initialize a `tree_sitter.Parser`, and parse a simple C code snippet. It then shows how to access the root node of the generated Abstract Syntax Tree (AST) and inspect its properties.

import tree_sitter_c as tsc from tree_sitter import Language, Parser # Load the C language grammar C_LANGUAGE = Language(tsc.language()) # Create a parser and set its language parser = Parser() parser.set_language(C_LANGUAGE) # C code to parse c_code = b""" int main() { printf("Hello, Tree-sitter C!"); return 0; } """ # Parse the code tree = parser.parse(c_code) # Get the root node and print its type root_node = tree.root_node print(f"Root node type: {root_node.type}") print(f"Root node text: {root_node.text.decode('utf8')}") # Example of traversing a child node if root_node.children: first_child = root_node.children[0] print(f"First child type: {first_child.type}")
Debug
Known issues
breakingThe `Language.build_library` method, previously used to compile grammars from source, has been removed from the `tree-sitter` Python bindings (around version 0.21.x).
fix
Instead of compiling from source, install pre-compiled language grammars directly via pip (e.g., `pip install tree-sitter-c`). Grammars are then loaded using `Language(tree_sitter_c.language())`.
affects: tree-sitter >= 0.21.0
gotchaIncompatibility between the installed `tree-sitter` core library version and the `tree-sitter-c` grammar package version can lead to runtime errors (e.g., 'version-mismatch').
fix
Ensure both `tree-sitter` and `tree-sitter-c` are up-to-date and compatible. If issues persist, try reinstalling both or checking the GitHub repositories for known compatibility matrices or specific version requirements.
affects: All versions
gotchaThe `tree-sitter` parser expects source code to be provided as `bytes` objects, typically UTF-8 encoded.
fix
Always encode your string input to bytes using `.encode('utf8')` before passing it to `parser.parse()`. For example, `my_c_code_string.encode('utf8')`.
affects: All versions
Upgrade
Version history
0.24.2latest on PyPI · released Apr 22, 2026
Audit
Dependencies
tree-sitterrequiredRequired for the core Tree-sitter Python bindings and parsing engine. tree-sitter-c provides only the C grammar definition.
Agent activity
37 hits · last 30 days
node
28
Meta
1
OpenAI (training)
1
Resources
tree-sitter-c — pip install tree-sitter-c · libregistry