简体   繁体   English

缺少使用Python目标的ANTLR4中的Lexer方法?

[英]Missing Lexer Methods in ANTLR4 with Python Target?

I am trying to use ANTLR4 with Python2 Target. 我正在尝试将ANTLR4与Python2 Target一起使用。 As I understand it, semantic predicates of the following form are code from the target language embedded in the grammar. 据我了解,以下形式的语义谓词是嵌入语法中的目标语言的代码。 For example, I have one such rule: 例如,我有一个这样的规则:

'S' {getCharPositionInLine() == 1}? 'OMETHING'

This basically is to ensure that the token SOMETHING begins at the start of the line (otherwise it is something else). 这基本上是为了确保令牌SOMETHING在行的开头开始(否则它是其他的东西)。 I used the Java target (default), because its testing facilities are better than in Python. 我使用了Java目标(默认),因为它的测试工具比Python更好。 So far so good. 到现在为止还挺好。 However once my grammar was working, upon trying to translate this code to Python, I come across a problem. 但是,一旦我的语法工作,在尝试将此代码转换为Python时,我遇到了一个问题。 Firstly I assume that the above code in Java (I don't know much Java at all) is the calling of the class method getCharPositionInLine , so naturally it seems reasonable to translate the above to: 首先我假设Java中的上述代码(我根本不太了解Java)是调用类方法getCharPositionInLine ,所以将上面的内容翻译成:

'S' {self.getCharPositionInLine() == 1}? 'OMETHING'

However I ultimately get the error: 但是我最终得到了错误:

'MyLexer' object has no attribute 'getCharPositionInLine'

And I go and check the Lexer.py files (which MyLexer inherits from) in both antlr4/runtime/Python2/ and antlr4/runtime/Python3/ , and lo and behold, there is no such method. 然后我去检查antlr4/runtime/Python2/antlr4/runtime/Python3/Lexer.py文件(MyLexer继承的文件),并且看,没有这样的方法。 I didn't realise certain targets were missing features. 我没有意识到某些目标缺少功能。 Having known nothing about parsers and lexers before this I was very proud of my grammar but after all this I might not even be able to use it! 在此之前对解析器和词法分析器一无所知我对我的语法感到非常自豪,但毕竟我甚至无法使用它! Is there any way I can work around this? 有什么方法可以解决这个问题吗?

As I understand python runtimes use column property instead of getCharPositionInLine . 据我所知,python运行时使用column属性而不是getCharPositionInLine See also: https://github.com/antlr/antlr4/blob/4.6/runtime/Python2/src/antlr4/Lexer.py#L237 另见: https//github.com/antlr/antlr4/blob/4.6/runtime/Python2/src/antlr4/Lexer.py#L237

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM