Created
April 21, 2024 22:18
-
-
Save secemp9/415db1ef3c1a030e748b69b990655fbd to your computer and use it in GitHub Desktop.
Parsing C code example using tree-sitter Python module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from tree_sitter import Language, Parser, Tree, Node | |
| import tree_sitter_c | |
| from typing import Generator | |
| C_LANGUAGE = Language(tree_sitter_c.language(), "c") | |
| parser = Parser() | |
| parser.set_language(C_LANGUAGE) | |
| c_code = """ | |
| #include <stdio.h> | |
| int main() { | |
| printf("Hello, world!\n"); | |
| return 0; | |
| } | |
| """ | |
| tree = parser.parse(bytes(c_code, "utf8")) | |
| def traverse_tree(tree: Tree) -> Generator[Node, None, None]: | |
| cursor = tree.walk() | |
| visited_children = False | |
| while True: | |
| if not visited_children: | |
| yield cursor.node | |
| if not cursor.goto_first_child(): | |
| visited_children = True | |
| elif cursor.goto_next_sibling(): | |
| visited_children = False | |
| elif not cursor.goto_parent(): | |
| break | |
| def explain_code(tree: Tree): | |
| for node in traverse_tree(tree): | |
| node_type = node.type | |
| node_start = node.start_byte | |
| node_end = node.end_byte | |
| node_text = c_code[node_start : node_end].encode("utf-8") | |
| print(f"Type: {node_type}, Text: {node_text}") | |
| explain_code(tree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment