我对词法分析和解析还不熟悉,所以我希望这是一个很容易解决的问题。我试图用Python的PLY来解析一个包含多组标记的文件:
STRING STRING QUANTITY STRING STRING # TypeA
STRING STRING STRING STRING STRING STRING # TypeB
STRING STRING QUANTITY QUANTITY QUANTITY # TypeC
每一行应该是我的程序可以理解的一种命令类型。例如,让我们调用在顶行TypeA
、第二行TypeB
中定义的类型,依此类推。因为每行应该有一个命令,所以每行末尾的NEWLINE
标记表示命令的结束。我成功地使用以下lexer成功地将文件标记化:
我想写一个解析器,它将每个匹配的命令解析成不同的对象。最后我会得到一个解析对象的列表。在
我试图构建以下规则:
def p_command(self, p):
'''command : tokens NEWLINE
| NEWLINE'''
print("found command:", list(p))
def p_tokens(self, p):
'''tokens : type_a_tokens
| type_b_tokens
| type_c_tokens'''
p[0] = p[1]
def p_type_a_tokens(self, p):
'''type_a_tokens : STRING STRING QUANTITY STRING STRING'''
p[0] = "TypeA"
def p_type_b_tokens(self, p):
'''type_b_tokens : STRING STRING STRING STRING STRING STRING'''
p[0] = "TypeB"
def p_type_c_tokens(self, p):
'''type_c_tokens : STRING STRING QUANTITY QUANTITY QUANTITY'''
p[0] = "TypeC"
我在第一个NEWLINE
之后立即为令牌得到一个SyntaxError
。不知何故,解析器在看到与p_type_a_tokens
模式匹配的模式后,不知道如何开始解析新命令。在
有人能解释一下什么是一套非常简单的解析规则吗?尽管PLY的文档通常都很好,但是到目前为止,我发现的所有示例都是针对计算器或编程语言的,在这些语言中,像换行这样的东西是不适用的。在
完整来源:
from ply import lex, yacc
class InputParser(object):
# top level tokens
tokens = [
'QUANTITY',
'STRING',
'NEWLINE'
]
t_QUANTITY = r'[+-]?(\d+\.\d*|\d*\.\d+|\d+)([eE][+-]?\d*\.?\d*|[GMkmunpf])?'
t_STRING = r'[a-zA-Z_][a-zA-Z_0-9]*'
# ignored characters
t_ignore = ' \t'
# ignore comments
t_ignore_COMMENT = r'\#.*'
def __init__(self, **kwargs):
self.lexer = lex.lex(module=self, **kwargs)
self.parser = yacc.yacc(module=self, **kwargs)
# detect new lines
def t_newline(self, t):
r'\n+'
# generate newline token
t.type = "NEWLINE"
# error handling
def t_error(self, t):
# anything that gets past the other filters
print("Illegal character '%s' on line %i at position %i" %
(t.value[0], self.lexer.lineno))
# skip forward a character
t.lexer.skip(1)
# match commands on their own lines
def p_command(self, p):
'''command : tokens NEWLINE
| NEWLINE'''
print("found command:", list(p))
p[0] = p[1]
def p_tokens(self, p):
'''tokens : type_a_tokens
| type_b_tokens
| type_c_tokens'''
p[0] = p[1]
def p_type_a_tokens(self, p):
'''type_a_tokens : STRING STRING QUANTITY STRING STRING'''
print("found type a")
p[0] = "TypeA"
def p_type_b_tokens(self, p):
'''type_b_tokens : STRING STRING STRING STRING STRING STRING'''
print("found type b")
p[0] = "TypeB"
def p_type_c_tokens(self, p):
'''type_c_tokens : STRING STRING QUANTITY QUANTITY QUANTITY'''
print("found type c")
p[0] = "TypeC"
def p_error(self, p):
if p:
error_msg = "syntax error '%s'" % p.value
else:
error_msg = "syntax error at end of file"
print(error_msg)
def parse(self, text):
self.parser.parse(text, lexer=self.lexer)
if __name__ == "__main__":
parser = InputParser()
parser.parse("""
a b 5.5 c d # TypeA
e f 1.6 g h # TypeA
i j k l m n # TypeB
# empty line
o p -1 2.0 3e4 # TypeC
""")
问题是由于第一个规则是特殊的:这是解析器开始的地方。因为上面的第一个规则不能组合两个命令(在两个相邻的行上找到),所以它失败了。在
我通过在
p_command
上方添加一个新的根规则来修复它,它可以采用单个command
(当文件只包含一个命令时)或一个命令列表(command_list
):(我还向类添加了一个
commands
字段来保存解析后的命令)这可以处理多个被“合并”在一起的命令,就像在我的输入文件中发现的那样。在
相关问题 更多 >
编程相关推荐