[antlr-interest] Lexer/Parser Reuse?

Loring Craymer lgcraymer at yahoo.com
Tue Sep 11 09:18:57 PDT 2007


One thing that will work is to subclass
ANTLRStringStream and add

	public void setInputTo(String input) {
		reset();
		this.data = input.toCharArray();
		this.n = input.length();		
	}
and then call this method instead of creating a new
string stream.

Ter--maybe you should add this to ANTLRStringStream? 
I freely grant permission, etc, etc.

--Loring

--- Matthew Bowman <matthew.bowman at sogotech.com>
wrote:

> 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
> 
> 
> 



       
____________________________________________________________________________________
Building a website is a piece of cake. Yahoo! Small Business gives you all the tools to get online.
http://smallbusiness.yahoo.com/webhosting 


More information about the antlr-interest mailing list