1、stringusing namespace std;#define rightlength 20#define product_num 20 / 产生式最多个数#define num_noterminal 26 / 非终结符最多个数#define num_terminal 26 / 终结符最多个数struct Production char Left; char Rightrightlength; int num;struct VT bool vtnum_noterminalnum_terminal;struct Stack char P; char a;class CMyDlgpublic:
2、CMyDlg(); void InputRule(); CString showLastVT(); CString showFirstVT(); CString shownoTerminal(char G); CString showTerminal(char g); CString showLeftS(char S, int j, int k); void InitAll(); CString showSentence(CString sen, int start); CString showStack(char S, int n); void Initarry(char arry, int
3、 n); CString ProdtoCStr(Production prod); int selectProd(int i, int j, char S); void preFunctor(CString sen); void insertFirstVT(Stack S, int &sp, char P, char a); void insertLastVT(Stack S, int & void ShowPreTable(); void createPreTable(); char pretablenum_terminalnum_terminal; bool like_Q(Producti
4、on prod, char Q); void createLastVT(); bool likeQ_(Production prod, char Q); bool likeQa_(Production prod); bool like_aQ(Production prod); bool like_a(Production prod); bool likea_(Production prod); bool Dignose(char c); int findg(char c); int findG(char c); void createFirstVT(); void createTerminal
5、(); void createnoTerminal(); void buildProduction(CString s); bool test(CString s); void parse(); / 语法分析 CString gram; / 存放文法; Production productionproduct_num; VT FirstVT; VT LastVT; int locProduct; / 已有产生式个数 char Gnum_noterminal; char gnum_terminal; int i_G; int i_g; CString m_sen;FunctorFirst.cpp
6、#include FunctorFirst.hCMyDlg:CMyDlg()bool CMyDlg:test(CString s) / 测试是否是算符优先文法 bool t = 1; for (int i = 0;i 64 & si 64 & si + 1 91) t = 0; break; return t;void CMyDlg:InputRule() string infile; string line; cout infile; endl; ifstream input(infile.c_str(); if (!input) cout endl #打不开文件,请确认输入的路径有效# !
7、请再次运行本程序! exit(0); while (getline(input, line) if (test(line.c_str() = 0) cout 这不是算符优先文法! exit(0); buildProduction(line.c_str();这是算符优先文法! input.close();buildProduction(CString s) int i = 0; int j = 0; int k = 0; for (k = 0;k s.GetLength();k+) / 得到左部 if (sk != ) productionlocProduct.Left = sk; for (i
8、 = k + 1; if (si - 1 = -si = int temp = i; for (i = temp + 1; if (si !| if (si ! productionlocProduct.Rightj = si; j+; productionlocProduct.num = j; else locProduct+; productionlocProduct.Left = productionlocProduct - 1.Left; j = 0; locProduct+;createnoTerminal() / 建立非终结符索引 i_G = 0; / 最后一个位置的下一个下标 l
9、ocProduct; for (j = 0;j i_G - 1) Gi_G = productioni.Left; i_G+;createTerminal() / 建立终结符索引 i_g = 0; for (int k = 0; productioni.num;k+) char temp = productioni.Rightk; if (Dignose(temp) for (j = 0; i_g; if (temp != gj) j+; else break; if (j i_g - 1) gi_g = temp; i_g+;createFirstVT() / production已完成,创
10、建FirstVT int i, j; Stack S100; int sp = 0; for (i = 0;i+) / 初始化FirstVTj+) FirstVT.vtij = false; if (likea_(productioni) insertFirstVT(S, sp, productioni.Left, productioni.Right0); if (likeQa_(productioni) insertFirstVT(S, sp, productioni.Left, productioni.Right1); while (sp 0) sp-; char Q = Ssp.P; char a = Ssp.a; for (i =
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1