ANTLR Python解析器错误

时间:2018-05-21 17:31:09

标签: python antlr4

我尝试用python读取一个antlr语法。

如同这个问题:

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

错误:

import java.util.*;
import java.lang.*;
import java.io.*;
        import java.util.regex.Matcher;
import java.util.regex.Pattern;


/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {

final String regex = "(,,)(£)?";
final String string = "18,,5 Ramsey Lane,,See,Amerighi,,samerighih@trellian.com,,£307018.48,,\n"
     + "18,,5 Ramsey Lane,,See,Amerighi,,samerighih@trellian.com,,£307018.48,,\n"
     + "18,5 Ramsey Lane,,See,Amerighi,,samerighih@trellian.com,,£307018.48,,\n"
     + "18,,5 Ramsey Lane,,See,Amerighi,,samerighih@trellian.com,,£307018.48,,";
final String subst = ",";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);

System.out.println("Substitution result: " + result);
    }
}

发生。

我的语法在java上运行良好,没有德语变音符号。

代码生成如下:

Substitution result: 18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,307018.48,
18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,307018.48,
18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,307018.48,
18,5 Ramsey Lane,See,Amerighi,samerighih@trellian.com,307018.48,

错误有其他原因吗?也许版本问题 或其他什么?

1 个答案:

答案 0 :(得分:0)

出现此问题的原因是,您尚未使用antlr4.InputStream mark函数是InputStream类的一部分

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

以下代码有效

  • 语法文件

    语法someGrammar;

    操作:NUMBER'+'NUMBER;

    NUMBER:[0-9] +;

    WHITESPACE:''->跳过;

  • PYTHON代码

    导入antlr4 从antlr4导入* 从psqlListener导入psqlListener 从psqlLexer导入psqlLexer 从psqlParser导入psqlParser 导入系统 inputStream = antlr4.InputStream('4 + 5'); lexer = psqlLexer(inputStream) 流= CommonTokenStream(lexer) parser = psqlParser(stream)