I want tree grammar C to morph its input tree to fit tree grammar Pascal, which has a different vocabulary. How do I handle the token type vocabulary issues?
Created May 3, 2012
This vocabulary issue is actually the normal case...language A with vocab A translates to language B with vocab B, and so on until the final transofrmation phase. The easiest way to handle this (until ANTLR provides vocabulary unioning operations) is to have the first grammar in the transformation define all the tokens, which are then defined for any furter phase that imports the first grammar's export vocabulary.
The normal situation looks like this:
class A extends Lexer;
...
class B extends Parser;
class C extends TreeParser; // pass one over tree
class D extends TreeParser; // pass two over tree
Since ANTLR cannot yet to vocab unions, the real solution is to simply define
token types for enhanced C vocab in C grammar:
class C extends TreeParser; // pass one over tree
tokens { // define token types for output vocab of C
Blort;
Foo;
}
Then, D will import C as usual, but D won't add any tokens as C is union of old plus stuff D needs.
Another way to handle this is to define some or all of the tokens in a supergrammar, which all phases inherit from or importVocab from.