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

Ana Nelson nelson.ana at gmail.com
Mon Feb 18 01:27:05 PST 2008


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/fa716137/attachment.html 


More information about the antlr-interest mailing list