New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to traverse the ast tree? #21
Comments
Thanks for your wonderful reply! The example is very clear how the code is structed. However, do you have any idea on how to destructualize the code? What i'm saying is for a certain line of code within a function, I want to collect the all of the components, e.g, int a = b + c, then i need to collect int,a,=,b,+,c. It confuses me how to use this parser to traverse the data structure. I'd be very appreciated if you can give me any hints, a short example code might be the best :) |
@dandelion915 As I understand it, you want to get familiar with how AST expresses a piece of code. It goes through manually creating for-loops, arrays, functions, pointers, expressions (chained or single), ... . It should provide you with the initial insight into the AST structure needed to write your own code. So for a basic
Now, as you can see,
Also, have a look at cppast. |
Thank for your speedy reply. I have seen your wonderful example.But what i'm saying is a reverse process of your example. To be specific, let's say i have an input hello-world.cpp file like this:
The purpose is to get the function body with in add() and main() using cppparser:
You see, I did debug's hardwork to traverse the data structure from root to the leaf node to get the function body(such as "a","b","Hello World\n"). It confuses me how to use this parser to automatically traverse the data structure of different phrases. Or is there any solution to stop parsing below the function level and just save all the function body as strings? I'd be very appreciated if you can give me any hints, a short example code might be the best :) |
I have been skimming through the codes for the past few days and I believe no such functionality has been implemented yet. There are some related codes in the |
Hi @satya-das, are you accepting PRs? |
Thanks for your reply. I have figured out the exposure of function body by a different thought. First using cppparser to get all the function name and return type name, then use string match algorithm to find the beginning of the function, then math the
But this temporary string-match based solution has varies problems. Like comments may also contain function name or {}, so I'm still looking for parsers to get the function body. For producers for cppparser, maybe only adding tostring function to the project can lead to my solution, but sadly there isn't one. |
By the way, are you familiar with Clang? I searched and found Clang has such abilities, but there's no tutorial document to get start... |
@dandelion915 Oh wonderful. Thank you. |
Dear @dandelion915,
This example matches only the expressions and hashInclude nodes. Here, |
Hi @salehjg !
|
Say I have two source code file1.cpp and file2.cpp written in C++, I want to know the difference code between them and locate this difference code to the function. I kind of get that this cppparser is able to get ast of the source code, but i'm still confused how to traverse the ast tree and compare two ast trees from different source code? Could anyone help me with this?
The text was updated successfully, but these errors were encountered: