以下是 Python 代码实现:

def match(token):
    global i
    if i >= len(word):
        return False
    if word[i] == token:
        i += 1
        return True
    return False

def A():
    if match('i'):
        if B():
            if match('*'):
                if match('e'):
                    return True
    return False

def B():
    if S():
        if B():
            return True
    elif match('e'):
        return True
    return False

def S():
    if match('['):
        if match('e'):
            if C():
                if match(']'):
                    return True
    elif match('.'):
        if match('i'):
            return True
    return False

def C():
    if match('e'):
        if C():
            return True
    elif match('e'):
        return True
    return False

word = input().strip()
i = 0
if A() and i == len(word):
    print('Success')
else:
    print('Error')

程序中的match函数用于匹配当前位置的单词是否与传入的参数相同,如果相同则将指针后移一位,返回True,否则返回False。

递归下降分析程序中的每一个函数对应文法中的一个产生式,函数内部按照产生式的形式进行分析,调用其他函数实现子产生式的分析,并根据当前位置的单词进行判断。如果所有的函数都调用成功并且指针已经到达单词序列的末尾,则判断为“Success”,否则判断为“Error”。


原文地址: https://www.cveoy.top/t/topic/oPPd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录