LangProc / langproc-cw

Compiler coursework repository for Instruction Architectures and Compilers module at Imperial College London
18 stars 22 forks source link

AST visualisation #25

Open Fiwo735 opened 6 months ago

Fiwo735 commented 6 months ago

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:

Jpnock commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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)