[antlr-interest] bi-directional grammar

Erel Segal erelsgl at gmail.com
Tue Sep 6 04:13:35 PDT 2011


ANTLR parsers normally work in a single direction - they get an input
document and return some information related to it (e.g. the parse tree). Is
it possible to create, using the same grammar, a generator that will work in
the opposite direction - get the information and return the original
document?

To illustrate what I mean, here is a simple grammar, with a single rule:

question returns [String issue]
    :   'What are your salary demands?' {$issue="Salary";}
    |   'What would you like as your job description?' {$issue="Job
Description";}
    |   'Do you demand a company car?' {$issue="Leased Car";}
    ;

If I create a parser from this grammar, run it on a stream that contains "What
are your salary demands?", and call its question() method, it will return
"Salary".

I would like to be able to do the opposite - send "Salary" as input, and get
"What are your salary demands?" as output, but use the same grammar for both
tasks.

Is this possible with ANTLR?


More information about the antlr-interest mailing list