Rule-Based Translation

To build a translator, we have to define an input-to-output mapping (a set of “this goes to that” rules) no matter what translator architecture we use. Instead of coding them with a general-purpose programming language, we can use a translation DSL. There are a number of excellent rule-based systems available such as ASF+SDF,[40] Stratego/XT,[41] and TXL.[42] The ANTLR project also has a simple rule engine called ANTLRMorph.[43]

To use these engines, we feed them an input language grammar and a set of translation rules, as shown in the following image. From the grammar, the engine builds a parse tree from the input and then applies the rules to get the output. As we saw in Decoupling Tree Traversal from Pattern Matching ...

Get Language Implementation Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.