{"id":4289,"library":"tree-sitter-cpp","title":"C++ Grammar for Tree-sitter","description":"tree-sitter-cpp provides the C++ grammar for the Tree-sitter parsing library. It enables Python applications to parse C++ code, build abstract syntax trees (ASTs), and perform code analysis tasks like syntax highlighting, linting, and refactoring. The library is actively maintained with frequent minor updates (as seen in its v0.23.x releases), and it requires the core `tree-sitter` Python package to function.","status":"active","version":"0.23.4","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-cpp","tags":["parsing","tree-sitter","cpp","grammar","syntax-tree","code-analysis"],"install":[{"cmd":"pip install tree-sitter-cpp tree-sitter","lang":"bash","label":"Install core library and C++ grammar"}],"dependencies":[{"reason":"Provides the core Python bindings and parsing engine required to utilize the C++ grammar. tree-sitter-cpp only provides the language definition.","package":"tree-sitter"}],"imports":[{"note":"The `Language` and `Parser` classes are part of the core `tree-sitter` library, not `tree-sitter-cpp` itself. `tree_sitter_cpp` provides the grammar object.","wrong":"from tree_sitter_cpp import Language, Parser","symbol":"Language, Parser","correct":"from tree_sitter import Language, Parser"},{"note":"For pre-compiled grammars installed via pip, the `language()` function from the grammar package (e.g., `tree_sitter_cpp.language()`) should be used directly with `tree_sitter.Language`. The `Language.build_library` method was removed from `py-tree-sitter` around version 0.21.x and is no longer the recommended approach for using distributed grammars.","wrong":"cpp_language = Language.build_library('/path/to/grammar.so', ['/path/to/tree-sitter-cpp'])","symbol":"language()","correct":"import tree_sitter_cpp as ts_cpp\ncpp_language = Language(ts_cpp.language())"}],"quickstart":{"code":"import tree_sitter_cpp as ts_cpp\nfrom tree_sitter import Language, Parser\n\n# Load the C++ grammar\nCPP_LANGUAGE = Language(ts_cpp.language())\n\n# Create a parser and set its language\nparser = Parser()\nparser.set_language(CPP_LANGUAGE)\n\n# C++ code to parse\ncpp_code = b\"\"\"#include <iostream>\n\nint main() {\n    std::cout << \"Hello, Tree-sitter!\" << std::endl;\n    return 0;\n}\n\"\"\"\n\n# Parse the code\ntree = parser.parse(cpp_code)\n\n# Get the root node of the syntax tree\nroot_node = tree.root_node\n\n# Print the root node type and its first child (for demonstration)\nprint(f\"Root node type: {root_node.type}\")\nif root_node.children:\n    print(f\"First child type: {root_node.children[0].type}\")\n    print(f\"First child text: {root_node.children[0].text.decode('utf8')}\")\n\n# Example of traversing the tree (optional, for deeper analysis)\ndef traverse(node, depth=0):\n    print('  ' * depth + f'- {node.type} [ {node.start_point} - {node.end_point} ] {node.text.decode('utf8') if node.text else ''}')\n    for child in node.children:\n        traverse(child, depth + 1)\n\n# print(\"\\nFull AST:\")\n# traverse(root_node)\n","lang":"python","description":"This quickstart demonstrates how to load the `tree-sitter-cpp` grammar, initialize a `tree_sitter` parser with it, and parse a simple C++ code snippet. It then shows how to access the root node of the generated syntax tree and print basic information about it and its children. The code uses `bytes` objects for source code, as required by the `tree-sitter` library."},"warnings":[{"fix":"Instead of `Language.build_library(...)`, you should `pip install` the specific grammar package (e.g., `tree-sitter-cpp`) and load it using `Language(grammar_package.language())`. For `tree-sitter-cpp`, this means `import tree_sitter_cpp as ts_cpp; cpp_language = Language(ts_cpp.language())`.","message":"The `Language.build_library` static method was removed from the `py-tree-sitter` library around version 0.21.x.","severity":"breaking","affected_versions":"tree-sitter>=0.21.x"},{"fix":"Ensure both packages are installed: `pip install tree-sitter tree-sitter-cpp`. Then, import `Language` and `Parser` from `tree_sitter` and the grammar from `tree_sitter_cpp`.","message":"Installing `tree-sitter-cpp` alone is not sufficient to parse C++ code in Python. The core `tree-sitter` Python package, which provides the `Language` and `Parser` classes, must also be installed and imported.","severity":"gotcha","affected_versions":"All"},{"fix":"Encode your source strings before passing them to `parser.parse()`: `parser.parse(your_string.encode('utf8'))`.","message":"The `tree-sitter` parsing functions expect source code as a `bytes` object (UTF-8 or UTF-16 encoded), not a standard Python `str`.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}