-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Allow converting a ParseTree to an AST #2428
Comments
This makes no sense, really. A parse tree is a superset of an AST. You have all the info, which is contained in an AST already in your parse tree. Only the access to the token info is a tad bit more complex (you have to get it from the context's start/stop nodes). |
Try writing a compiler with just a parse tree and it'll make more sense |
You can manually convert the parse tree to AST with Visitor. BTW, Roslyn Compiler Platform uses full fidelity parse trees: https://github.com/dotnet/roslyn/wiki/Roslyn-Overview#syntax-trees. |
Roslyn isn't a compiler though, it's a set of tools for IDEs and code analysis. A parse tree is the right structure for that kind of job, and an AST is the right structure for a compiler. Sure of course it's possible to do it manually, but if it's a common use case it would make sense to abstract it out and allow the user to avoid reinventing the wheel. |
It definitely is. |
Actually, this feature-request to simplify AST construction makes a ton of sense.
Clearly, the above implies ASTs absolutely make sense for compiler construction and similar tasks,
Sorry, but this is plain wrong.
Just like the source code the parse tree was built from contains all the same info and maybe more. |
TLDR: A Roslyn syntax tree is an AST it is not a parse tree.
You sure can. Here is an arbitrary example of such a conversion: AstBuilder.java
In other words, without supporting direct AST generation, it isn't obvious what benefits Antlr brings to a task of parsing a programming language.
Sorry, but calling a Roslyn syntax tree a parse tree is factually wrong. You're correct that Roslyn produces full fidelity trees. But they are full fidelity in the sense that its possible to do a round-trip from a Roslyn syntax tree to the original source code exactly as the code was spelled by the programmer. class Program
{
public static int Main(string[] args)
{
return 0;
}
} |
The real answer to this is #2428 (comment). ANTLR 3 tried to make ASTs using the grammar, but they just ended up as partial-fidelity parse trees that took longer to create than full-fidelity ones. The whole feature was dropped from ANTLR 4 which made it simpler, faster, and more powerful. The tools already exist if a different particular representation is desired in an application. |
If anyone lands here in search of a way of converting an Antlr's parse tree into an AST, |
ANTLR tree depends on grammar. Moreover, it's possible to skip a step of parse tree building and generate AST during the parsing process. In this case listeners with disabled option |
First, thank you everyone for your work in developing and maintaining this fantastic library. I can completely understand the desire to remove features that weren't being widely used and require a great deal of work to maintain. However, the suggestion in this thread that converting a parse tree to an AST tree is easy to do by hand is less easy to understand. If someone had the time and skill, this would be an awesome feature. Consider:
Parr, Terence. Language Implementation Patterns: Create Your Own Domain-Specific and General Programming Languages (Pragmatic Programmers) (pp. 91-92). Pragmatic Bookshelf. Kindle Edition. When I read in that book about the AST support in ANTLR 3, I was really impressed by the convenience it promised, and especially the potential for reducing sensitivity to grammar changes as the language is developed. So, thank you again for your work, and thank you for leaving the issue open. I hope someone can get to it someday. Cheers. |
I agree that ANTLR should focus on one thing, parsing. Let's leave the AST to other projects, that may experiment with different approaches. Shameless plug, you may be interested in the "*LaSu" methodology that we use, and the open-source support libraries that we've written for it, that provide the building blocks for an AST and some glue code to ease the mapping from an ANTLR parse tree: https://github.com/Strumenta/StarLasu |
This comment was marked as outdated.
This comment was marked as outdated.
This is an interesting post about this |
This comment was marked as outdated.
This comment was marked as outdated.
Would like to just print out the modified tree so i can get the source code. I think generating an AST or having a minimal example on how to "print" the resulting code would be nice. |
This comment was marked as outdated.
This comment was marked as outdated.
Translated: "This is a holiday auto-reply email from QQ mailbox. Hello, I am on vacation recently and cannot reply to your email personally. I will reply to you as soon as possible after the vacation." *Incomming auto-reply mail again ..." |
I also need antlr to create an AST rather than a parse tree, so I have created two repositories working towards this. With some additional information, it should be possible to compress and modify the tree into an AST automatically. As stated, it is more efficient to create an AST instead of a parse tree if needed, but there are use cases where performance is not as important and a smaller AST is more manageable (e.g. AST to AST translation). The project is in an early stage: part1 part2 |
This comment was marked as outdated.
This comment was marked as outdated.
I made some progress in creating the tool to transform the antlr parse tree into an AST. As a result, I can now create a self-contained JavaScript version of the AST and also query/transform/print/visualize it with CSS selectors from JavaScript. |
What would be awesome and IMHO an obvious next step with ANTLR would be to give people the ability to convert from a generated ParseTree to an AST.
There are a number of ways this could be done eg:
The text was updated successfully, but these errors were encountered: