[antlr-interest] Reading from sys.stdin with ANTLR-Python (for an REPL)

Benjamin Niemann pink at odahoda.de
Mon Feb 18 11:49:41 PST 2008


Hi Aaron,

Aaron Lebo wrote:

> Hi. I'm obviously using the Python runtime of ANTLR.
> 
> Reading from a file and parsing it is trivial. The code looks a bit like:
> 
> import antlr3
> import LangLexer
> import LangParser
> 
> input = antlr3.ANTLRFileStream('filename...')
> lexer = LangLexer.LangLexer(input)
> tokens = antlr3.CommonTokenStream(lexer)
> parser = LangParser.LangParser(tokens)
> 
> So I'm having no issue with that. What I am wanting to do is an REPL and I
> need to interactively parse the data coming from sys.stdin. The problem is
> that all of the 'Stream' classes from the antlr3 package read all input on
> creation, so passing sys.stdin or sys.stdin.read() causes the REPL to
> hang, as something like antlr3.ANTLRInputStream(sys.stdin) reads
> infinitely.
> 
> Am I making sense? What solution is there?

ANTLR out of the box is not really suited for such incremential parsing -
that's not a Python specific issue. Building an incremential TokenStream is
not a big deal - I have the code already, and I'm planning for a long time
now to clean it up and add it to the Python runtime... But the logic to
detect when a statement is complete resides in the parser and I don't know
a solution for that problem...

-Ben


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/



More information about the antlr-interest mailing list