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

Aaron Lebo aaron.m.lebo at gmail.com
Mon Feb 18 02:25:08 PST 2008


Thanks for the response Ana.

You are right, input is a built-in function, but even using a different
identifier doesn't make a difference. I originally was only reading 1 line
at a time like you suggested, but that doesn't handle tokens which can span
multiple lines (like say comments or strings). And being able to handle
multi-line strings is the whole reason I am in this mess. :)

Your last suggestion doesn't seem like a bad idea, but I'm not sure how that
would like in practice. Can anyone else offer some help?



On Feb 18, 2008 3:27 AM, Ana Nelson <nelson.ana at gmail.com> wrote:

> Hi, Aaron,
>
> I'm a ruby developer and have only dabbled in python. If you only need to
> read 1 line at a time without keeping track of prior inputs and if you don't
> mind the overhead of creating a new parser with each line of input then
> something like this might work:
>
> while 1:
>   line = sys.stdin.readline()
>   if not line: break
>   strm = antlr3.ANTLRStringStream(line)
>   lexer = LangLexer.LangLexer(strm)
>   tokens = antlr3.CommonTokenStream(lexer)
>   parser = LangParser.LangParser(tokens)
>   # do stuff here...
> print "finished!"
>
> Also I notice that "input" is a python function. Not sure if that might be
> causing a conflict? I see it's used in the example so it's probably okay,
> but it stands out in my syntax highlighter.
>
> I'm thinking the "right" way to do this will be to create the parser and
> lexer prior to entering the loop and somehow append each line to an
> InputStream as it's read, but maybe the above will get you started.
>
> -Ana
>
>
>
> On 18/02/2008, Aaron Lebo <aaron.m.lebo at gmail.com> 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?
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080218/4f8f12b2/attachment-0001.html 


More information about the antlr-interest mailing list