题目:
考虑简单算术表达式文法G:
E→E + T | T
T→T * F | F
F→(E) | id
试设计SLR(1)或者LR(1)分析程序,以输入的 (a+b)*c+(d+e) 符号串进行语法分析。
解:
解题步骤:求增广文法->求LR(0)自动机->求Follow集->求SLR分析表
增广文法:
LR(0)自动机:
Follow集:
SLR分析表:
#include<bits/stdc++.h> using namespace std; string input_str; string input_stack[1000]; int input_top = -1;//输入栈 string sysbol_stack[1000]; int sysbol_top = -1;//符号栈 int num[100];//状态栈 int state_top = -1; map<int, map<string, pair<string, int >>>Action; map<string, int> hasp_map; map<int,map<string,int> >Goto; map<string, int> len_hash;//映射长度 pair<string,string> Equation[30];//产生式 int Equation_tot=-1; void print() { //打印输出结果 //打印状态栈 //cout << "状态栈->符号栈->剩余输入" << endl; for (int i = 0; i <= 20 || i <= state_top; i++) { if (i <= state_top) cout << num[i] << " "; else cout << " "; } //打印符号栈 for (int i = 0; i <= 30 || i <= sysbol_top; i++) { if (i <= sysbol_top) cout << sysbol_stack[i] << " "; else cout << " "; } //打印剩余输入 for (int i = input_top; i>=0; i--) { if (i>=0) cout << input_stack[i] << " "; else cout << " "; } cout << endl; } bool parse() { while (input_top != -1) { print(); //cout << "第一个:" << Action[num[state_top]][input_stack[input_top]].first << endl; if (Action[num[state_top]][input_stack[input_top]].first == "s") { //移入操作 int state = Action[num[state_top]][input_stack[input_top]].second; num[++state_top] = state;//移入状态 sysbol_stack[++sysbol_top] = input_stack[input_top];//移入符号 input_top--;//输入栈出栈 } else if (Action[num[state_top]][input_stack[input_top]].first == "r") { //归约操作 int Equation_num=Action[num[state_top]][input_stack[input_top]].second;//归约的式子 //cout << "归约的式子" << Equation[Equation_num].first <<"->" << Equation[Equation_num].second << endl; //cout << "长度:" << Equation[Equation_num].second.size() << endl; //int len = Equation[Equation_num].second.size(); int temp_len = len_hash[Equation[Equation_num].second]; sysbol_top -= temp_len;//符号栈出栈 sysbol_stack[++sysbol_top] = Equation[Equation_num].first;//放入归约后的符号 state_top-= temp_len;//状态栈出栈 int temp = state_top; num[++state_top] = Goto[num[temp]][sysbol_stack[sysbol_top]];//放入归约后的数字 } else if (Action[num[state_top]][input_stack[input_top]].first == "acc") { return 1; } else { return 0; } } return 0; } void init() { //初始化输入栈 input_stack[++input_top] = "$"; reverse(input_str.begin(), input_str.end()); for (int i = 0; i < input_str.size(); i++) { if (input_str[i] >= 'a' && input_str[i] <= 'z') input_stack[++input_top] = "id"; else input_stack[++input_top].push_back(input_str[i]); } //初始化状态栈 num[++state_top] = 0;//初始化为0 //初始化符号栈 sysbol_stack[++sysbol_top] = "$";//初始化栈顶 //初始化符号映射关系 //初始化Goto Goto[0]["E"] = 1; Goto[0]["T"] = 2; Goto[0]["F"] = 3; Goto[4]["T"] = 2; Goto[4]["F"] = 3; Goto[4]["E"] = 8; Goto[6]["F"] = 3; Goto[6]["T"] = 9; Goto[7]["F"] = 10; //初始化构造表 Action[0]["id"].first = "s"; Action[0]["id"].second = 5; Action[0]["("].first = "s"; Action[0]["("].second = 4; Action[1]["+"].first = "s"; Action[1]["+"].second = 6; Action[1]["$"].first = "acc"; Action[2]["+"].first = "r"; Action[2]["+"].second =2; Action[2][")"].first = "r"; Action[2][")"].second =2; Action[2]["$"].first = "r"; Action[2]["$"].second =2; Action[2]["*"].first = "s"; Action[2]["*"].second =7; Action[3]["+"].first = "r"; Action[3]["+"].second = 4; Action[3][")"].first = "r"; Action[3][")"].second = 4; Action[3]["$"].first = "r"; Action[3]["$"].second = 4; Action[3]["*"].first = "r"; Action[3]["*"].second = 4; Action[4]["id"].first = "s"; Action[4]["id"].second = 5; Action[4]["("].first = "s"; Action[4]["("].second = 4; Action[5]["+"].first = "r"; Action[5]["+"].second = 6; Action[5][")"].first = "r"; Action[5][")"].second = 6; Action[5]["$"].first = "r"; Action[5]["$"].second = 6; Action[5]["*"].first = "r"; Action[5]["*"].second = 6; Action[6]["id"].first = "s"; Action[6]["id"].second = 5; Action[6]["("].first = "s"; Action[6]["("].second = 4; Action[7]["id"].first = "s"; Action[7]["id"].second = 5; Action[7]["("].first = "s"; Action[7]["("].second = 4; Action[8]["+"].first = "s"; Action[8]["+"].second = 6; Action[8][")"].first = "s"; Action[8][")"].second = 11; Action[9]["+"].first = "r"; Action[9]["+"].second = 1; Action[9][")"].first = "r"; Action[9][")"].second = 1; Action[9]["$"].first = "r"; Action[9]["$"].second = 1; Action[9]["*"].first = "s"; Action[9]["*"].second = 7; Action[10]["+"].first = "r"; Action[10]["+"].second = 3; Action[10][")"].first = "r"; Action[10][")"].second = 3; Action[10]["$"].first = "r"; Action[10]["$"].second = 3; Action[10]["*"].first = "r"; Action[10]["*"].second = 3; Action[11]["+"].first = "r"; Action[11]["+"].second = 5; Action[11][")"].first = "r"; Action[11][")"].second = 5; Action[11]["$"].first = "r"; Action[11]["$"].second = 5; Action[11]["*"].first = "r"; Action[11]["*"].second = 5; //初始化产生式 Equation[++Equation_tot].first = "S", Equation[Equation_tot].second = "E"; Equation[++Equation_tot].first = "E", Equation[Equation_tot].second = "E+T"; Equation[++Equation_tot].first = "E", Equation[Equation_tot].second = "T"; Equation[++Equation_tot].first = "T", Equation[Equation_tot].second = "T*F"; Equation[++Equation_tot].first = "T", Equation[Equation_tot].second = "F"; Equation[++Equation_tot].first = "F", Equation[Equation_tot].second = "(E)"; Equation[++Equation_tot].first = "F", Equation[Equation_tot].second = "id"; //映射长度 len_hash["E+T"] = 3; len_hash["(E)"] = 3; len_hash["T*F"] = 3; len_hash["id"] = 1; len_hash["E"] = len_hash["T"]= len_hash["F"]=1; } int main() { cin >> input_str;//输入串 init(); //初始化分析表 bool ans=parse(); if (ans) { cout << "accept!" << endl; } else { cout << "wrong!" << endl; } return 0; }