[antlr-interest] Found bug on @header with ANTLR 3.0.1 with Python target

Benjamin Niemann pink at odahoda.de
Wed Jul 2 10:02:44 PDT 2008


Hi Cesare,

On Wed, Jul 2, 2008 at 3:10 PM, Cesare Di Mauro
<cesare.dimauro at a-tono.com> wrote:
> Thank you very much for your help.
>
> I found another bug (I hope it's a real bug this time! :)) on the @header instruction.
>
> If I put this:
>
> @header {import antlr3

Can you insert a linebreak in front of the import? I'm not sure about
the semantics from the top of my head, but it might be that it treats
the import as indented by 9 characters, so the rest of the section has
'negative' indention.

-Ben

> from BadSyntaxLexer import BadSyntaxLexer
>
> class TParser(object):
>
>  def LoadSyntax(self, TextSyntax):
>
>    self.TextSyntax = TextSyntax
>    CharStream = antlr3.FileStream(TextSyntax)
>    Lexer = BadSyntaxLexer(CharStream)
>    TokenStream = antlr3.CommonTokenStream(Lexer)
>    self.Parser = BadSyntaxParser(TokenStream)
>    self.Parser.services()}
>
> on the grammar file, the parser file will have a completely removed indentation:
>
> from antlr3 import *
> from antlr3.compat import set, frozenset
> import antlr3
> from BadSyntaxLexer import BadSyntaxLexer
>
> class TParser(object):
>
> def LoadSyntax(self, TextSyntax):
>
> self.TextSyntax = TextSyntax
> CharStream = antlr3.FileStream(TextSyntax)
> Lexer = BadSyntaxLexer(CharStream)
> TokenStream = antlr3.CommonTokenStream(Lexer)
> self.Parser = BadSyntaxParser(TokenStream)
> self.Parser.services()
>
>
> generating a Python syntax error:
>
> D:\Test>python BadSyntaxTest.py
> Traceback (most recent call last):
>  File "BadSyntaxTest.py", line 3, in <module>
>    import BadSyntaxParser
>  File "D:\Test\BadSyntaxParser.py", line 10
>    def LoadSyntax(self, TextSyntax):
>      ^
> IndentationError: expected an indented block
>
>
> ANTLR states that @header statement can be used to put imports or function / classes at the beginning of file, but for Python its useless (except for imports) if the indentation is removed.
>
> Cesare
>
> In data 02 luglio 2008 alle ore 11:44:14, Benjamin Niemann <pink at odahoda.de> ha scritto:
>
>> Hi Cesare,
>>
>> [+antlr-interest, as this might be of interest for more people]
>>
>> I don't think, this is a problem specific to Python.
>> You define INT as ('0'..'9')*, so an empty token is a valid int.
>> Technically the behavior is correct, albeit not very useful. The
>> correct solution would be to emit a warning or even error for tokens
>> that could match an empty sequence.
>>
>> The quick fix is to make use ('0'..'9')+ for INT.
>>
>> -Ben
>
> --
> Dott. Cesare Di Mauro
> A-Tono S.r.l.
> T.: (+39)095-7365314
> Information in this email is confidential and may be privileged. It is intended for the addresses only.
> If you have received it in error, please notify the sender immediately and delete it from your system. You should not otherwise copy it, retransmit it or use or disclose its content to anyone.
> Thank you for your co-operation.
>


More information about the antlr-interest mailing list