[antlr-interest] Dynamically generate grammar

Cliff Hudson cliff.s.hudson at gmail.com
Wed Apr 14 01:37:03 PDT 2010


For something like what you have above, it would probably be far easier to
write a simple string splitter which does what you want.  For instance, you
could use a regular expression to find where all of the ${...} parts are and
pull them out (or you could write an ANTLR lexer which does this.)  Then you
grab the remaining bits of string and place them into a list in order with
the ${} tags.  For each input line, you match the stored string against the
line starting from the current position in the line.  If you are at a tag in
your list, you hold on to it, match the next entry in the list, and then
assign all of the characters skipped between the last matched string and the
next matched string to the tag you are holding.  Repeat until the line is
consumed or you come across an error.

In your above example, your lexer would spit out:
1: <<
2: ${TITLE}
3: >>,
4: ${AUTHOR}
5: ;

Suppose your input string were:
<<My book>>, Joe Jones

The algorithm would match the first entry in the list, '<<'.  Since you
aren't holding any tag, you move the 'cursor' to the end of the <<.
You then have a tag, so you hold on to it and go the next entry.
You see '>>,' so you match that.  You grab all the text between the match
position and the last cursor position.  You assign it to the TITLE entry in
your record because that's the tag you are holding.  You then move the
cursor to the end of the '>>,'.
You see the AUTHOR tag, so you hold on to it and go to the next entry.
You see ';' so you match that.  Grab all the text between the match position
and the last cursor position.  Assign it to the AUTHOR entry in your record.
End of the line, you are done.

Easier than writing lever rules would be to just use a regex to pull the ${
... } out of the "grammar" file and split your strings that way.  But it can
be done either way.

2010/4/14 Ламер <Gogi66 at yandex.ru>

> Hi,
>
> I just started using ANTLR. I have a question about ANTLR features....
> Is is possible to generate grammar dynamically. I mean processing input in
> two steps.
>
> The goal is to retrieve data from a structured text file(different
> structures). This block will use
> grammar that will tell another parser which structure will be.
>
> E.g.
> <<${TITLE}>>, ${AUTHOR};      // it is a first grammar that tells which
> structure of input text will
>
> /*parse this grammar, then parse input text*/
>
> <<News for today>>, John Woo
>
>
> Sorry for mistakes in English,
> best regards,
> Ivan Ivanov
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list