Mike75
Mike75

Reputation: 524

ANTLR Python parser error

I try to read a antlr grammar with python.

As in this question:

Umlauts in JSON files lead to errors in Python code created by ANTLR4

the error:

File "/usr/local/lib/python2.7/dist-packages/antlr4/Lexer.py", line 109,     
  in nextToken
    tokenStartMarker = self._input.mark()
AttributeError: 'str' object has no attribute 'mark'

occurs.

My grammar works fine with java and has no german umlauts.

The code is generated like this:

antlr4 -Dlanguage=Python2 -visitor MyGrammar.g4

Could the error have another reason? Maybe version problems or anything else?

Upvotes: 4

Views: 1638

Answers (1)

Anant Gupta
Anant Gupta

Reputation: 1149

The reason for getting this issue is because, you have not used antlr4.InputStream The mark function is part of the InputStream class

https://github.com/antlr/antlr4/blob/master/runtime/Python2/src/antlr4/InputStream.py

The following code works

GRAMMAR FILE

    grammar someGrammar;

    operation  : NUMBER '+' NUMBER ;

    NUMBER     : [0-9]+ ;

    WHITESPACE : ' ' -> skip ;

PYTHON CODE

    import antlr4
    from antlr4 import *
    from psqlListener import psqlListener
    from psqlLexer import psqlLexer
    from psqlParser import psqlParser
    import sys
    inputStream = antlr4.InputStream('4+5');
    lexer = psqlLexer(inputStream)
    stream = CommonTokenStream(lexer)
    parser = psqlParser(stream)

Upvotes: 5

Related Questions