I am trying to parse a mathematical formula to a subset of LaTeX using ANTLR4. For example it should parse (a+4)/(b*10)
to \frac{a+4}{b\cdot 10}
.
My simple grammar creates a tree like this:
Now I am trying to implement parse tree listeners to somehow construct the LaTeX String while the tree is traversed. Here, I am failing because to construct a String like \frac{}{}
it has to be built recursively. The parse tree walker, however, visits one tree node after the other (in a breadth-first way as far as I can tell).
I've read about parse tree visitors that might be what I need. But I wasn't able to find some examples how these visitors are applied.
Could you provide an example how parse tree listeners/visitors can be used in this particular case? Do you think my approach to use ANTLR for the parser makes sense in the first place?
*Visitor
classes are properly created. – Decencies