[antlr-interest] Using multiple parsers -- newbie question

biz_morland biz_morland at yahoo.com
Wed Jun 11 14:45:55 PDT 2003


Hi,

I've written a Java program that uses an ExpressionParser, 
ExpressionLexer, and ExpressionTreeWalker. I've put these three 
classes in a separate grammar file called expression.g and compiling 
and running these class doesn't produce any problems.

Now I've discovered an extra requirement which basically means I have 
to create 2 parsers, each only slightly different in the way they 
parse some text. 

So now I have to write two separate classes: ExpressionParser_1 and 
ExpressionParser_2. (The other classes, ExpressionLexer and 
ExpressionTreeWalker, should stay exactly the same.)

My question is, how does one implement two different parsers?

At first I thought, this is easy, I just have to create 2 different 
parser classes, each with a different name. Maybe even create 2 
different grammar files to hold each new parser (since I discovered a 
single grammar file cannot hold more than one parser class). 

So now:

"expression_1.g" contains: ExpressionParser_1, ExpressionLexer, 
ExpressionTreeWalker.
"expression_2.g" contains: ExpressionParser_2, ExpressionLexer, 
ExpressionTreeWalker.

And I just compile each grammar file before running my java program, 
which seems to generate each of the classes named above.

In the java program I have an "if" statement that switches between 
parsers, calling either ExpressionParser_1 or ExpressionParser_2.

But as usual things aren't as easy as they seem, and my program 
refuses to differentiate between one or the other parser. Which 
parser class actually is used, depends only on which grammar file I 
saved last!

Assuming nothing's wrong with the java program itself (and with the 
if conditional that should call the appropriate parser class), would 
anyone know why this isn't working? Or maybe even suggest a way to 
switch between 2 parsers in the same program?

Many thanks,
Greg






 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list