LR分析器,自下向上分析法

来源:互联网 发布:kruskal算法 编辑:程序博客网 时间:2024/05/04 07:21
#include<iostream>#include<string>#include<stack>#include<vector>#include<fstream>using namespace std;void goto_Function(string state, string symbol);void movies(string state, string symbol);//在主函数开始时,进行初始化stack<string>State;stack<string>Symbol;//输入字符串的指针int indexString = 0;//下面三个函数主要是栈的动作//执行压栈动作,将状态符,符号压入栈中void push(string state, string symbol){State.push(state);Symbol.push(symbol);}//打印栈中的状态和符号void printStack(){stack<string>temp = State;stack<string>test = Symbol;while (!temp.empty()){cout << temp.top() << " ";temp.pop();}while (!test.empty()){cout << test.top() << " ";test.pop();}}//当执行规约动作是,从栈中弹出相应的状态,和符号void popOfStack(int length){//此时弹出的数量,应该是产生式右部的长度for (int i = 0; i < length; i++){State.pop();Symbol.pop();}}/*//////////////////////////////////////////////////////////////////产生式:1:E->E+T4:T->F2:E->T5:F->(E)3:T->T*F6:F->id*/////////////////////////////////////////////////////////////////////现在是主要的动作是处理移进,规约,接受和出错的过程//移进过程中不需要goto函数,归约时才会用到goto_Function函数/*///////////////////////////////////////////////////////////////////状态                      动作movies(s,a)                    转移goto(s,a)       id      +      *       (        )        $        E        T       F0      s5                     s4 1  2       31   s6acc  2   r2     s7   r2       r23   r4     r4               r4       r44   s5  s4  8   2       35              r6     r6               r6       r66   s5                     s4                                   9       37      s5                     s4   108              s6                       s119              r1      r7               r1       r110             r3      r3               r3       r311             r5      r5               r5       r5///////////////////////////////////////////////////////////////////*///movies(s,a)void movies(string state, string symbol){if (state == "0"){if (symbol == "id"){cout << "移进" << endl;indexString += 2;State.push("5");Symbol.push("id");}if (symbol == "("){cout << "移进" << endl;indexString++;State.push("4");Symbol.push("(");}//printStack();}else if (state == "1"){if (symbol == "+"){cout << "移进" << endl;indexString++;State.push("6");Symbol.push("+");}if (symbol == "$"){cout << "接受" << endl;exit(true);}//printStack();}else if (state == "2"){if (symbol == "+" || symbol == ")" || symbol == "$"){cout << "按E->T归约" << endl;popOfStack(1);Symbol.push("E");//Symbol.push(symbol);goto_Function(State.top(),"E");}if (symbol == "*"){cout << "移进" << endl;indexString++;State.push("7");Symbol.push("*");}//printStack();}else if (state == "3"){if (symbol == "+" || symbol == "*" || symbol == ")" || symbol == "$"){cout << "按T->F归约" << endl;popOfStack(1);Symbol.push("T");//Symbol.push(symbol);goto_Function(State.top(), "T");}//printStack();}else if (state=="4"||state=="6"||state=="7"){if (symbol == "id"){cout << "移进" << endl;indexString += 2;State.push("5");Symbol.push("id");}if (symbol == "("){cout << "移进" << endl;indexString++;State.push("4");Symbol.push("(");}//printStack();}else if (state == "5"){if (symbol == "+" || symbol == "*" || symbol == ")" || symbol == "$"){cout << "按F->id归约" << endl;popOfStack(1);Symbol.push("F");//Symbol.push(symbol);goto_Function(State.top(), "F");}    //printStack();}else if (state == "8"){if (symbol == "+"){cout << "移进" << endl;indexString++;State.push("6");Symbol.push("+");}if (symbol == ")"){cout << "移进" << endl;indexString++;State.push("11");Symbol.push(")");}//printStack();}else if (state == "9"){if (symbol == "*"){cout << "移进" << endl;indexString++;State.push("7");Symbol.push("*");}if (symbol == "+" || symbol == ")" || symbol == "$"){cout << "按E->E+T归约" << endl;popOfStack(3);Symbol.push("E");//Symbol.push(symbol);goto_Function(State.top(), "E");}//printStack();}else if (state == "10"){if (symbol == "+" || symbol == "*" || symbol == ")" || symbol == "$"){cout << "按T->T*F归约" << endl;popOfStack(3);Symbol.push("T");//Symbol.push(symbol);goto_Function(State.top(), "T");}//printStack();}else if (state=="11"){if (symbol == "+" || symbol == "*" || symbol == ")" || symbol == "$"){cout << "按F->(E)归约" << endl;popOfStack(3);Symbol.push("F");//Symbol.push(symbol);goto_Function(State.top(), "F");}//printStack();}}//goto_Function()函数,当执行归于而之后,根据当前状态和符号确定哪一个状态应该进入State中void goto_Function(string state, string symbol){if (state == "0"){if (symbol == "E"){State.push("1");}else if (symbol == "T"){State.push("2");}else if (symbol=="F"){State.push("3");}}else if (state == "4"){if (symbol == "E"){State.push("8");}else if (symbol == "T"){State.push("2");}else if (symbol == "F"){State.push("3");}}else if (state == "6"){if (symbol == "T"){State.push("9");}else if (symbol == "F"){State.push("3");}}else if (state == "7"){if (symbol == "F"){State.push("10");}}}int main(int argc, char argv[]){//初始化State,SymbolState.push("$");State.push("0");Symbol.push("$");string str;cout << "输入字符串:" << endl;cin >> str;//分析输入字符串string temp;for (; indexString < str.size(); ){if (str[indexString] == 'i'){temp = "id";movies(State.top(), temp);}else if (str[indexString] == '+'){temp = "+";movies(State.top(), temp);}else if (str[indexString] == '*'){temp = "*";movies(State.top(), temp);}else if (str[indexString] == '('){temp = "(";movies(State.top(), temp);}else if (str[indexString] == ')'){temp = ")";movies(State.top(), temp);}else if (str[indexString] == '$'){temp = "$";movies(State.top(), temp);}}return 0;}

0 0
原创粉丝点击