SLR1分析

实验目的

构造LR分析程序,利用它进行语法分析,判断给出的符号串是否为该文法识别的句子,了解LR(K)分析方法是严格的从左向右扫描,和自底向上的语法分析方法。

实验内容

对下列文法,用SLR(1)分析法对任意输入的符号串进行分析: 

(1)S->E

(2)E->E+T

(3)E->T

(4)T->T*F

(5)T->F

(6)F->(E)

(7)F->i

设计思想

(1)总控程序,也可以称为驱动程序。对所有的LR分析器总控程序都是相同的。

(2)分析表或分析函数,不同的文法分析表将不同,同一个文法采用的LR分析器不同时,分析表将不同,分析表又可以分为动作表(ACTION)和状态转换(GOTO)表两个部分,它们都可用二维数组表示。

(3)分析栈,包括文法符号栈和相应的状态栈,它们均是先进后出栈。

分析器的动作就是由栈顶状态和当前输入符号所决定。

u LR分析器由三个部分组成:

u 其中:SP为栈指针,S[i]为状态栈,X[i]为文法符号栈。状态转换表用GOTO[i,X]=j表示,规定当栈顶状态为i,遇到当前文法符号为X时应转向状态j,X为终结符或非终结符。

u ACTION[i,a]规定了栈顶状态为i时遇到输入符号a应执行。动作有四种可能:

(1)移进:

    action[i,a]= Sj:状态j移入到状态栈,把a移入到文法符号栈,其中i,j表示状态号。

(2)归约:

    action[i,a]=rk:当在栈顶形成句柄时,则归约为相应的非终结符A,即文法中有A- B的产生式,若B的长度为R(即|B|=R),则从状态栈和文法符号栈中自顶向下去掉R个符号,即栈指针SP减去R,并把A移入文法符号栈内,j=GOTO[i,A]移进状态栈,其中i为修改指针后的栈顶状态。

(3)接受acc:

    当归约到文法符号栈中只剩文法的开始符号S时,并且输入符号串已结束即当前输入符是'#',则为分析成功。

(4)报错:

当遇到状态栈顶为某一状态下出现不该遇到的文法符号时,则报错,说明输入端不是该文法能接受的符号串。

【实验要求】

1、编程时注意编程风格:空行的使用、注释的使用、缩进的使用等。

2、如果遇到错误的表达式,应输出错误提示信息。 

3、程序输入/输出实例: 

输入一以#结束的符号串(包括+—*/()i#):在此位置输入符号串 

输出过程如下:

步骤     状态栈     符号栈      剩余输入串            动 作  

 1          0             #          i+i*i#               移进 

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
 
#include<stdio.h>
 
#include<stdlib.h>
 
int Action[12][6] ={105, 0, 0, 104, 0, 0, 0, 106, 0, 0, 0, -1, 0, 52, 107, 0, 52, 52, 0, 54, 54, 0, 54, 54, 105, 0, 0, 104, 0, 0, 0, 56, 56, 0, 56, 56, 105, 0, 0, 104, 0, 0, 105, 0, 0, 104, 0, 0, 0, 106, 0, 0, 111, 0, 0, 51, 107, 0, 51, 51, 0, 53, 53, 0, 53, 53, 0, 55, 55, 0, 55, 55 };
 
