{"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":"python","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,"search_vec":"'activ':49 'advanc':41 'analysi':42 'applic':31 'ast':75 'c':4,5,10,18,34,66,73 'code':35 'compat':57 'concret':37 'core':64 'enabl':29 'ensur':55 'frequent':52 'grammar':6,20,74 'languag':19,67 'latest':60 'librari':27,47 'maintain':50 'minor':53 'ongo':56 'packag':14 'pars':26,33 'parser':72 'provid':16 'python':13,30 'releas':54 'sitter':3,9,25,63,71 'specif':68 'syntax':38,77 'syntax-tre':76 'tool':43 'transform':45 'tree':2,8,24,39,62,70,78 'tree-sitt':23,61,69 'tree-sitter-c':1,7","created_at":"2026-04-09T18:52:59.413445+00:00","updated_at":"2026-04-09T18:52:59.413445+00:00","problems":{"verify_error":"error: Failed to parse: `tree-sitter tree-sitter-c`\n  Caused by: Expected one of `@`, `(`, `<`, `=`, `>`, `~`, `!`, `;`, found `t`\ntree-sitter tree-sitter-c\n            ^"},"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.24.2","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/tree-sitter/tree-sitter-c","docs":null,"changelog":null,"pypi":"https://pypi.org/project/tree-sitter-c/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database","serialization","ai-ml"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-28","next_check":"2026-07-05","install_tag":null}}