fkling / astexplorer

A web tool to explore the ASTs generated by various parsers.
https://astexplorer.net/
MIT License
6.05k stars 711 forks source link

[ Not an Issue] - generate AST with ANTLR4 grammars #413

Open cancerberoSgx opened 5 years ago

cancerberoSgx commented 5 years ago

Your project is awesome, and I wonder if you are aware of this:

https://github.com/antlr/grammars-v4

It's a huge collection of language grammars which can generate JavaScript parsers.

I was playing a with those and made my own ast-explorer:

Playground: https://cancerberosgx.github.io/demos/univac/playground/

Project page: https://github.com/cancerberoSgx/univac

Tried to focus on languages that this project doesn't support, like Java, C++, go, ruby, smalltalk, python, erlang, dart, C, fortran, vb6, less, sass, webassembly, and others.

The big catch is that antlr4 is not designed to build ASTs but to visit nodes as they are declared in the grammar. So I needed to remove some node kinds in certain conditions in certain languages. Also the generated tree structure is defined by the grammar, and and doesn't have to match the format language AST.

Nevertheless, most of the grammars are easily portable to JS/browser so I wanted to heads up the authors of this project about this.

Also wonder if you would accept a PR with a proposal of such a AST generator, for example Java / C / C++ / python which seems to be kind of official grammars ? And if you already know about this, I wonder what's you opinion on using this approach to build ASTs ?

Thanks, keep it up!

fkling commented 4 years ago

Sorry for the long radio silence. I was on vacation and I'm currently in process to move my family across continents.

I knew about antlr, but I wasn't aware of all the grammars!

Also the generated tree structure is defined by the grammar, and and doesn't have to match the format language AST.

I guess it would be interesting to find out which languages have "official" ASTs and/or parsers. I do want to provide support for server side languages at some point. But antlr based parsers could be just as useful I guess?

KvanTTT commented 4 years ago

I think it's a good idea to integrate ANTLR grammars to astexplorer. Also, it would be great to have the possibility of changing grammars online.

The big catch is that antlr4 is not designed to build ASTs but to visit nodes as they are declared in the grammar. So I needed to remove some node kinds in certain conditions in certain languages.

I don't think this is a required step. Concrete parse trees are not bad.