[antlr-interest] Lexer/Parser Reuse?

Matthew Bowman matthew.bowman at sogotech.com
Tue Sep 11 08:08:47 PDT 2007


I tried calling reset on both the lexer/parser however it seems the 
CommonTokenStream instance in between them still maintains the old input.

Example:

 String input1 = "2 + 3";
 String input2 = "4 * 5";

 MyLexer lex = new MyLexer();
 CommonTokenStream tokens = new CommonTokenStream(lex);
 MyParser parser = new MyParser(tokens);
 parser.setTreeAdaptor(new MyAstTreeAdaptor);

 lex.setCharStream(new ANTLRStringStream(input1));
 MyAst tree1  = (MyAst) parser.parseExpression().getTree();
 System.out.println(tree1.toStringTree());

 lex.setCharStream(new ANTLRStringStream(input2));
 MyAst tree2  = (MyAst) parser.parseExpression().getTree();
 System.out.println(tree2.toStringTree());

Result:

 (+ 2 3)
 (+ 2 3)

However if I do it this way it works...

Example

   /** same as above **/

 lex.setCharStream(new ANTLRStringStream(input1));
 MyAst tree1  = (MyAst) parser.parseExpression().getTree();
 System.out.println(tree1.toStringTree());

 lex.setCharStream(new ANTLRStringStream(input2));
 tokens = new CommonTokenStream(lex);
 parser.setTokenStream(tokens);
 MyAst tree2  = (MyAst) parser.parseExpression().getTree();
 System.out.println(tree2.toStringTree());

Result

 (+ 2 3)
 (* 4 5)

:(

Thanks for the reply!
--Matthew

Terence Parr wrote:
> Check for a reset method in lexer/parser (v3).  Actually, you want to 
> set the char stream / token stream.  I *think* that made it into 
> 3.0.1.  If not, I can get you 3.1b1.
> Ter
> On Sep 10, 2007, at 11:36 PM, Matthew Bowman wrote:
>
>> Greetings everybody,
>>
>> I found this thread 
>> (http://www.antlr.org/pipermail/antlr-interest/2005-August/013503.html) 
>> on the list put it didn't help me :(
>>
>> I'm trying to figure out how I can setup a "pipeline" of a Lexer --> 
>> Parser combination for a mini-scripting language I'm using ANTLR for. 
>> Basically there's an "on-demand" interpreter component that I would 
>> like to push String expressions into the Lexer and pull my nice AST 
>> out of the parser (over and over again with out recreating a new 
>> Lexer/Parser each time).
>>
>> Could anyone help me out on how I would go about doing such a thing?
>>
>> Thanks in advance,
>> --Matthew Bowman
>


-- 
Matthew Bowman
Chief Technical Officer

SoGo Technology SRL
 Traian Vuia 89
 Cluj-Napoca 400387
 Romania

Phone (ext. 642)
 +40 (318) 122364  : Romania
 +47 ( 21) 543272  : Oslo, Norway
 + 1 (404) 7956245 : Atlanta, Georgia USA
 + 1 (866) 3575982 : Toll-Free USA

Fax
 +40 (364) 402227  : Romania

Mobile
 +40 (751) 596963  : Romania




More information about the antlr-interest mailing list