Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带ANTLR4的字符串lexer_Java_String_Character_Antlr_Lexer - Fatal编程技术网

Java 带ANTLR4的字符串lexer

Java 带ANTLR4的字符串lexer,java,string,character,antlr,lexer,Java,String,Character,Antlr,Lexer,我在ANTLR4中定义了一些类似这样的字符串标记,但有些例外肯定在Java中得到了正确处理: STRINGLIT: '"'('\\'[bfrnt\\"]|~[\n"EOF])*'"'; ILLEGAL_ESC: '"'(('\\'[bfrnt\\"]|~[\n\\"EOF]))*('\\'(~[bfrnt\\"]|EOF)) {if (true) throw new bkool.parser.IllegalEscape(getText());}; UNCLOSED_STRING: '

我在ANTLR4中定义了一些类似这样的字符串标记,但有些例外肯定在Java中得到了正确处理:

STRINGLIT: '"'('\\'[bfrnt\\"]|~[\n"EOF])*'"';

ILLEGAL_ESC: '"'(('\\'[bfrnt\\"]|~[\n\\"EOF]))*('\\'(~[bfrnt\\"]|EOF))
    {if (true) throw new bkool.parser.IllegalEscape(getText());};

UNCLOSED_STRING: '"'('\\'[bfrnt\\"]|[\n\\"EOF])*
    {if (true) throw new bkool.parser.UncloseString(getText());};
然后我测试了一些案例,包括:

"This is a string"
"String with legal escape \\"
"Legal \\n"
"Illegal \"
"Illegal \n"
没有抛出异常。然后在其他一些情况下:

"This is a string
"String with legal escape \\"
"Legal \\n"
"Illegal \"
"Illegal \n"
然后,它以:

Unclosed string: "
通过打印带有异常名称的相应不正确字符串来处理异常


我已经和他们斗争了一天,现在我被困住了。我的ANTLR定义还有什么不好的地方?

看看一些lexer错误处理技巧(基本上是:lexing后验证)。另外,
[\n“EOF]
并不是你想的那样。
EOF
是一个单独的标记。实际上,我已经在一周前解决了这个问题,无论如何,谢谢你:现在我有另一个问题,你能帮我解决吗?