[antlr-interest] - Python Target
Benjamin Niemann
pink at odahoda.de
Sun Jul 15 14:17:30 PDT 2007
Edward Hartley wrote:
>> I'm looking at the example grammar Expr.g from the book and when testing
>> it with valid expressions like (3+4)*9 I get the output like the
>> following
>>>>> (3+4)*9
>> line 0:-1 mismatched input '<EOF>' expecting NEWLINE
>>
>> Is this behaviour correct or is it due to my having missed something:
>> I retained the skip() call in the NEWLINE rule.
>> I don not get this behavior with the Java target and see the same as is
>> described in the book.
>
> I can't reproduce this. Are you sure that the input actually has a '\n' at
> the end? How does your main script look like?
>
>
>
> My Driver program looks like this:
>
> import antlr3
> from ExprLexer import ExprLexer
> from ExprParser import ExprParser
>
> while True:
> expr = raw_input('>>> ')
> if expr == "'':
> break
> cStream = antlr3.StringStream(expr)
> lexer = ExprLexer(cStream)
> tStream = antlr3.CommonTokenStream(lexer)
> parser = ExprParser(tStream)
> parser.prog()
>
> I'm a bit mystified and expect I've missed something blindingly obvious.
The result of raw_input() does not include the '\n'. But in this grammar,
every line must be terminated by NEWLINE.
Either change the grammar (with raw_input() you are not able to read
multiline input anyway) or add a '\n' to the input before you feed it into
ANTLR.
--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
More information about the antlr-interest
mailing list