int Goto[12][3] ={1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 2, 3, 0, 0, 0, 0, 9, 3, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 
char Grammar[20][10] = { '' };
 
char VT[10], VN[10];
 
char AVT[6] = { 'i''+''*''('')''#' };
 
char GVN[3] = { 'E''T''F' };
 
int vnNum, vtNum, stateNum = 12;
 
int VNum[10];
 
int grammarNum;
 
typedef struct{
 
char *base;
 
char *top;
 
}SymbolStack;
 
  
 
typedef struct{
 
int *base;
 
int *top;
 
}StateStack;
 
  
 
StateStack state;
 
SymbolStack symbol;
 
  
 
int ScanGrammar()
 
{
 
FILE *fp = fopen("D:SLR文法.txt","r");
 
FILE *tp;
 
char singleChar, nextChar;
 
int i = 0, j = 0, k, count;
 
while (!feof(fp))
 
{
 
fscanf(fp, "%c", &singleChar);
 
if (singleChar == '?')
 
{
 
Grammar[i][j] = '';
 
break;
 
}
 
if (singleChar == ' ')
 
{
 
Grammar[i][j] = '';
 
i++;
 
j = 0;
 
continue;
 
}
 
if (singleChar == '_')
 
{
 
tp = fp;
 
fscanf(tp, "%c", &nextChar);
 
if (nextChar == '>')
 
{
 
fp = tp;
 
continue;
 
}
 
}
 
if (singleChar == '|')
 
{
 
Grammar[i + 1][0] = Grammar[i][0];
 
Grammar[i][j] = '';
 
i++;
 
j = 1;
 
continue;
 
}
 
Grammar[i][j] = singleChar;
 
if (singleChar >= 'A'&&singleChar <= 'Z'){
 
count = 0;
 
while (VN[count] != singleChar&&VN[count] != '')
 
{
 
count++;
 
}
 
if (VN[count] == '')
 
{
 
vnNum = count + 1;
 
if (singleChar == 'S')
 
{
 
j++;
 
continue;
 
}
 
VN[count] = singleChar;
 
vnNum = count + 1;
 
}
 
}
 
else
 
{
 
count = 0;
 
while (VT[count] != singleChar&&VT[count] != '')
 
{
 
count++;
 
}
 
if (VT[count] == '')
 
{
 
VT[count] = singleChar;
 
vtNum = count + 1;
 
}
 
}
 
j++;
 
}
 
printf("输入的文法: ");
 
for (k = 0; k <= i; k++)
 
{
 
j = 0;
 
while (Grammar[k][j] != '')
 
{
 
if (j == 1)
 
{
 
printf("_>");
 
}
 
printf("%c", Grammar[k][j]);
 
j++;
 
}
 
printf(" ");
 
}
 
count = 0;
 
printf("VT:");
 
while (VT[count] != '')
 
{
 
printf("%3c", VT[count]);
 
count++;
 
}
 
VT[count] = '#';
 
vtNum = count + 1;
 
printf("%3c", VT[count]);
 
printf(" VN:");
 
count = 0;
 
while (VN[count] != '')
 
{
 
printf("%3c", VN[count]);
 
count++;
 
}
 
printf(" ");
 
// printf(" %d %d ", vtNum, vnNum);
 
fclose(fp);
 
grammarNum = i + 1;
 
return i;
 
}
 
  
 
int vNumCount()
 
{
 
int i,j;
 
for (i = 0; i < grammarNum; i++)
 
{
 
j = 1;
 
while (Grammar[i][j] != '')
 
{
 
j++;
 
}
 
VNum[i]=j;
 
//printf("%3d", VNum[i]);
 
}
 
printf(" ");
 
return 0;
 
}
 
  
 
void InitStack()
 
{
 
state.base = (int *)malloc(100 * sizeof(int));
 
if (!state.base)exit(1);
 
state.top = state.base;
 
*state.top = 0;
 
symbol.base = (char*)malloc(100 * sizeof(char));
 
if (!symbol.base)exit(1);
 
symbol.top = symbol.base;
 
*symbol.top = '#';
 
}
 
  
 
int Judge(int stateTop, char inputChar)
 
{
 
  
 
    int i,j;
 
for (i = 0; i < stateNum; i++){
 
if (stateTop == i)break;
 
}
 
for (j = 0; j < vtNum; j++){
 
if (inputChar == AVT[j])break;
 
}
 
    return Action[i][i];
 
}
 
  
 
int GetGoto(int stateTop, char inputChar){
 
int i, j;
 
for (i = 0; i < stateNum; i++){
 
if (stateTop == i) break;
 
}
 
for (j = 0; j < vnNum; j++)
 
{
 
if (inputChar == GVN[j]) break;
 
}
 
return Goto[i][j];
 
}
 
  
 
int print(int count, int i, char Input[], int action, int gt, int sign)
 
{
 
int *p = state.base, stateNum;
 
int j, jj;
 
char *q = symbol.base, symbolNum;
 
printf("%d ", count);
 
while (p != state.top + 1)
 
{
 
stateNum = *p;
 
printf("%d", stateNum);
 
p++;
 
}
 
printf(" ");
 
while (q != symbol.top + 1)
 
{
 
symbolNum = *q;
 
printf("%c", symbolNum);
 
q++;
 
}
 
printf(" ");
 
j = i;
 
jj = 0;
 
while (jj < j)
 
{
 
printf(" ");
 
jj++;
 
}
 
while (Input[j] != '')
 
{
 
printf("%c", Input[j]);
 
j++;
 
}
 
printf(" ");
 
if (sign == 1)
 
{
 
printf(" S%d %d ", action, gt);
 
}
 
if (sign == 2)
 
{
 
printf(" r%d %d ", action, gt);
 
}
 
if (sign == 3)
 
{
 
printf("tacc %d ", gt);
 
}
 
if (sign == 0) printf(" 0 0 ");
 
return 0;
 
}
 
  
 
int Pop(int action)
 
{
 
int *p, stateNumn, ssValue, i;
 
state.top--;
 
p = state.top;
 
stateNum = *p;
 
i = VNum[action] - 1;
 
while (i != 0)
 
{
 
symbol.top--;
 
i--;
 
}
 
symbol.top++;
 
*symbol.top = Grammar[action][0];
 
ssValue=GetGoto(stateNum,Grammar[action][0]);
 
if (ssValue == 0)return ssValue;
 
state.top++;
 
*state.top = ssValue;
 
return ssValue;
 
}
 
  
 
int Reduction()
 
{
 
char Input[20];
 
int i = 0,count = 1;
 
int ssValue, action;
 
int stateTop, gt;
 
int sign = -1; // 彩进1, 规约2, 接受3
 
scanf("%s", &Input);
 
while (Input[i] != '')
 
{
 
if (Input[i] >= 'A'&&Input[i] <= 'Z')
 
{
 
printf("输入的不是有效的表达式!");
 
return 0;
 
}
 
i++;
 
}
 
i = 0;
 
printf("步骤 状态栈 符号栈 输入串 ACTION GOTO ");
 
while (Input[i] != '')
 
{
 
if (count == 1)
 
{
 
print(count, i, Input, 0, 0, 0);
 
count++;
 
}
 
stateTop = *state.top;
 
ssValue = Judge(stateTop, Input[i]);
 
if (ssValue == 0)
 
{
 
state.top--;
 
if (*symbol.top == '#')
 
{
 
printf("规约出错!");
 
return 0;
 
}
 
continue;
 
}
 
if (ssValue == -1)
 
{
 
sign = 3;
 
print(count, i, Input, ssValue, 0, sign);
 
count++;
 
return 1;
 
}
 
if (ssValue >= 100)
 
{
 
sign = 1;
 
action = ssValue - 100;
 
state.top++;
 
*state.top = action;
 
symbol.top++;
 
*symbol.top = Input[i];
 
i++;
 
print(count, i, Input, action, 0, sign);
 
count++;
 
}
 
if (ssValue >= 50 && ssValue < 100)
 
{
 
sign = 2;
 
action = ssValue - 50;
 
gt = Pop(action);
 
print(count, i, Input, action, gt, sign);
 
count++;
 
}
 
}
 
return 0;
 
}
 
  
 
int main()
 
{
 
ScanGrammar();
 
vNumCount();
 
InitStack();
 
Reduction();
 
return 0;
 
}

  

截图

 

原文地址:https://www.cnblogs.com/mxk123456/p/14208189.html