编译器学习之 语法分析器生成器LR(1) 参考后整理的C++实现代码
该代码部分是参照好友vizdl的java代码写成的,在此对好友vizdl表示感谢,没有他的帮助就没有这份代码。
最近在学习编译器中的语法分析部分.
其中的表驱动LR(1)语法分析器是一个典型的自底向上语法分析器
自底向上语法分析,寻找语法分析树的上边沿与某个产生式 A->β 右侧相匹配的子串,将字串换成产生式左侧的非终结符A,并将A作为新的上边沿。如果过是在推导过程中是在位置k处用A替换β那么(A->β,k)对是当前推导中的一个【句柄_handle】。这种替换就是规约。通过不断的规约,在成功的情况下最终将回归到语法分析树的根部(该语法分析树根在上,枝杈在下)。
代码的主要的组成部分:
第一部分:将语法存入数据结构中
第二部分:计算First表,使用closure和goto算法构建出CC,通过CC来构建语法分析表(Action表和Goto表)
第三部分:输出语法分析表
其中的几个核心算法
1.First算法//计算
2.closure算法
3.goto算法
4.构建CC的算法
输入样例:
输入括号语法:
start : <Goal>;
<Goal> ::= <List>;
<List> ::= <List><Pair>
| <Pair>;
<Pair> ::= "(" <Pair> ")"
| "("")";
#
输出:
Action表如下:
eof ( )
0 err s3 err
1 acc s3 err
2 r3 r3 err
3 err s5 s7
4 r2 r2 err
5 err s5 s9
6 err err s10
7 r5 r5 err
8 err err s11
9 err err r5
10 r4 r4 err
11 err err r4
Goto表如下:
Goal List Pair
0 err s1 s2
1 err err s4
2 err err err
3 err err s6
4 err err err
5 err err s8
6 err err err
7 err err err
8 err err err
9 err err err
10 err err err
11 err err err
输入表达式语法:
start : <Goal>;
<Goal> ::= <Expr>;
<Expr> ::= <Term><Expr'>;
<Expr'> ::= "+" <Term><Expr'>
| "-" <Term><Expr'>
| "ε";
<Term> ::= <Factor><Term'>;
<Term'> ::= "*" <Factor><Term'>
| "/" <Factor><Term'>
| "ε";
<Factor> ::= "("<Expr>")"
| "num"
| "name";
#
输出:
Action表如下:
eof + - * / ( ) num name
0 err err err err err s4 err s5 s6
1 acc err err err err err err err err
2 err s8 s9 err err err err err err
3 err err err s12 s13 err err err err
4 err err err err err s17 err s19 s20
5 r11 r11 r11 r11 r11 err err err err
6 r12 r12 r12 r12 r12 err err err err
7 r2 err err err err err err err err
8 err err err err err s4 err s5 s6
9 err err err err err s4 err s5 s6
10 r5 err err err err err err err err
11 r6 r6 r6 err err err err err err
12 err err err err err s4 err s5 s6
13 err err err err err s4 err s5 s6
14 r9 r9 r9 err err err err err err
15 err s26 s27 err err err err err err
16 err err err s30 s31 err err err err
17 err err err err err s17 err s19 s20
18 err err err err err err s34 err err
19 err r11 r11 r11 r11 err r11 err err
20 err r12 r12 r12 r12 err r12 err err
21 err s8 s9 err err err err err err
22 err s8 s9 err err err err err err
23 err err err s12 s13 err err err err
24 err err err s12 s13 err err err err
25 err err err err err err r2 err err
26 err err err err err s17 err s19 s20
27 err err err err err s17 err s19 s20
28 err err err err err err r5 err err
29 err r6 r6 err err err r6 err err
30 err err err err err s17 err s19 s20
31 err err err err err s17 err s19 s20
32 err r9 r9 err err err r9 err err
33 err err err err err err s43 err err
34 r10 r10 r10 r10 r10 err err err err
35 r3 err err err err err err err err
36 r4 err err err err err err err err
37 r7 r7 r7 err err err err err err
38 r8 r8 r8 err err err err err err
39 err s26 s27 err err err err err err
40 err s26 s27 err err err err err err
41 err err err s30 s31 err err err err
42 err err err s30 s31 err err err err
43 err r10 r10 r10 r10 err r10 err err
44 err err err err err err r3 err err
45 err err err err err err r4 err err
46 err r7 r7 err err err r7 err err
47 err r8 r8 err err err r8 err err
Goto表如下:
Goal Expr Expr' Term Term' Factor
0 err s1 err s2 err s3
1 err err err err err err
3 err err err err s11 err
4 err s18 err s15 err s16
5 err err err err err err
6 err err err err err err
7 err err err err err err
8 err err err s21 err s3
9 err err err s22 err s3
10 err err err err err err
11 err err err err err err
12 err err err err err s23
13 err err err err err s24
14 err err err err err err
15 err err s25 err err err
16 err err err err s29 err
17 err s33 err s15 err s16
18 err err err err err err
19 err err err err err err
20 err err err err err err
21 err err s35 err err err
22 err err s36 err err err
23 err err err err s37 err
24 err err err err s38 err
25 err err err err err err
26 err err err s39 err s16
27 err err err s40 err s16
代码部分:
1 #include <iostream> 2 #include <sstream> 3 #include <vector> 4 #include <string> 5 #include <set> 6 #include <map> 7 8 using namespace std; 9 10 const int MASK = 0X80000000; 11 const int DECODE = 0X7fffffff; 12 string separationCharacter = " "; 13 14 enum Type 15 { 16 LT, //< 17 RT, //> 18 SEMICOLON, //; 19 QUOTE, //" 20 OR, //| 21 COLON, //: 22 EQ //= 23 }; 24 typedef struct NTnode { 25 string name; 26 vector<vector<int>> expr; 27 }NTNode; 28 29 typedef struct Tokens { 30 bool isNt = true; 31 string name; 32 }Token; 33 34 typedef struct Nodes { 35 int state; 36 int sym; 37 int val; 38 }Node; 39 40 41 //阶段一 42 map<string, int> NTMap; 43 map<string, int> TMap; 44 vector<NTNode> production; //总式 45 vector<string> T; 46 int startIndex = 0; //默认为0 47 int point = 0; //读text的指针 48 string text; //全文 49 int textSize = 0; //全文长度 50 int counts = 0; //当前表达式号 51 string lefts; //左侧非终结符号 52 NTNode* ntn = NULL; //中介节点 53 Token token; 54 //阶段二 55 vector<int> preArr; 56 //map<int, set<int>> First; //这个虚拟数组包含了所有的非终结符FIRST 57 //set<int> *first = NULL; //中介set 58 vector<set<int>> First; 59 int eof, epsilon; 60 vector<set<string>> CC; 61 vector<vector<int>> Action; 62 vector<vector<int>> Goto; 63 set<string> res; 64 set<int>resInt; 65 66 //阶段一 67 void skip(char c); 68 void skip(Type t); 69 void addT(string name); 70 string getNT(); //获取 NT 71 string getT(); //获取 T 72 void addNT(string name); 73 void setStart(string name); 74 void initText(string s); //过滤无关字符 75 void initStartSymbol(); //获取start:<Goal>中的 Goal(非终结符) 76 void initCodeAnalyzer(); //记录每个等号左侧的非终结符 77 void notFindStartNT(); //没有找到目标(开始)非终结符 并退出程序 78 void analyze(); //将符号右侧的符号放入节点中 79 void productions(); //置于analyze()内部 80 void skipDefineSymol(); 81 void expr(); //开始读取表达式 82 void term(); //读取单个元素 83 int creatNewExper(string name);//创建一个表达式挂在name节点上 84 void notFindTWarning(string name); //错误的非终结符 85 void notFindNTWarning(string name); //错误的终结符 86 int addExpr(); //给节点添加一条表达式 87 void factor(); //通过factor()获取token 88 void addExpeElement(string name, bool isNt, string addElement); 89 int getNTSerialNumber(string name); //获取相应的数字 90 int getTSerialNumber(string name); //获取相应的数字 91 void addExprElement(int value); //将value压入表达式中 92 void addExprElement(int idx, int value);//将value压入表达式中 93 //阶段二 94 void toLRTable(); 95 void initPreArr(); //初始化 96 void FIRSTAllSymbol(); //计算所有非终结符号的FIRST集 97 void FIRST(int idx); //输入非终结符下标 98 bool isT(int val); //是否是终结符 99 void closure(set <string>* s); //closure算法 100 set<string> go(set<string> s, int x); //goto算法 101 set<int> FIRSTNextStr(vector<int> expr, int idx, int prospectiveSymbol); 102 int CCcontainsTheSet(set<string> set); 103 void createActionAndGotoTable(vector<Node> node, int endState); 104 //阶段三 105 void printActionAndGotoTable(); 106 107 108 109 int main() 110 { 111 string s; 112 string c; 113 while (true) 114 { 115 int i; 116 getline(cin, c); 117 for (i = 0; i < c.length(); i++) 118 { 119 if (c[i] != '#') 120 s += c[i]; 121 else 122 break; 123 } 124 if (i != c.length()) 125 break; 126 } 127 initText(s); //去除多余符号 128 initStartSymbol(); //获得start:<Goal>头部 129 130 addT("eof"); 131 addT("ε"); 132 eof = getTSerialNumber("eof"); 133 epsilon = getTSerialNumber("ε"); 134 135 initCodeAnalyzer(); //将其他::=左侧的头作为节点放入总容器 production 中 136 /* 137 cout << "production总容器测试 :" << endl; 138 for (int i = 0; i < production.size(); i++) 139 { 140 cout << "production[" << i << "].name = "; 141 cout << production[i].name << endl; 142 } 143 */ 144 analyze(); //将::=右侧的符号放入到容器中 145 /* 146 cout << "production输出测试 :" << endl; 147 for (int i = 0; i < production.size(); i++) 148 { 149 NTNode ntn = production[i]; 150 cout << ntn.name << endl; 151 for (int j = 0; j < ntn.expr.size(); j++) 152 { 153 for (int k = 0; k < ntn.expr[j].size(); k++) 154 { 155 cout << ntn.expr[j][k] << "\t\t"; 156 } 157 cout << endl; 158 } 159 cout << endl; 160 } 161 */ 162 toLRTable(); // 163 /* 164 cout << "toLRTable初始化 :" << endl; 165 for (int i = 0; i < preArr.size(); i++) 166 { 167 cout << preArr[i] << endl; 168 } 169 */ 170 printActionAndGotoTable(); 171 cout << "CC展开:" << endl; 172 for (int i = 0; i < CC.size(); i++) 173 { 174 cout << "CC" << i << ":" << endl; 175 for (string j : CC[i]) 176 { 177 cout << j << endl; 178 } 179 cout << endl; 180 } 181 return 0; 182 } 183 184 //阶段一 : 数据存储 185 string getNT() 186 { 187 skip(LT); //< 188 string res; 189 while (point < textSize && text[point] != '>') 190 res += text[point++]; 191 skip(RT); //> 192 return res; 193 } 194 string getT() 195 { 196 skip(QUOTE);// " 197 string res; 198 while (point < textSize && text[point] != '\"') 199 res += text[point++]; 200 skip(QUOTE); 201 return res; 202 } 203 void initStartSymbol() 204 { 205 point = 0; 206 string needle = "start:<"; 207 if (textSize <= needle.length()) 208 notFindStartNT(); 209 while (point < needle.length()) 210 { 211 if (needle[point] == text[point]) 212 point++; 213 else 214 notFindStartNT(); 215 } 216 point = needle.length(); 217 while (point < textSize && text[point] != '>') 218 point++; 219 220 setStart(string(text, needle.length(), point - needle.size())); 221 skip(RT); 222 skip(SEMICOLON); 223 } 224 void initCodeAnalyzer() 225 { 226 int idx = point; 227 point = 0; 228 counts = 0; 229 while (true) 230 { 231 while (point < textSize && text[point] != ';') 232 { 233 point++; 234 } 235 point++; 236 counts++; 237 238 if (point >= textSize) 239 break; 240 string name = getNT(); 241 addNT(name); 242 } 243 counts = 0; 244 point = idx; 245 } 246 void initText(string s)//去除多余杂质 247 { 248 int idx = 0; 249 while (idx < s.length()) 250 { 251 if (s[idx] == '\r' || s[idx] == '\n' || s[idx] == '\t' || s[idx] == ' ') 252 idx++; 253 else 254 { 255 text += s[idx++]; 256 textSize++; 257 } 258 } 259 } 260 void setStart(string name) 261 { 262 addNT(name); 263 startIndex = NTMap[name]; 264 } 265 void addNT(string name) 266 { 267 if (name.empty()) 268 { 269 cout << "非终结符不可为空" << endl; 270 exit(-1); 271 } 272 if (NTMap.count(name) == 0) 273 { 274 NTNode node; 275 node.name = name; 276 NTMap[name] = production.size(); 277 production.push_back(node); 278 } 279 } 280 void addT(string name) 281 { 282 if (TMap.count(name) == 0) 283 { 284 TMap[name] = T.size(); 285 T.push_back(name); 286 } 287 } 288 void skip(Type t) 289 { 290 switch (t) { 291 case LT: 292 skip('<'); 293 break; 294 case RT: 295 skip('>'); 296 break; 297 case OR: 298 skip('|'); 299 break; 300 case SEMICOLON: 301 skip(';'); 302 break; 303 case QUOTE: 304 skip('\"'); 305 break; 306 case COLON: 307 skip(':'); 308 break; 309 case EQ: 310 skip('='); 311 break; 312 } 313 } 314 void skip(char c) 315 { 316 if (point >= textSize || text[point] != c) 317 { 318 cout << "第" << counts << "个产生式,缺少符号 " << c << endl; 319 exit(-1); 320 } 321 point++; 322 } 323 void notFindStartNT() 324 { 325 cout << "没有找到目标非终结符号!" << endl; 326 exit(-1); 327 } 328 void analyze() 329 { 330 while (point < textSize) 331 { 332 counts++; 333 productions(); 334 } 335 } 336 void productions() 337 { 338 lefts = getNT(); // ::= 左侧头 339 skipDefineSymol(); // ::= 符号 340 expr(); 341 } 342 void skipDefineSymol() // ::= 343 { 344 skip(COLON); 345 skip(COLON); 346 skip(EQ); 347 } 348 void expr()//开始读取表达式 349 { 350 term(); 351 while (point < textSize && text[point] == '|') 352 { 353 skip(OR); // | 354 term(); 355 } 356 skip(SEMICOLON);// ; 357 } 358 void term()//读取单个元素置于节点上 359 { 360 creatNewExper(lefts);//创建一个节点 361 while (point < textSize && (text[point] == '\"' || text[point] == '<')) 362 { 363 factor(); 364 addExpeElement(lefts, token.isNt, token.name); 365 } 366 } 367 int creatNewExper(string name)//创建一个表达式挂在name节点上 368 { 369 notFindNTWarning(name); 370 ntn = &production[NTMap[name]]; 371 return addExpr(); //给节点添加一条表达式 372 } 373 void notFindNTWarning(string name) 374 { 375 if (NTMap.count(name) == 0) 376 { 377 cout << "错误的非终结符" << name << "!" << endl; 378 exit(-1); 379 } 380 } 381 void notFindTWarning(string name) 382 { 383 if (TMap.count(name) == 0) 384 { 385 cout << "错误的终结符" << name << "!" << endl; 386 exit(-1); 387 } 388 } 389 int addExpr() 390 { 391 ntn->expr.push_back(vector<int>()); 392 return ntn->expr.size() - 1; //返回这个新表达式的下标 393 } 394 void factor() 395 { 396 if (text[point] == '\"') 397 { 398 token.isNt = false; 399 token.name = getT(); 400 } 401 else 402 { 403 token.isNt = true; 404 token.name = getNT(); 405 } 406 } 407 void addExpeElement(string name, bool isNt, string addElement) 408 { 409 ntn = &production[NTMap[name]]; 410 if (isNt) 411 { 412 notFindNTWarning(name); 413 notFindNTWarning(addElement); 414 addExprElement(getNTSerialNumber(addElement)); 415 } 416 else 417 { 418 addT(addElement); 419 addExprElement(getTSerialNumber(addElement)); 420 } 421 } 422 int getNTSerialNumber(string name) 423 { 424 notFindNTWarning(name); 425 return NTMap[name]; 426 } 427 int getTSerialNumber(string name) 428 { 429 notFindTWarning(name); 430 return TMap[name] | MASK; 431 432 } 433 void addExprElement(int value) 434 { 435 addExprElement(ntn->expr.size() - 1, value); 436 } 437 void addExprElement(int idx, int value) 438 { 439 ntn->expr[idx].push_back(value); 440 } 441 442 //阶段二 : 构建表 443 bool isT(int val) 444 { 445 return ((val & MASK) == MASK); 446 } 447 void toLRTable() 448 { 449 initPreArr(); 450 FIRSTAllSymbol(); 451 /* 452 cout << "查看FIRST集合_测试" << endl; 453 for (int i = 0; i < First.size(); i++) 454 { 455 cout << "i = " << i << endl; 456 for (int j : First[i]) 457 cout <<"j = "<< j << " "; 458 cout << endl; 459 } 460 */ 461 set<string> CC0 = set<string>(); 462 vector<vector<int>> expr = production[startIndex].expr; 463 for (int i = 0; i < expr.size(); i++) 464 { 465 //cout << "insert的字符串为 = " << to_string(startIndex) + " " + to_string(i) + " " + "0" + " " + to_string(eof) << endl; 466 CC0.insert((to_string(startIndex) + " " + to_string(i) + " " + "0" + " " + to_string(eof))); 467 //CC0.insert(to_string(startIndex) + separationCharacter + to_string(i) + separationCharacter + "0" + separationCharacter + to_string(eof)); 468 } 469 closure(&CC0); 470 CC = vector<set<string>>(); 471 CC.push_back(CC0); 472 int begin = 0; 473 int lastSize = -1; 474 vector<Node> res = vector<Node>(); 475 int endState = -1; 476 while (lastSize != CC.size()) { 477 //cout << "=> 最外WHILE" << endl; 478 lastSize = CC.size(); 479 for (int i = begin; i < lastSize; i++) { 480 //cout << "=> 第一个FOR" << endl; 481 set<string> s = CC[i]; 482 for (string item : s) 483 { 484 //cout << ":::::::item = " << item << endl; 485 vector<string> strs; 486 strs.clear(); 487 string buf; 488 stringstream ss(item); 489 490 while (ss >> buf) 491 strs.push_back(buf); 492 int productionIdx = atoi(strs[0].c_str());//string 转 int 493 int exprIdx = atoi(strs[1].c_str()); 494 int placeholder = atoi(strs[2].c_str()); 495 int prospectiveSymbol = atoi(strs[3].c_str()); 496 497 vector<int>list = production[productionIdx].expr[exprIdx]; 498 if (placeholder < list.size()) 499 { 500 int x = list[placeholder]; 501 set<string> temp = go(s, x); 502 int CCj = CCcontainsTheSet(temp); 503 if (CCj == -1) 504 { 505 //cout << "CC个数加一" << endl; 506 CC.push_back(temp); 507 CCj = CC.size() - 1; 508 } 509 Node node; 510 node.state = i; 511 node.sym = x; 512 node.val = CCj; 513 res.push_back(node); 514 } 515 else 516 { 517 Node node; 518 node.state = i; 519 node.sym = prospectiveSymbol; 520 node.val = (((productionIdx - 1 >= 0 ? preArr[productionIdx - 1] : 0) + exprIdx + 1) | MASK); 521 res.push_back(node); 522 523 if (productionIdx == startIndex) 524 endState = i; 525 } 526 } 527 begin = lastSize; 528 } 529 } 530 createActionAndGotoTable(res, endState); 531 } 532 void initPreArr() 533 { 534 preArr.resize(production.size()); 535 if (preArr.size() > 0) 536 { 537 preArr[0] = production[0].expr.size(); 538 for (int i = 1; i < preArr.size(); i++) 539 { 540 preArr[i] = preArr[i - 1] + production[i].expr.size(); 541 } 542 } 543 } 544 void FIRSTAllSymbol() 545 { 546 First.resize(production.size()); 547 for (int i = First.size() - 1; i >= 0; i--) 548 { 549 FIRST(i); 550 } 551 /* 552 cout << "First :" << endl; 553 for (int i = 0; i < First.size(); i++) 554 { 555 for (int j : First[i]) 556 { 557 cout << j << " "; 558 } 559 cout << endl; 560 }*/ 561 return; 562 } 563 void FIRST(int idx) 564 { 565 if (First[idx].size() != 0) 566 return; 567 568 First[idx] = set<int>(); 569 vector<vector<int>> next = production[idx].expr; 570 //for(int i=next.size()-1;i>=0;i--) 571 for (vector<int> list : next) 572 { 573 //vector<int> list = next[i]; 574 int val = list[0]; 575 576 if (isT(val)) { 577 First[idx].insert(val); 578 } 579 else { 580 if (val == idx && First[val].size() == 0) 581 return; 582 FIRST(val); 583 for (int i : First[val]) 584 First[idx].insert(i); 585 } 586 } 587 } 588 void closure(set<string>* s) 589 { 590 int lastSize = -1; 591 592 //cout << "closure 展出:" << endl; 593 while (lastSize != s->size()) 594 { 595 lastSize = s->size(); 596 set<string> hashset = set<string>(); 597 //cout << "set.number-> " << s->size() << endl; 598 for (string item : *s) 599 { 600 vector<string> strs; 601 string buf; 602 stringstream ss(item); 603 604 while (ss >> buf) 605 strs.push_back(buf); 606 int productionIdx = atoi(strs[0].c_str());//string 转 int 607 int exprIdx = atoi(strs[1].c_str()); 608 int placehoIder = atoi(strs[2].c_str()); 609 int prospectiveSymbol = atoi(strs[3].c_str()); 610 vector<int> temp = production[productionIdx].expr[exprIdx]; 611 //cout << "item = " << item << endl; 612 if ((placehoIder < temp.size()) && (!isT(temp[placehoIder]))) 613 { 614 615 int cIdx = temp[placehoIder]; 616 //cout << "我是NT -> " << cIdx << endl; 617 618 set<int> set = FIRSTNextStr(temp, placehoIder + 1, prospectiveSymbol); 619 vector<vector<int>> expr = production[cIdx].expr; 620 for (int i = 0; i < expr.size(); i++) { 621 for (int val : set) { 622 //string res = to_string(cIdx) + separationCharacter + to_string(i) + separationCharacter + "0" + separationCharacter + to_string(val); 623 string res = to_string(cIdx) + " " + to_string(i) + " " + "0" + " " + to_string(val); 624 hashset.insert(res); 625 } 626 } 627 } 628 } 629 for (string i : hashset) 630 s->insert(i); 631 } 632 return; 633 } 634 set<string> go(set<string> s, int x) 635 { 636 res.clear(); 637 //set<string>res = set<string>(); 638 for (string item : s) 639 { 640 vector<string> strs; 641 string buf; 642 stringstream ss(item); 643 644 while (ss >> buf) 645 strs.push_back(buf); 646 int productionIdx = atoi(strs[0].c_str());//string 转 int 647 int exprIdx = atoi(strs[1].c_str()); 648 int placeholder = atoi(strs[2].c_str()); 649 int prospectiveSymbol = atoi(strs[3].c_str()); 650 651 vector<int> temp = production[productionIdx].expr[exprIdx]; 652 string str; 653 if (placeholder + 1 <= temp.size() && temp[placeholder] == x) 654 { 655 str = to_string(productionIdx) + separationCharacter + to_string(exprIdx) + separationCharacter + to_string(placeholder + 1) + separationCharacter + to_string(prospectiveSymbol); 656 res.insert(str); 657 } 658 } 659 closure(&res); 660 return res; 661 } 662 set<int> FIRSTNextStr(vector<int> expr, int idx, int prospectiveSymbol) 663 { 664 set<int> resInt = set<int>(); 665 666 if (idx >= expr.size()) 667 { 668 resInt.insert(prospectiveSymbol); 669 return resInt; 670 } 671 if (isT(expr[idx])) 672 { 673 resInt.insert(expr[idx]); 674 return resInt; 675 } 676 for (int i : First[expr[idx]]) 677 resInt.insert(i); 678 679 if (resInt.count(epsilon)) 680 { 681 resInt.erase(epsilon); 682 for (int i : FIRSTNextStr(expr, idx + 1, prospectiveSymbol)) 683 resInt.insert(i); 684 } 685 return resInt; 686 } 687 int CCcontainsTheSet(set<string> sets) 688 { 689 bool containAll = true; 690 for (int i = 0; i < CC.size(); i++) 691 { 692 set<string> s = CC[i]; 693 containAll = true; 694 for (string str : s) 695 { 696 if (sets.count(str) == 0) 697 containAll = false; 698 } 699 if ((s.size() == sets.size()) && containAll) 700 return i; 701 } 702 return -1; 703 } 704 void createActionAndGotoTable(vector<Node> node, int endState) 705 { 706 vector<int> a; 707 for (int i = 0; i < T.size(); i++) 708 a.push_back(-1); 709 vector<int> b; 710 for (int i = 0; i < production.size(); i++) 711 b.push_back(-1); 712 713 for (int i = 0; i < CC.size(); i++) 714 Action.push_back(a); 715 716 //初始化 717 /* 718 for (int i = CC.size() - 1; i >= 0; i--) 719 { 720 for (int j = T.size() - 1; j >= 0; j--) 721 { 722 Action[i][j] = -1; 723 } 724 }*/ 725 //Goto.reserve(CC.size()); 726 for (int i = 0; i < CC.size(); i++) 727 Goto.push_back(b); 728 /* 729 for (int i = CC.size()-1; i >= 0; i--) 730 { 731 for (int j = production.size()-1;j>=0; j--) 732 { 733 Goto[i][j] = -1; 734 } 735 }*/ 736 for (Node n : node) 737 { 738 if (isT(n.sym)) 739 Action[n.state][n.sym & DECODE] = n.val; 740 else 741 Goto[n.state][n.sym] = n.val; 742 } 743 Action[endState][eof & DECODE] = 0x80000000; 744 return; 745 } 746 747 748 //阶段三 : 输出 749 void printActionAndGotoTable() 750 { 751 if (Action.size() == 0 || Goto.size() == 0) 752 { 753 cout << L"表未生成, 请使用toLRTable函数生成表。" << endl; 754 return; 755 } 756 cout << "Action表如下:" << endl; 757 cout << "\t"; 758 for (int i = 0; i < T.size(); i++) 759 { 760 if (i != (epsilon & DECODE)) 761 { 762 cout << T[i] + "\t"; 763 } 764 } 765 cout << endl; 766 for (int i = 0; i < Action.size(); i++) 767 { 768 cout << to_string(i) + "\t"; 769 for (int j = 0; j < Action[i].size(); j++) 770 { 771 if (j != (epsilon & DECODE)) 772 { 773 if (Action[i][j] == -1) { 774 cout << "err\t"; 775 } 776 else if (Action[i][j] == 0x80000000) { 777 cout << "acc\t"; 778 } 779 else if ((Action[i][j] & MASK) == MASK) { 780 cout << "r" + to_string(Action[i][j] & DECODE) + "\t"; 781 } 782 else { 783 cout << "s" + to_string(Action[i][j]) + "\t"; 784 } 785 } 786 } 787 cout << endl; 788 } 789 cout << "Goto表如下:" << endl; 790 cout << "\t"; 791 for (int i = 0; i < production.size(); i++) 792 { 793 cout << production[i].name + "\t"; 794 } 795 cout << "\n"; 796 for (int i = 0; i < Goto.size(); i++) 797 { 798 cout << to_string(i) + "\t"; 799 for (int j = 0; j < Goto[i].size(); j++) 800 { 801 if (Goto[i][j] == -1) { 802 cout << "err\t"; 803 continue; 804 } 805 cout << "s" + to_string(Goto[i][j]) + "\t"; 806 } 807 cout << endl; 808 } 809 }
浙公网安备 33010602011771号