@@ -18,7 +18,7 @@ \section{Восходящий синтаксический анализ}
18
18
19
19
\begin {center }
20
20
\begin {tabular }{c||c|c|c|c|c||c|c|c|c}
21
- States & $ t_0 $ &$ \dots $ & $ t_a$ & $ \dots $ & \$ & $ N_0 $ &$ \dots $ & $ N_b$ & $ \dots $ \\ \hline
21
+ States & $ t_0 $ &$ \dots $ & $ t_a$ & $ \dots $ & \$ & $ N_0 $ &$ \dots $ & $ N_b$ & $ \dots $ \\ \hline \hline
22
22
$ \dots $ & $ \dots $ &$ \dots $ & $ \dots $ & $ \dots $ & $ \dots $ & $ \dots $ &$ \dots $ & $ \dots $ & $ \dots $ \\ \hline
23
23
$ 10 $ & $ \dots $ &$ \dots $ & $ s_i$ & $ \dots $ & $ r_k$ & $ \dots $ &$ \dots $ & $ j$ & $ \dots $ \\ \hline
24
24
$ \dots $ & $ \dots $ &$ \dots $ & $ \dots $ & $ \dots $ & $ acc$ & $ \dots $ &$ \dots $ & $ \dots $ & $ \dots $
@@ -211,16 +211,16 @@ \subsection{Примеры}
211
211
\begin {example }
212
212
Пример управляющей LR(0) таблицы.
213
213
214
- \begin {tabular }{|c| c|c|c||c| }
215
- \hline & a & b & \$ & S \\ [0.5ex]
214
+ \begin {tabular }{c|| c|c|c||c}
215
+ & a & b & \$ & S \\ \hline
216
216
\hline 0 & $ s_3 $ , $ r_1 $ & $ r_1 $ & $ r_1 $ & 1 \\
217
217
\hline 1 & & & acc & \\
218
218
\hline 2 & $ r_2 $ & $ r_2 $ & $ r_2 $ & \\
219
219
\hline 3 & $ s_3 $ , $ r_1 $ & $ r_1 $ & $ r_1 $ & 4 \\
220
220
\hline 4 & & $ s_5 $ & & \\
221
221
\hline 5 & $ s_3 , r_1 $ & $ r_1 $ & $ r_1 $ & 6 \\
222
- \hline 6 & $ r_0 $ & $ r_0 $ & $ r_0 $ & \\ [1ex]
223
- \hline
222
+ \hline 6 & $ r_0 $ & $ r_0 $ & $ r_0 $ &
223
+
224
224
\end {tabular }
225
225
226
226
Как видим, в данном случае в таблице присутствуют shift-reduce конфликты. В случае, когда не удаётся построить таблицу без конфликтов, говорят, что грамматика не LR(0).
@@ -236,16 +236,15 @@ \subsection{Примеры}
236
236
\textit {FOLLOW}_1 (S) = \{ b, \$\}
237
237
$$
238
238
239
- \begin {tabular }{|c| c|c|c||c| }
240
- \hline & a & b & \$ & S \\ [0.5ex]
239
+ \begin {tabular }{c|| c|c|c||c}
240
+ & a & b & \$ & S \\ \hline
241
241
\hline 0 & $ s_3 $ & $ r_1 $ & $ r_1 $ & 1 \\
242
242
\hline 1 & & & acc & \\
243
243
\hline 2 & & & & \\
244
244
\hline 3 & $ s_3 $ & $ r_1 $ & $ r_1 $ & 4 \\
245
245
\hline 4 & & $ s_5 $ & & \\
246
246
\hline 5 & $ s_3 $ & $ r_1 $ & $ r_1 $ & 6 \\
247
247
\hline 6 & & $ r_0 $ & $ r_0 $ & \\ [1ex]
248
- \hline
249
248
\end {tabular }
250
249
251
250
В данном случае в таблице отсутствуют shift-reduce конфликты. То есть наша грамматика SLR(1), но не LR(0).
@@ -421,138 +420,22 @@ \subsection{Классический GLR алгоритм}
421
420
422
421
Построим для данной грамматики LR автомат:
423
422
424
- \begin {tikzpicture }[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
425
- % uncomment if require: \path (0,306); %set diagram left start at 0, and has height of 306
426
-
427
-
428
- % Text Node
429
- \draw (21.5,33) .. controls (21.5,30.24) and (23.74,28) .. (26.5,28) -- (94.5,28) .. controls (97.26,28) and (99.5,30.24) .. (99.5,33) -- (99.5,89) .. controls (99.5,91.76) and (97.26,94) .. (94.5,94) -- (26.5,94) .. controls (23.74,94) and (21.5,91.76) .. (21.5,89) -- cycle ;
430
- \draw (60.5,61) node [color={rgb, 255:red, 62; green, 45; blue, 45 } ,opacity=1 ] {$ \begin {array}{l}
431
- S'\rightarrow .S\$ \\
432
- S\rightarrow .abC\\
433
- S\rightarrow .aBC
434
- \end {array}$ };
435
- % Text Node
436
- \draw (25,141) .. controls (25,138.24) and (27.24,136) .. (30,136) -- (91,136) .. controls (93.76,136) and (96,138.24) .. (96,141) -- (96,156) .. controls (96,158.76) and (93.76,161) .. (91,161) -- (30,161) .. controls (27.24,161) and (25,158.76) .. (25,156) -- cycle ;
437
- \draw (60.5,148.5) node {$ S'\rightarrow S.\$ $ };
438
- % Text Node
439
- \draw (446,39) node [scale=0.9,color={rgb, 255:red, 255; green, 0; blue, 0 } ,opacity=1 ] {$ 5 $ };
440
- % Text Node
441
- \draw (93,18) node [scale=0.9,color={rgb, 255:red, 255; green, 0; blue, 0 } ,opacity=1 ] {$ 0 $ };
442
- % Text Node
443
- \draw (213,18) node [scale=0.9,color={rgb, 255:red, 255; green, 0; blue, 0 } ,opacity=1 ] {$ 2 $ };
444
- % Text Node
445
- \draw (89,126) node [scale=0.9,color={rgb, 255:red, 255; green, 0; blue, 0 } ,opacity=1 ] {$ 1 $ };
446
- % Text Node
447
- \draw (330,18) node [scale=0.9,color={rgb, 255:red, 255; green, 0; blue, 0 } ,opacity=1 ] {$ 3 $ };
448
- % Text Node
449
- \draw (212,117) node [scale=0.9,color={rgb, 255:red, 255; green, 0; blue, 0 } ,opacity=1 ] {$ 4 $ };
450
- % Text Node
451
- \draw (141.5,33) .. controls (141.5,30.24) and (143.74,28) .. (146.5,28) -- (214.5,28) .. controls (217.26,28) and (219.5,30.24) .. (219.5,33) -- (219.5,89) .. controls (219.5,91.76) and (217.26,94) .. (214.5,94) -- (146.5,94) .. controls (143.74,94) and (141.5,91.76) .. (141.5,89) -- cycle ;
452
- \draw (180.5,61) node [color={rgb, 255:red, 62; green, 45; blue, 45 } ,opacity=1 ] {$ \begin {array}{l}
453
- S\rightarrow a.BC\\
454
- S\rightarrow a.bC\\
455
- B\rightarrow .b
456
- \end {array}$ };
457
- % Text Node
458
- \draw (121,51) node [scale=0.9,color={rgb, 255:red, 0; green, 0; blue, 0 } ,opacity=1 ] {$ a$ };
459
- % Text Node
460
- \draw (261.5,33) .. controls (261.5,30.24) and (263.74,28) .. (266.5,28) -- (332.5,28) .. controls (335.26,28) and (337.5,30.24) .. (337.5,33) -- (337.5,89) .. controls (337.5,91.76) and (335.26,94) .. (332.5,94) -- (266.5,94) .. controls (263.74,94) and (261.5,91.76) .. (261.5,89) -- cycle ;
461
- \draw (299.5,61) node [color={rgb, 255:red, 62; green, 45; blue, 45 } ,opacity=1 ] {$ \begin {array}{l}
462
- S\rightarrow ab.C\\
463
- B\rightarrow b.\\
464
- C\rightarrow .c
465
- \end {array}$ };
466
- % Text Node
467
- \draw (25,209) .. controls (25,206.24) and (27.24,204) .. (30,204) -- (91,204) .. controls (93.76,204) and (96,206.24) .. (96,209) -- (96,224) .. controls (96,226.76) and (93.76,229) .. (91,229) -- (30,229) .. controls (27.24,229) and (25,226.76) .. (25,224) -- cycle ;
468
- \draw (60.5,216.5) node {$ S'\rightarrow S\$ .$ };
469
- % Text Node
470
- \draw (69,111) node [scale=0.9,color={rgb, 255:red, 0; green, 0; blue, 0 } ,opacity=1 ] {$ S$ };
471
- % Text Node
472
- \draw (69,178) node [scale=0.9,color={rgb, 255:red, 0; green, 0; blue, 0 } ,opacity=1 ] {$ \$ $ };
473
- % Text Node
474
- \draw (378,53.5) .. controls (378,50.74) and (380.24,48.5) .. (383,48.5) -- (449,48.5) .. controls (451.76,48.5) and (454,50.74) .. (454,53.5) -- (454,68.5) .. controls (454,71.26) and (451.76,73.5) .. (449,73.5) -- (383,73.5) .. controls (380.24,73.5) and (378,71.26) .. (378,68.5) -- cycle ;
475
- \draw (416,61) node [color={rgb, 255:red, 62; green, 45; blue, 45 } ,opacity=1 ] {$ S\rightarrow abC.$ };
476
- % Text Node
477
- \draw (240,51) node [scale=0.9,color={rgb, 255:red, 0; green, 0; blue, 0 } ,opacity=1 ] {$ b$ };
478
- % Text Node
479
- \draw (356,51) node [scale=0.9,color={rgb, 255:red, 0; green, 0; blue, 0 } ,opacity=1 ] {$ C$ };
480
- % Text Node
481
- \draw (142,132) .. controls (142,129.24) and (144.24,127) .. (147,127) -- (215,127) .. controls (217.76,127) and (220,129.24) .. (220,132) -- (220,167) .. controls (220,169.76) and (217.76,172) .. (215,172) -- (147,172) .. controls (144.24,172) and (142,169.76) .. (142,167) -- cycle ;
482
- \draw (181,149.5) node [color={rgb, 255:red, 62; green, 45; blue, 45 } ,opacity=1 ] {$ \begin {array}{l}
483
- S\rightarrow aB.C\\
484
- C\rightarrow .c
485
- \end {array}$ };
486
- % Text Node
487
- \draw (142,210) .. controls (142,207.24) and (144.24,205) .. (147,205) -- (215,205) .. controls (217.76,205) and (220,207.24) .. (220,210) -- (220,225) .. controls (220,227.76) and (217.76,230) .. (215,230) -- (147,230) .. controls (144.24,230) and (142,227.76) .. (142,225) -- cycle ;
488
- \draw (181,217.5) node [color={rgb, 255:red, 62; green, 45; blue, 45 } ,opacity=1 ] {$ S\rightarrow aBC.$ };
489
- % Text Node
490
- \draw (271,142) .. controls (271,139.24) and (273.24,137) .. (276,137) -- (324,137) .. controls (326.76,137) and (329,139.24) .. (329,142) -- (329,157) .. controls (329,159.76) and (326.76,162) .. (324,162) -- (276,162) .. controls (273.24,162) and (271,159.76) .. (271,157) -- cycle ;
491
- \draw (300,149.5) node [color={rgb, 255:red, 62; green, 45; blue, 45 } ,opacity=1 ] {$ C\rightarrow c.$ };
492
- % Text Node
493
- \draw (247,139) node [scale=0.9,color={rgb, 255:red, 0; green, 0; blue, 0 } ,opacity=1 ] {$ c$ };
494
- % Text Node
495
- \draw (308,111) node [scale=0.9,color={rgb, 255:red, 0; green, 0; blue, 0 } ,opacity=1 ] {$ c$ };
496
- % Text Node
497
- \draw (190,184) node [scale=0.9,color={rgb, 255:red, 0; green, 0; blue, 0 } ,opacity=1 ] {$ C$ };
498
- % Text Node
499
- \draw (322,127) node [scale=0.9,color={rgb, 255:red, 255; green, 0; blue, 0 } ,opacity=1 ] {$ 6 $ };
500
- % Text Node
501
- \draw (213,195) node [scale=0.9,color={rgb, 255:red, 255; green, 0; blue, 0 } ,opacity=1 ] {$ 7 $ };
502
- % Text Node
503
- \draw (84.5,194) node [scale=0.9,color={rgb, 255:red, 255; green, 0; blue, 0 } ,opacity=1 ] {$ acc$ };
504
- % Connection
505
- \draw (99.5,61) -- (139.5,61) ;
506
- \draw [shift={(141.5,61)}, rotate = 180] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
507
-
508
- % Connection
509
- \draw (60.5,94) -- (60.5,134) ;
510
- \draw [shift={(60.5,136)}, rotate = 270] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
511
-
512
- % Connection
513
- \draw (60.5,161) -- (60.5,202) ;
514
- \draw [shift={(60.5,204)}, rotate = 270] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
515
-
516
- % Connection
517
- \draw (219.5,61) -- (259.5,61) ;
518
- \draw [shift={(261.5,61)}, rotate = 180] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
519
-
520
- % Connection
521
- \draw (337.5,61) -- (376,61) ;
522
- \draw [shift={(378,61)}, rotate = 180] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
523
-
524
- % Connection
525
- \draw (180.69,94) -- (180.86,125) ;
526
- \draw [shift={(180.87,127)}, rotate = 269.68] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
527
-
528
- % Connection
529
- \draw (181,172) -- (181,203) ;
530
- \draw [shift={(181,205)}, rotate = 270] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
531
-
532
- % Connection
533
- \draw (299.69,94) -- (299.92,135) ;
534
- \draw [shift={(299.93,137)}, rotate = 269.68] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
535
-
536
- % Connection
537
- \draw (220,149.5) -- (269,149.5) ;
538
- \draw [shift={(271,149.5)}, rotate = 180] [color={rgb, 255:red, 0; green, 0; blue, 0 } ][line width=0.75] (10.93,-3.29) .. controls (6.95,-1.4) and (3.31,-0.3) .. (0,0) .. controls (3.31,0.3) and (6.95,1.4) .. (10.93,3.29) ;
539
-
540
-
541
- \end {tikzpicture }
423
+ \begin {center }
424
+ \input {figures/GLR/GLR_example.tex }
425
+ \end {center }
542
426
543
427
И управляющую таблицу:
544
428
545
- \begin {tabular }{|c| c|c|c|c||c|c|c| }
546
- \hline & a & b & c & \$ & B & C & S \\ [0.5ex]
429
+ \begin {tabular }{c|| c|c|c|c||c|c|c}
430
+ & a & b & c & \$ & B & C & S \\ \hline
547
431
\hline 0 & $ s_2 $ & & & & & 1 & \\
548
432
\hline 1 & & & & acc & & & \\
549
433
\hline 2 & & $ s_3 $ & & & 4 & & \\
550
434
\hline 3 & & & $ s_6 $ , $ r_3 $ & & & 5 & \\
551
435
\hline 4 & & & $ s_6 $ & & & 7 & \\
552
436
\hline 5 & & & & $ r_1 $ & & & \\
553
437
\hline 6 & & & & $ r_4 $ & & & \\
554
- \hline 7 & & & & $ r_2 $ & & & \\ [1ex]
555
- \hline
438
+ \hline 7 & & & & $ r_2 $ & & &
556
439
\end {tabular }
557
440
558
441
Разберем слово $ w$ с помощью алгоритма GLR. Использована следующая аннотация: вершины-состояния обозначены кругами, вершины-символы --- прямоугольниками.
0 commit comments