Lex文件
import ply.lex as lex
# List of token names.
tokens = (
"SYMBOL",
"COUNT"
)
t_SYMBOL = (r"Cl|Ca|Co|Os|C|H|O")
def t_COUNT(t):
r"\d+"
t.value = int(t.value)
return t
def t_error(t):
raise TypeError("Unknown text '%s'" % (t.value,))
atomLexer = lex.lex()
data1 = "CH3Cl"
data = "OClOsOH3C"
def testItOut():
# Give the lexer some input
atomLexer.input(data1)
# Tokenize
tok = atomLexer.token()
while tok:
print (tok)
tok = atomLexer.token()
解析文件
import ply.yacc as yacc
# Get the token map from the lexer.
from atomLex import tokens
def p_expression_symbol(p):
'molecule : SYMBOL'
p[0] = p[1]
def p_error(p):
raise TypeError("unknown text at %r" % (p.value,))
atomParser = yacc.yacc()
def testItOut():
# Give the parser some input
s = input('Type a chemical name > ')
# Parse it
result = atomParser.parse(s)
print ('The atom is: ' + result)
while(True):
testItOut()
目前我希望能够进入CH3Cl,虽然在我的解析文件中我不完全确定如何创建我给出的这些语法定义,
chemical : chemical molecule
chemical : molecule
molecule : SYMBOL COUNT
molecule : SYMBOL
这些语法定义在解析文件中会是什么?谢谢。
答案 0 :(得分:0)
有一套很好的PLY文档和示例,可以用来回答这个问题:http://www.dabeaz.com/ply/ply.html
第6.2节特别有用。我建议你改变这段代码:
def p_expression_symbol(p):
'molecule : SYMBOL'
p[0] = p[1]
包含新规则。名称p_expression_symbol
也不合适。我想你是从其中一个例子中复制过的。我们现在有:
def p_chemical_forumal(p):
'''molecule : SYMBOL
chemical : chemical molecule
chemical : molecule
molecule : SYMBOL COUNT
molecule : SYMBOL'''
p[0] = p[1]
文档中还有其他有用的示例可用于练习。