[antlr-interest] Dynamically generate grammar

Daniels, Troy (US SSA) troy.daniels at baesystems.com
Wed Apr 14 10:22:48 PDT 2010


If you will eventually have a complicated enough grammar that you want a real parser for both passes, that is possible but complicated.  You would need to write a parser for your meta-grammar, which probably uses StringTemplate to generate the grammar for your text input.  Then you would have to call the main Antlr class to build the parser, use javax.tools.JavaCompiler the compile the code and ensure that the code ended up in the classpath.  You could then create an instance of the new parser/lexer (probably with Class.forName and other reflection methods) and use that to parse the actual input.

So it is possible, but you probably do not want to do it if you can avoid it.

Troy

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org 
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Cliff Hudson
> Sent: Wednesday, April 14, 2010 4:37 AM
> To: Ламер
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Dynamically generate grammar
> 
> 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
> >
> 
> 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