Steve Ebersole
2012-12-06 18:00:22 UTC
Looking to upgrade our translators from Antlr 2 to Antlr 4 (yep coming
out of the dark ages lol). As background our translator translates
between one query language (HQL / JPQL) into another query language
(SQL) through a number of intermediate representations.
So I went ahead and bought the Antlr 4 book to help work through the
migration.
One thing in particular, however, is still causing me some concerns and
uncertainty. The book says that "writing tree parsers is unnecessary"
because Antlr4 tooling now generates listeners and visitors. Sure I can
see that when all passes simply walk the same tree. But what about the
case like I mentioned above where each pass needs to consume a different
tree?
Take an example from the book (specifically The "Starter Project" from
Chapter 3) where we look at a translator that takes a Java source file
that defines a byte[] through an array initializer; the translation
reads that and transforms it into a String:
<quote>
For example, we could transform:
static short[] data = {1,2,3};
into the following equivalent string with Unicode constants:
static String data = "\u0001\u0002\u0003"; // Java char are unsigned short
</quote>
This works in the book example because the transformation is the end
result (it simply writes the "transformed result" to System.out). But
imagine that this translation parse is part of a larger chain of
parses. So now, rather than just writing the translation result out to
stdout we instead need to "write out" a mutated tree. And lets further
imagine that instead of a simple one-kind-of-assignment to
another-kind-of-assignment transformation we instead want the resulting
tree to be structurally different and that the subsequent passes need to
walk that structurally different tree. I just don't see how do do that
without writing a tree parser. Am I missing something?
And assuming I am not missing something...
(1) Does Antlr4 still have the capability of authoring tree parsers via
grammar?
(2) Any pointers on writing such "tree transforming" parsers in Antlr4?
I have not yet read through the whole book, so I might very well just
not gotten to it yet.
Thanks,
Steve
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
out of the dark ages lol). As background our translator translates
between one query language (HQL / JPQL) into another query language
(SQL) through a number of intermediate representations.
So I went ahead and bought the Antlr 4 book to help work through the
migration.
One thing in particular, however, is still causing me some concerns and
uncertainty. The book says that "writing tree parsers is unnecessary"
because Antlr4 tooling now generates listeners and visitors. Sure I can
see that when all passes simply walk the same tree. But what about the
case like I mentioned above where each pass needs to consume a different
tree?
Take an example from the book (specifically The "Starter Project" from
Chapter 3) where we look at a translator that takes a Java source file
that defines a byte[] through an array initializer; the translation
reads that and transforms it into a String:
<quote>
For example, we could transform:
static short[] data = {1,2,3};
into the following equivalent string with Unicode constants:
static String data = "\u0001\u0002\u0003"; // Java char are unsigned short
</quote>
This works in the book example because the transformation is the end
result (it simply writes the "transformed result" to System.out). But
imagine that this translation parse is part of a larger chain of
parses. So now, rather than just writing the translation result out to
stdout we instead need to "write out" a mutated tree. And lets further
imagine that instead of a simple one-kind-of-assignment to
another-kind-of-assignment transformation we instead want the resulting
tree to be structurally different and that the subsequent passes need to
walk that structurally different tree. I just don't see how do do that
without writing a tree parser. Am I missing something?
And assuming I am not missing something...
(1) Does Antlr4 still have the capability of authoring tree parsers via
grammar?
(2) Any pointers on writing such "tree transforming" parsers in Antlr4?
I have not yet read through the whole book, so I might very well just
not gotten to it yet.
Thanks,
Steve
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address