Reusing of Parser instance

Jan 15, 2010 at 4:15 PM

Hi

Can I re-use instance of generated Parser class to call its Parse method several times?

It seems that ValueStack property can't be reset.

 

Coordinator
Jan 17, 2010 at 1:11 AM

Hi Almas.  You are correct, there is no method to re-initialize the state of the parser.  You need to create another parser object. 

It is difficult to make the Parse method reset the parser state, becuase it typically has an attached scanner and error handler which need to be reset as well.   However, I am currently considering changes for the next update which would make parser object creation even more light weight.  Some users have thread pools with many threads running an identical parsers.  This would be more efficient if these parsers could share a single parsing table.  In the current version each new parser initializes a new table.

If you really do need to restart a parse and discard the current parse state please let me know, as it is an interesting idea.  Would you want to leave the current scanner and error-handler state as they are, or would you want Parse() to do a call-back to the scanner and error-handler to reset also?

Cheers, John

Jan 18, 2010 at 6:57 AM

Hi John.

Thanks for answer and for your great application.

For my specific needs i wanted to re-initialize the same instances of parser and scanner because i parse thousands of separate sentences in the cycle. However if scanner/parser creation is a cheap operation this feature probably is unnecessary.

Apr 14, 2010 at 11:44 AM

I have similar concerns with the cost of parser creation. In my application I may need to parse as many as 5,000,000 (yes!) separate "sentences" during a performance critical batch operation, so a light-weight reset or instantiation is critical for me. Considering sharing the parser tables vs reseting the same parser instance, I think the first is preferable because it allows concurrency; multiple threads do not need to synchronize (i.e. stand in line waiting for the parser instance to be available). And probably the time to create an instance is equal to the time to create the per instance data, which must be rewritten anyway during a reset. But, like Almas, I don't really care as long as the turnaround time is fast.