#include #include using namespace std;

string str; // 全局变量,存储输入的单词序列 int index = 0; // 全局变量,表示当前读取到的单词在序列中的下标

bool match(char c) { // 匹配函数,用于判断当前单词是否为指定的字符 if (str[index] == c) { index++; // 匹配成功,将下标加1指向下一个单词 return true; } return false; }

bool A() { // A → iBe if (match('i') && B() && match('') && match('e')) { return true; } return false; }

bool B() { // B → SB | e if (S() && B()) { return true; } else if (match('e')) { return true; } return false; }

bool S() { // S → [eC] | .i if (match('[') && match('e') && C() && match(']')) { return true; } else if (match('.') && match('i')) { return true; } return false; }

bool C() { // C → eC | e if (match('e') && C()) { return true; } else if (match('e')) { return true; } return false; }

int main() { cin >> str; // 输入单词序列 if (A() && index == str.length()) { // 如果A()函数返回true且已经读取完所有单词 cout << 'Success' << endl; // 输出“Success” } else { cout << 'Error' << endl; // 输出“Error” } return 0; }

递归下降分析程序:判断单词序列是否符合文法

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

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