A bit late, but googling pyparsing reentrancy shows this topic, so my answer is.
I solved the problem with reusing / reusing an instance of the parser by attaching a context to the parsed string. You are a subclass of str , put your context in the attribute of the new class str, pass its pyparsing instance pyparsing and pyparsing context back into action.
Python 2.7:
from pyparsing import LineStart, LineEnd, Word, alphas, Optional, Regex, Keyword, OneOrMore
gives
['#include', 'included_doc', '\n', 'ham', '=', 'None', '\n', 'spam', '=', 'ham'] {'ham': 'None', 'parrot': 'dead', 'spam': 'ham'}
This approach makes Parser itself completely reusable and repetitive. pyparsing internals are usually reentrant unless you touch the static fields of ParserElement . The only drawback is that pyparsing flushes its packrat cache each time parseString called, but this can be resolved by overriding SpecStr.__hash__ (to make it hashed, for example, object , not str ) and some monkeypatching option. In my dataset, this is not a problem at all, since the performance hit is negligible, and this even contributes to memory usage.
source share