{"id":2321,"library":"tree-sitter-c","title":"tree-sitter-c C Grammar","description":"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.","status":"active","version":"0.24.1","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-c","tags":["tree-sitter","parser","c","grammar","ast","syntax-tree"],"install":[{"cmd":"pip install tree-sitter tree-sitter-c","lang":"bash","label":"Install core library and C grammar"}],"dependencies":[{"reason":"Required for the core Tree-sitter Python bindings and parsing engine. tree-sitter-c provides only the C grammar definition.","package":"tree-sitter","optional":false}],"imports":[{"note":"The core classes are directly importable from the top-level `tree_sitter` package.","wrong":"from tree_sitter.binding import Language","symbol":"Language","correct":"from tree_sitter import Language, Parser"},{"note":"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())`.","wrong":"Language.build_library('/path/to/c.so', ['/path/to/tree-sitter-c-repo'])","symbol":"language","correct":"import tree_sitter_c as tsc\nC_LANGUAGE = Language(tsc.language())"}],"quickstart":{"code":"import tree_sitter_c as tsc\nfrom tree_sitter import Language, Parser\n\n# Load the C language grammar\nC_LANGUAGE = Language(tsc.language())\n\n# Create a parser and set its language\nparser = Parser()\nparser.set_language(C_LANGUAGE)\n\n# C code to parse\nc_code = b\"\"\"\nint main() {\n    printf(\"Hello, Tree-sitter C!\");\n    return 0;\n}\n\"\"\"\n\n# Parse the code\ntree = parser.parse(c_code)\n\n# Get the root node and print its type\nroot_node = tree.root_node\nprint(f\"Root node type: {root_node.type}\")\nprint(f\"Root node text: {root_node.text.decode('utf8')}\")\n\n# Example of traversing a child node\nif root_node.children:\n    first_child = root_node.children[0]\n    print(f\"First child type: {first_child.type}\")","lang":"python","description":"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."},"warnings":[{"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())`.","message":"The `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).","severity":"breaking","affected_versions":"tree-sitter >= 0.21.0"},{"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.","message":"Incompatibility 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').","severity":"gotcha","affected_versions":"All versions"},{"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')`.","message":"The `tree-sitter` parser expects source code to be provided as `bytes` objects, typically UTF-8 encoded.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}