Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/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
Python 2.7 使用PLY匹配正常字符串_Python 2.7_Lex_Lexer_Ply - Fatal编程技术网

Python 2.7 使用PLY匹配正常字符串

Python 2.7 使用PLY匹配正常字符串,python-2.7,lex,lexer,ply,Python 2.7,Lex,Lexer,Ply,我正在使用编写解析器。这个问题和这个类似。但是,我使用双引号打开和关闭字符串。例如: “我不知道A是什么” 我将普通字符串lexer定义为: t_NORMSTRING = r'"([^"\n]|(\\"))*"$' 我还有一个变量的lexer: def t_VAR(t): r'[a-zA-Z_][a-zA-Z_0-9]*' 问题是我的lexer无法将“我不知道”A“是什么”识别为NORMSTRING标记。它返回错误 Illegal character '"' at 1 Syntax e

我正在使用编写解析器。这个问题和这个类似。但是,我使用双引号打开和关闭字符串。例如:

“我不知道A是什么”

我将普通字符串lexer定义为:

t_NORMSTRING = r'"([^"\n]|(\\"))*"$'
我还有一个变量的lexer:

def t_VAR(t):
   r'[a-zA-Z_][a-zA-Z_0-9]*'
问题是我的lexer无法将“我不知道”A“是什么”识别为NORMSTRING标记。它返回错误

Illegal character '"' at 1
Syntax error at 'LexToken(VAR,'do',10,210)'

请告诉我为什么不正确。

通过一个小PLY程序探讨了这个问题,我认为您的问题与数据处理方面的差异有关,而不是PLY解析和词汇匹配本身。(作为旁注,两者在字符串处理方面有一些细微的区别。我将我的代码限制为python v2)

只有在使用非原始字符串或使用
input
而不是
raw\u input
时,您才会看到错误。下面是我的示例代码和结果:

命令:

$ python --version
Python 2.7.5
$ python string.py
输出:

Data: '"I do not know what \"A\" is"'
String: '"I do not know what \"A\" is"'
Data: '"I do not know what "A" is"'
Illegal character '"'
Illegal character '"'
String: '" is"'
Please type your line: "I do not know what \"A\" is"
String: '"I do not know what \"A\" is"'
Please type your line: "I do not know what \"A\" is"
Illegal character '"'
Illegal character '"'
输出:

Data: '"I do not know what \"A\" is"'
String: '"I do not know what \"A\" is"'
Data: '"I do not know what "A" is"'
Illegal character '"'
Illegal character '"'
String: '" is"'
Please type your line: "I do not know what \"A\" is"
String: '"I do not know what \"A\" is"'
Please type your line: "I do not know what \"A\" is"
Illegal character '"'
Illegal character '"'
输出:

Data: '"I do not know what \"A\" is"'
String: '"I do not know what \"A\" is"'
Data: '"I do not know what "A" is"'
Illegal character '"'
Illegal character '"'
String: '" is"'
Please type your line: "I do not know what \"A\" is"
String: '"I do not know what \"A\" is"'
Please type your line: "I do not know what \"A\" is"
Illegal character '"'
Illegal character '"'
输出:

Data: '"I do not know what \"A\" is"'
String: '"I do not know what \"A\" is"'
Data: '"I do not know what "A" is"'
Illegal character '"'
Illegal character '"'
String: '" is"'
Please type your line: "I do not know what \"A\" is"
String: '"I do not know what \"A\" is"'
Please type your line: "I do not know what \"A\" is"
Illegal character '"'
Illegal character '"'
最后,在正则表达式中可能不需要字符串锚点
$