1

I have written a Python code to obtain the Parse Tree of a given Java source code using ANTLR4. I wish to convert this Parse Tree to AST and following that I wish to apply tree edit distance algorithm to different ASTs. The question is, how can I convert Parse Tree obtained from ANTLR4 to AST?

If the conversion is not possible is there a way to obtain AST directly from ANTLR4?

I believe resources for using ANTLR4 with Python3 are really limited and there are not many. So any help is appreciated. Thanks in advance.

alpt
  • 61
  • 1
  • 5
  • Why can't you apply "tree edit distance" to the parse tree and get useful results? – Ira Baxter Jul 08 '19 at 08:17
  • @IraBaxter Since parse trees contain all the tokens in a source code, applying tree edit distance algorithm on a parse tree sounds like comparing each and every token in two different source codes. Applying these algorithms on ASTs is more like comparing the structures of different source codes. Correct me if I am wrong. And thanks for the advice. – alpt Jul 08 '19 at 13:49
  • Ask yourself what the essential difference would be in the results. Differencing over tokens will ignore the tokens that are identical. Differening over the absence of tokens... will ignore the tokens that are identical but have been dropped in the construction of the AST. Comparing the tokens is extra work, granted; this will one matter seriously if your edit distance algorithm is nonlinear. And frankly, ASTs don't provide any real structure that isn't already there. See my answers on the differencer between CSTs and ASTs: https://stackoverflow.com/a/1916687/120163. – Ira Baxter Jul 08 '19 at 14:54

0 Answers0