Skip to content
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

AST visualisation #25

Open
Fiwo735 opened this issue Mar 16, 2024 · 3 comments
Open

AST visualisation #25

Fiwo735 opened this issue Mar 16, 2024 · 3 comments
Assignees

Comments

@Fiwo735
Copy link
Collaborator

Fiwo735 commented Mar 16, 2024

I've started working on an AST visualisation in the form of a tree graph. Before I commit to the development, I wanted to get your feedback about the approach below, especially if you have some experience with similar tasks:

  1. Create parser_visualizer.y which has the same grammar as parser_full.y and each rule builds a generic tree composed of TreeNode (declared in ast_node.hpp, it would have no other information, but name and children).
  2. Write a C++ driver that parses input program using parser_visualizer.y and then walks the tree and outputs a very simple .dot file.
  3. Call Graphviz CLI (or similar) to create a graph, using in-built arguments to make it look nice.

I've also considered and found issues with other approaches/aspects:

  • Walk the already existing AST (parsed with ParseAST in compiler.cpp): no easy way to walk since we removed the generic branches_, hence each node would require overriding a base virtual function like Walk in order to handle all named children.
  • Put the C++ visualiser logic in compiler.cpp: the logic might be non-trivial, adding to the existing complexity of understanding that file and making it look scarier + separation is usually nice.
  • Use C++ Graphviz module to manipulate the graphs directly in C++: looks less portable if we ever wanted to change the graphing + more cumbersome to implement it (Graphviz CLI seems easier to use).
  • Use GCC/Clang to generate .dot files: their AST is different to ours + it'd be hard to match the C version + that option is not very well documented/developped (to my surprise, but maybe that's due to insufficient googling).
@Fiwo735 Fiwo735 self-assigned this Mar 16, 2024
@Jpnock
Copy link
Collaborator

Jpnock commented Mar 18, 2024

How about if this is done external to the compiler coursework repo and instead a contribution is made to something like https://github.com/fkling/astexplorer (see example here: https://astexplorer.net/)?

@Fiwo735
Copy link
Collaborator Author

Fiwo735 commented Mar 19, 2024

That's a good point - after creating this Issue I actually thought about creating a standalone repo to avoid polutting this one. Your point of contributing to AST explorer is interesting as I've been always suprised that they don't offer C/C++. However, that also might be a problem, as there certainly is a very good reason why they don't offer support for one of the most popular languages.

Aside from contributing C to AST explorer being technically challenging, it also would be quite specific as we only care about the limited subset of C90, which I guess wouldn't be very popular outside students working on this coursework.

Hence, I'd say it might be more feasible (and "thematically" correct) to develop the visualiser in this repo/standalone repo under LangProc, depending on the code size - i.e., finding the correct balance of main repo poluttion vs ease of use if it was provided as one package.

@Jpnock
Copy link
Collaborator

Jpnock commented Mar 25, 2024

Even if it wasn't accepted upstream, we could still consider our own fork for C90 specifically

Maybe have a look at these to see if they could be useful: https://github.com/zaach/jison / https://github.com/ericprud/ts-jison (looks like they can be used to generate a parser in javascript / typescript, then it should be possible to walk the AST and output whatever structure astexplorer requires, as documented here: https://github.com/fkling/astexplorer?tab=readme-ov-file#how-to-add-a-new-parser)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants