[antlr-interest] bi-directional grammar

The Researcher researcher0x00 at gmail.com
Tue Sep 6 05:50:15 PDT 2011


On Tue, Sep 6, 2011 at 7:13 AM, Erel Segal <erelsgl at gmail.com> wrote:

> 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?
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
I don't know how this could be done in ANTLR, but PROLOG should work.

Eric


More information about the antlr-interest mailing list