题目
编写程序,计算化学分子式中元素的数目,并完成以下测试:
atom_count(“He”) == 1
atom_count(“H2”) == 2
atom_count(“H2SO4”) == 7
atom_count(“CH3COOH”) == 8
atom_count(“NaCl”) == 2
atom_count(“C60H60”) == 120
参考语法
species_list : species_list species
species_list : species
species : SYMBOL
species : SYMBOL COUNT
附录
(1)元素周期表
(2)识别化学元素的正则表达式
t_SYMBOL = (
r"C[laroudsemf]?|Os?|N[eaibdpos]?|S[icernbmg]?|P[drmtboau]?|"
r"H[eofgas]?|A[lrsgutcm]|B[eraik]?|Dy|E[urs]|F[erm]?|G[aed]|"
r"I[nr]?|Kr?|L[iaur]|M[gnodt]|R[buhenaf]|T[icebmalh]|"
r"U|V|W|Xe|Yb?|Z[nr]")
(3)存储分子式的数据结构
class Atom(object):
def __init__(self, symbol, count):
self.symbol = symbol
self.count = count
def __repr__(self):
return "Atom(%r, %r)" % (self.symbol, self.count)
代码:
calclex.py
import ply.lex as lex
# 本次需要识别的只有元素和数字
tokens = (
'NUMBER',
'SYMBOL'
)
# 识别数字
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
# 识别化学元素
def t_SYMBOL(t):
r"""
C[laroudsemf]?|Os?|N[eaibdpos]?|S[icernbmg]?|P[drmtboau]?|
H[eofgas]?|A[lrsgutcm]|B[eraik]?|Dy|E[urs]|F[erm]?|G[aed]|
I[nr]?|Kr?|L[iaur]|M[gnodt]|R[buhenaf]|T[icebmalh]|
U|V|W|Xe|Yb?|Z[nr]
"""
return t
# 忽略空格
t_ignore = ' \t'
# 错误识别并提示
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
## Build the lexer
lexer = lex.lex()
yacc_example.py
#! /usr/bin/env python
# coding=utf-8
import ply.yacc as yacc
from calclex import tokens
class Atom(object):
def __init__(self, symbol, count):
self.symbol = symbol
self.count = count
def __repr__(self):
return "Atom(%r, %r)" % (self.symbol, self.count)
# 语法规则
def p_species_list_expression(p):
'species_list : species_list species'
p[0] = p[1] + p[2].count
def p_species_list_term(p):
'species_list : species'
p[0] = p[1].count
# 识别单独化学符号
def p_species_symbol(p):
'species : SYMBOL'
p[0] = Atom(p[1], 1)
# 识别带有数字的化学符号
def p_species_count(p):
'species : SYMBOL NUMBER'
p[0] = Atom(p[1], p[2])
# 错误语法识别
def p_error(p):
print("Syntax error in input!")
# Build the parser
parser = yacc.yacc()
test = ['He', 'H2', 'H2SO4', 'CH3COOH', 'NaCl', 'C60H60']
for s in test:
print(s)
result = parser.parse(s)
print(result)
结果
新手上路,有错请指正