Nat*_*enn 3 java quotes parsing escaping javacc
标记可包含转义字符的引用字符串的常用方法是什么?这里有些例子:
1) "this is good"
2) "this is\"good\""
3) "this \is good"
4) "this is bad\"
5) "this is \\"bad"
6) "this is bad
7) this is bad"
8) this is bad
Run Code Online (Sandbox Code Playgroud)
下面是一个不能正常工作的示例解析器; 除了成功解析的示例4和5之外,它具有预期的结果.
options
{
LOOKAHEAD = 3;
CHOICE_AMBIGUITY_CHECK = 2;
OTHER_AMBIGUITY_CHECK = 1;
STATIC = false;
DEBUG_PARSER = false;
DEBUG_LOOKAHEAD = false;
DEBUG_TOKEN_MANAGER = true;
ERROR_REPORTING = true;
JAVA_UNICODE_ESCAPE = false;
UNICODE_INPUT = false;
IGNORE_CASE = false;
USER_TOKEN_MANAGER = false;
USER_CHAR_STREAM = false;
BUILD_PARSER = true;
BUILD_TOKEN_MANAGER = true;
SANITY_CHECK = true;
FORCE_LA_CHECK = true;
}
PARSER_BEGIN(MyParser)
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
public class MyParser {
public static void main(String[] args) throws UnsupportedEncodingException, ParseException{
//note that this conversion to an input stream is only good for small strings
MyParser parser = new MyParser(new ByteArrayInputStream(args[0].getBytes("UTF-8")));
parser.enable_tracing();
parser.myProduction();
System.out.println("Must have worked!");
}
}
PARSER_END(MyParser)
TOKEN:
{
<QUOTED:
"\""
(
"\\" ~[] //any escaped character
| //or
~["\""] //any non-quote character
)*
"\""
>
}
void myProduction() :
{}
{
<QUOTED>
<EOF>
}
Run Code Online (Sandbox Code Playgroud)
您可以从命令行运行MyParser并使用输入进行解析.它将打印"必须有效!" 如果它工作,或如果没有,则抛出错误.
如何在示例4和5上更改此解析器以正确失败?
The*_*ell 10
要修复正则表达式,请进行修改
TOKEN: {
<QUOTED:
"\""
(
"\\" ~[] //any escaped character
| //or
~["\"","\\"] //any character except quote or backslash
)*
"\"" >
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1595 次 |
| 最近记录: |