Knowledge

Simple LR parser

Source 📝

33: 248:
LALR generators calculate lookahead sets by a more precise method based on exploring the graph of parser states and their transitions. This method considers the particular context of the current parser state. It customizes the handling of each grammar occurrence of some nonterminal S. See article
415:
As can be observed there is a shift-reduce conflict for state 1 and terminal '1'. This occurs because, when the action table for an LR(0) parser is created, reduce actions are inserted on a per-row basis. However, by using a follow set, reduce actions can be added with finer granularity. The follow
253:
for further details of this calculation. The lookahead sets calculated by LALR generators are a subset of (and hence better than) the approximate sets calculated by SLR generators. If a grammar has table conflicts when using SLR follow sets, but is conflict-free when using LALR follow sets, it is
172:
have identical methods and similar tables at parse time; they differ only in the mathematical grammar analysis algorithms used by the parser generator tool. SLR and LALR generators create tables of identical size and identical parser states. SLR generators accept fewer grammars than do LALR
223:
SLR generators calculate that lookahead by an easy approximation method based directly on the grammar, ignoring the details of individual parser states and transitions. This ignores the particular context of the current parser state. If some nonterminal symbol
185:
form requires more compromises and grammar hackery. So LALR generators have become much more widely used than SLR generators, despite being somewhat more complicated tools. SLR methods remain a useful learning step in college classes on compiler theory.
197:'s LR parser theory. The tables created for real grammars by full LR methods were impractically large, larger than most computer memories of that decade, with 100 times or more parser states than the SLR and LALR methods.. 240:
uses Follow(S) as its LR(1) lookahead set. Such follow sets are also used by generators for LL top-down parsers. A grammar that has no shift/reduce or reduce/reduce conflicts when using follow sets is called an
451:
A reduce only needs to be added to a particular action column if that action is in the follow set associated with that reduce. This algorithm describes whether a reduce action must be added to an action column:
205:
To understand the differences between SLR and LALR, it is important to understand their many similarities and how they both make shift-reduce decisions. (See the article
161:
in a single left-to-right scan over the input stream, without guesswork or backtracking. The parser is mechanically generated from a formal grammar for the language.
455:
function mustBeAdded(reduceAction, action) { ruleNumber = reduceAction.value; ruleSymbol = rules.leftHandSide; return (action in followSet(ruleSymbol)) }
216:
The one difference between SLR and LALR is how their generators calculate the lookahead sets of input symbols that should appear next, whenever some completed
228:
is used in several places in the grammar, SLR treats those places in the same single way rather than handling them individually. The SLR generator works out
157:
and a relatively simple parser generator algorithm. As with other types of LR(1) parser, an SLR parser is quite efficient at finding the single correct
605: 124: 54: 823: 828: 105: 77: 58: 181:. Many computer languages don't readily fit the restrictions of SLR, as is. Bending the language's natural grammar into 84: 854: 833: 771: 673: 276:
Constructing the action and goto table as is done for LR(0) parsers would give the following item sets and tables:
91: 668: 640: 43: 73: 776: 719: 678: 469:
By using mustBeAdded on each reduce action in the action table, the result is a conflict-free action table:
62: 47: 801: 781: 645: 598: 813: 462:
is false, because the left hand side of rule 2 is "E", and 1 is not in E's follow set. Contrariwise,
818: 786: 724: 702: 190: 169: 750: 796: 740: 657: 158: 98: 622: 591: 138: 262:
A grammar that can be parsed by an SLR parser but not by an LR(0) parser is the following:
217: 848: 714: 630: 745: 194: 232:, the set of all terminal symbols which can immediately follow some occurrence of 791: 697: 578: 573: 250: 242: 182: 165: 32: 808: 707: 687: 635: 568: 563: 206: 178: 154: 150: 17: 614: 583: 174: 587: 209:
now for that background, up through the section on reductions'
26: 764: 733: 656: 621: 463: 459: 599: 8: 466:is true, because "$ " is in E's follow set. 61:. Unsourced material may be challenged and 606: 592: 584: 236:. In the parse table, each reduction to 125:Learn how and when to remove this message 471: 418: 328: 7: 189:SLR and LALR were both developed by 59:adding citations to reliable sources 25: 164:SLR and the more-general methods 824:History of compiler construction 31: 829:Comparison of parser generators 464:mustBeAdded(r2, "$ ") 193:as the first practical uses of 460:mustBeAdded(r2, "1") 1: 542: 530: 516: 503: 487: 473: 434: 420: 399: 387: 373: 360: 344: 330: 326:The action and goto tables: 834:Operator-precedence grammar 871: 476: 333: 777:Definite clause grammar 254:called a LALR grammar. 416:set for this grammar: 220:is found and reduced. 782:Deterministic parsing 55:improve this article 819:Scannerless parsing 787:Dynamic programming 170:Canonical LR parser 855:Parsing algorithms 615:Parsing algorithms 74:"Simple LR parser" 842: 841: 641:Recursive descent 555: 554: 449: 448: 413: 412: 135: 134: 127: 109: 16:(Redirected from 862: 797:Parser generator 720:Recursive ascent 608: 601: 594: 585: 472: 465: 461: 419: 329: 231: 173:generators like 139:computer science 130: 123: 119: 116: 110: 108: 67: 35: 27: 21: 870: 869: 865: 864: 863: 861: 860: 859: 845: 844: 843: 838: 760: 729: 652: 617: 612: 560: 456: 260: 229: 218:production rule 203: 159:bottom-up parse 131: 120: 114: 111: 68: 66: 52: 36: 23: 22: 15: 12: 11: 5: 868: 866: 858: 857: 847: 846: 840: 839: 837: 836: 831: 826: 821: 816: 811: 806: 805: 804: 794: 789: 784: 779: 774: 768: 766: 765:Related topics 762: 761: 759: 758: 755: 754: 753: 743: 737: 735: 731: 730: 728: 727: 722: 717: 712: 711: 710: 705: 700: 695: 685: 684: 683: 682: 681: 671: 662: 660: 654: 653: 651: 650: 649: 648: 646:Tail recursive 638: 633: 627: 625: 619: 618: 613: 611: 610: 603: 596: 588: 582: 581: 576: 571: 566: 559: 556: 553: 552: 550: 547: 545: 541: 540: 538: 535: 533: 529: 528: 525: 522: 519: 515: 514: 511: 509: 506: 502: 501: 498: 495: 492: 486: 485: 480: 475: 454: 447: 446: 443: 440: 437: 433: 432: 429: 426: 423: 411: 410: 408: 405: 402: 398: 397: 395: 392: 390: 386: 385: 382: 379: 376: 372: 371: 368: 366: 363: 359: 358: 355: 352: 349: 343: 342: 337: 332: 324: 323: 320: 316: 315: 312: 308: 307: 304: 301: 298: 295: 291: 290: 287: 284: 281: 274: 273: 270: 267: 259: 256: 211:lookahead sets 202: 201:Lookahead sets 199: 133: 132: 39: 37: 30: 24: 14: 13: 10: 9: 6: 4: 3: 2: 867: 856: 853: 852: 850: 835: 832: 830: 827: 825: 822: 820: 817: 815: 812: 810: 807: 803: 800: 799: 798: 795: 793: 790: 788: 785: 783: 780: 778: 775: 773: 770: 769: 767: 763: 756: 752: 749: 748: 747: 744: 742: 739: 738: 736: 732: 726: 723: 721: 718: 716: 713: 709: 706: 704: 701: 699: 696: 694: 691: 690: 689: 686: 680: 679:Shunting-yard 677: 676: 675: 672: 670: 667: 666: 664: 663: 661: 659: 655: 647: 644: 643: 642: 639: 637: 634: 632: 629: 628: 626: 624: 620: 616: 609: 604: 602: 597: 595: 590: 589: 586: 580: 577: 575: 572: 570: 567: 565: 562: 561: 557: 551: 548: 546: 543: 539: 536: 534: 531: 526: 523: 520: 517: 512: 510: 507: 504: 499: 496: 493: 491: 488: 484: 481: 479: 474: 470: 467: 458:for example, 453: 444: 441: 438: 435: 430: 427: 424: 421: 417: 409: 406: 403: 400: 396: 393: 391: 388: 383: 380: 377: 374: 369: 367: 364: 361: 356: 353: 350: 348: 345: 341: 338: 336: 331: 327: 321: 318: 317: 313: 310: 309: 305: 302: 299: 296: 293: 292: 288: 285: 282: 279: 278: 277: 271: 268: 265: 264: 263: 257: 255: 252: 246: 244: 239: 235: 227: 221: 219: 214: 212: 208: 200: 198: 196: 192: 191:Frank DeRemer 187: 184: 180: 176: 171: 167: 162: 160: 156: 152: 149:is a type of 148: 144: 140: 129: 126: 118: 115:December 2012 107: 104: 100: 97: 93: 90: 86: 83: 79: 76: –  75: 71: 70:Find sources: 64: 60: 56: 50: 49: 45: 40:This article 38: 34: 29: 28: 19: 734:Mixed, other 725:Shift-reduce 692: 489: 482: 477: 468: 457: 450: 414: 346: 339: 334: 325: 275: 261: 247: 237: 233: 225: 222: 215: 210: 204: 195:Donald Knuth 188: 163: 155:parse tables 146: 142: 136: 121: 112: 102: 95: 88: 81: 69: 53:Please help 41: 792:Memoization 757:Statistical 751:Left corner 708:Generalized 665:Precedence 579:SLR grammar 574:LALR parser 303:+ E → • 1 E 286:+ E → • 1 E 269:(1) E → 1 E 251:LALR parser 243:SLR grammar 183:SLR grammar 166:LALR parser 153:with small 809:Parse tree 741:Combinator 698:Look-ahead 436:following 319:Item set 3 311:Item set 2 294:Item set 1 280:Item set 0 147:SLR parser 85:newspapers 18:SLR parser 703:Canonical 658:Bottom-up 569:LL parser 564:LR parser 322:E → 1 E • 306:+ E → • 1 297:E → 1 • E 289:+ E → • 1 272:(2) E → 1 266:(0) S → E 230:Follow(S) 207:LR parser 151:LR parser 143:Simple LR 42:does not 849:Category 674:Operator 623:Top-down 558:See also 422:symbol 314:S → E • 300:E → 1 • 283:S → • E 258:Example 99:scholar 63:removed 48:sources 693:Simple 669:Simple 631:Earley 478:action 335:action 101:  94:  87:  80:  72:  746:Chart 490:state 445:1,$ 378:s1/r2 347:state 179:Bison 106:JSTOR 92:books 802:LALR 483:goto 340:goto 177:and 175:yacc 168:and 141:, a 78:news 46:any 44:cite 814:AST 772:PEG 715:CYK 537:acc 394:acc 213:.) 145:or 137:In 57:by 851:: 688:LR 636:LL 549:r1 544:3 532:2 527:3 524:r2 521:s1 518:1 513:2 508:s1 505:0 500:E 497:$ 442:$ 439:$ 431:1 407:r1 404:r1 401:3 389:2 384:3 381:r2 375:1 370:2 365:s1 362:0 357:E 354:$ 245:. 607:e 600:t 593:v 494:1 428:E 425:S 351:1 238:S 234:S 226:S 128:) 122:( 117:) 113:( 103:· 96:· 89:· 82:· 65:. 51:. 20:)

Index

SLR parser

cite
sources
improve this article
adding citations to reliable sources
removed
"Simple LR parser"
news
newspapers
books
scholar
JSTOR
Learn how and when to remove this message
computer science
LR parser
parse tables
bottom-up parse
LALR parser
Canonical LR parser
yacc
Bison
SLR grammar
Frank DeRemer
Donald Knuth
LR parser
production rule
SLR grammar
LALR parser
LR parser

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.