Python - 分子式的语法检查

Python - Syntax check of molecular formulas

这将是一个很长的问题,希望大家耐心等待。

我正在编写一个程序来检查分子式的语法是否正确。

我有一个 BNF 语法:

<formel>::= <mol> \n
<mol>   ::= <group> | <group><mol>
<group> ::= <atom> |<atom><num> | (<mol>) <num>
<atom>  ::= <LETTER> | <LETTER><letter>
<LETTER>::= A | B | C | ... | Z
<letter>::= a | b | c | ... | z
<num>   ::= 2 | 3 | 4 | ...

这是我的代码:

from linkedQFile import LinkedQ
import string
import sys

ATOMER = ["H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar"]

class FormelError(Exception):
    pass
class Gruppfel(Exception):
    pass

q = LinkedQ()
formel= "(Cl)2)3"

for symbol in formel:
    q.put(symbol)


def readNum():
    """Reads digits larger than 1. Raises exception if condition is not fulfilled."""
    try:
        if int(q.peek()) >= 2:
            print(q.peek())
            q.get()
            return
        else:
            q.get()
            print("Too small digit at the end of row: "+getRest())
            sys.exit()
    except (ValueError,TypeError):
        raise FormelError("Not a number.")

def readletter():
    """Reads lowercase letters and returns them."""
    if q.peek() in string.ascii_lowercase:
        print(q.peek())
        return q.get()
    else:
        raise FormelError("Expected lowercase letter.")

def readLetter():
    """Reads capital letters and returns them."""
    if q.peek() in string.ascii_uppercase:
        print(q.peek())
        return q.get()
    else:
        raise FormelError("Expected capital letter.")

def readAtom():
    """Reads atoms on the form X and Xx. Raises Exception if the format for an atom is not fulfilled or if the atom does not exist."""
    X = ""
    try:
        X += readLetter()
    except FormelError:
        print("Missing capital letter at end of row: "+getRest())
        sys.exit()  
        return

    try:
        x = readletter()
        atom = X+x
    except (FormelError, TypeError):
        atom = X

    if atom in ATOMER:
        return
    else:
        raise FormelError("Unknown atom.")

def readGroup():
    if q.peek() in string.ascii_uppercase or q.peek() in string.ascii_lowercase:
        try:
            readAtom()
        except:
            print("Unknown atom at end of row: "+getRest())
            sys.exit()
        try:
            while True:
                readNum()
        except FormelError:
            pass
        return
    if q.peek() == "(":
        print(q.peek())
        q.get()
        try:
            readMol()
        except FormelError:
            pass
        if q.peek() == ")":
            print(q.peek())
            q.get()
        else:
            print("Missing right parenthesis at end of row: "+ getRest())
            sys.exit()
            return
        digitfound = False
        try:
            while True:
                readNum()
                digitfound = True
        except:
            if digitfound:
                return
            print("Missing digit at end of row: "+getRest())
            sys.exit()
            return
    raise FormelError("Incorrect start of group")

def readMol():
    try:
        readGroup()
    except FormelError:
        print("Incorrect start of group at end of row: "+getRest()) 
        raise FormelError
    if q.peek() == None:
        return
    if not q.peek() == ")": 
        try:
            readMol()
        except FormelError:
            pass

def readFormel():
    try:
        readMol()
    except:
        return
    print("Correct formula")

def getRest():
    rest = ""
    while not q.isEmpty():
        rest += q.get()
    return rest

readFormel()

现在代码应该接受一些给定的公式并为一些给定的不正确的公式提供错误代码。让我们看看这些给定的公式:

正确: Si(C3(COOH)2)4(H2O)7

不正确: H2O)Fe

(Cl)2)3

程序接受正确的公式,但遗憾的是也接受不正确的公式。这样做的原因是:

中的 if 语句
if not q.peek() == ")": 
    try:
        readMol()
    except FormelError:
        pass

使得右括号不平衡(右侧有一个或多个括号太多)从代码中溜走,而不是被检测为 "group" 的错误开头。我怎样才能解决这个问题,同时仍然让 Si(C3(COOH)2)4(H2O)7 被接受为语法正确?

感谢您的耐心等待:)

您的 readMol 代码对“)”进行了错误测试(您甚至告诉我们)。如果您正在编写 recursive descent parser.

代码,则您的语法没有显示需要进行此类测试

事实上,你的语法对 mol 有一个奇怪的规则:

<mol>   ::= <group> | <group><mol>

这不适用于递归下降解析器。您已重构此类规则以在每个规则中共享公共前缀。在这种情况下,很容易:

<mol>   ::= <group> ( <mol> | empty ) ;

然后你直接根据语法规则写代码(见上文link) [你有点这样做了,除了“)”检查。] 它应该看起来像这样(我不是 python 专家):

def readMol():
    try:
        readGroup()
    except FormelError:
        print("Incorrect start of group at end of row: "+getRest()) 
        raise FormelError
    try:
       readMol()
    except FormelError:
       pass

在编写递归下降解析器时,首先将语法修改为最兼容的形式很有帮助(就像我对你的 mol 规则所做的那样)。然后对单个识别器进行编码是一项很难出错的纯机械任务。