【python】语法分析-化学分子式解析「编译原理」

题目

编写程序,计算化学分子式中元素的数目,并完成以下测试:

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)

结果

在这里插入图片描述


新手上路,有错请指正

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Khalil三省

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值