jeudi 7 mai 2020

Concrete Syntax Tree - to - Abstract Syntax Tree translation - start from root or leafs?

I am building library for translating Concrete Syntax Tree (parse tree as generated by ANTLR 4) into Abstract Syntax Tree that is made from the classes I have declared myself (https://ruslanspivak.com/lsbasi-part7/ is example, how to declare such classes and build AST in Python). My question is - what is the best practice of translation - should I start translation from the root of CST and then gradually add children nodes to the AST. Or should I go to the leafs of the CST and build AST from the leafs.

I have seen so far, that AST classes usually have strongly-typed constructors and that is why the bottom-up path (from leafs to the root) is the only way how instances of AST nodes can be constructed and combined into tree. But for me it seems to be easier to start from the root, but that requires the AST classes to have special methods that plugs children nodes into the instances of AST nodes but I have not seen such functions on the AST implementations so far.

So - what is the suggested path (top-down, bottom-up) for the building AST from the CST and why it would be convenient/good to build AST from the leafs?

Aucun commentaire:

Enregistrer un commentaire