Knowledge

Tail recursive parser

Source 📝

32:
grammars. They use a smaller amount of stack space than regular recursive descent parsers. They are also easy to write. Typical recursive descent parsers make parsing left recursive grammars impossible (because of an infinite loop problem). Tail recursive parsers use a node reparenting technique that
130:
A simple tail recursive parser can be written much like a recursive descent parser. The typical algorithm for parsing a grammar like this using an
802: 1020: 1025: 776:
Article in the January 2006 edition of Dr. Dobbs Journal, "Recursive Descent, Tail Recursion, & the Dreaded Double Divide"
1051: 1030: 968: 870: 865: 837: 213: 25: 973: 916: 875: 998: 978: 795: 1010: 131: 1015: 983: 921: 899: 947: 993: 937: 854: 889: 819: 788: 17: 138:
Parse the next level of the grammar and get its output tree, designate it the first tree,
29: 1045: 911: 827: 942: 988: 894: 1005: 904: 775: 884: 832: 216:
is shown here. Implementation details have been omitted for simplicity.
811: 763: 780: 177:
Parse another level down again and store this as the next tree,
42: 784: 28:. Tail recursive parsers are commonly used to parse 961: 930: 853: 818: 161:'s current operator as the current input token 148:, that can be put as the parent of this node: 796: 8: 803: 789: 781: 212:A basic example of this kind of parser in 24:are a derivation from the more common 7: 144:While there is terminating token, 14: 1021:History of compiler construction 1026:Comparison of parser generators 45:Grammar such as the following: 1: 1031:Operator-precedence grammar 164:Advance the input one token 1068: 26:recursive descent parsers 218: 47: 974:Definite clause grammar 33:makes this allowable. 22:tail recursive parsers 979:Deterministic parsing 151:Allocate a new node, 187:'s right subtree as 132:abstract syntax tree 1016:Scannerless parsing 984:Dynamic programming 171:'s left subtree as 1052:Parsing algorithms 812:Parsing algorithms 1039: 1038: 838:Recursive descent 1059: 994:Parser generator 917:Recursive ascent 805: 798: 791: 782: 753: 750: 747: 744: 741: 738: 735: 732: 729: 726: 723: 720: 717: 714: 711: 708: 705: 702: 699: 696: 693: 690: 687: 684: 681: 678: 675: 672: 669: 666: 663: 660: 657: 654: 651: 648: 645: 642: 639: 636: 633: 630: 627: 624: 621: 618: 615: 612: 609: 606: 603: 600: 597: 594: 591: 588: 585: 582: 579: 576: 573: 570: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 510: 507: 504: 501: 498: 495: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 327: 324: 321: 318: 315: 312: 309: 306: 303: 300: 297: 294: 291: 288: 285: 282: 279: 276: 273: 270: 267: 264: 261: 258: 255: 252: 249: 246: 243: 240: 237: 234: 231: 228: 225: 222: 126: 123: 120: 117: 114: 111: 108: 105: 102: 99: 96: 93: 90: 87: 84: 81: 78: 75: 72: 69: 66: 63: 60: 57: 54: 51: 18:computer science 1067: 1066: 1062: 1061: 1060: 1058: 1057: 1056: 1042: 1041: 1040: 1035: 957: 926: 849: 814: 809: 772: 770:Further reading 760: 755: 754: 751: 748: 745: 742: 739: 736: 733: 730: 727: 724: 721: 718: 715: 712: 709: 706: 703: 700: 697: 694: 691: 688: 685: 682: 679: 676: 673: 670: 667: 664: 661: 658: 655: 652: 649: 646: 643: 640: 637: 634: 631: 628: 625: 622: 619: 616: 613: 610: 607: 604: 601: 598: 595: 592: 589: 586: 583: 580: 577: 574: 571: 568: 565: 562: 559: 556: 553: 550: 547: 544: 541: 538: 535: 532: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 496: 493: 490: 487: 484: 481: 478: 475: 472: 469: 466: 463: 460: 457: 454: 451: 448: 445: 442: 439: 436: 433: 430: 427: 424: 421: 418: 415: 412: 409: 406: 403: 400: 397: 394: 391: 388: 385: 382: 379: 376: 373: 370: 367: 364: 361: 358: 355: 352: 349: 346: 343: 340: 337: 334: 331: 328: 325: 322: 319: 316: 313: 310: 307: 304: 301: 298: 295: 292: 289: 286: 283: 280: 277: 274: 271: 268: 265: 262: 259: 256: 253: 250: 247: 244: 241: 238: 235: 232: 229: 226: 223: 220: 208: 200: 196: 190: 186: 180: 174: 170: 160: 154: 147: 141: 128: 127: 124: 121: 118: 115: 112: 109: 106: 103: 100: 97: 94: 91: 88: 85: 82: 79: 76: 73: 70: 67: 64: 61: 58: 55: 52: 49: 39: 12: 11: 5: 1065: 1063: 1055: 1054: 1044: 1043: 1037: 1036: 1034: 1033: 1028: 1023: 1018: 1013: 1008: 1003: 1002: 1001: 991: 986: 981: 976: 971: 965: 963: 962:Related topics 959: 958: 956: 955: 952: 951: 950: 940: 934: 932: 928: 927: 925: 924: 919: 914: 909: 908: 907: 902: 897: 892: 882: 881: 880: 879: 878: 868: 859: 857: 851: 850: 848: 847: 846: 845: 843:Tail recursive 835: 830: 824: 822: 816: 815: 810: 808: 807: 800: 793: 785: 779: 778: 771: 768: 767: 766: 759: 756: 219: 210: 209: 206: 203: 202: 201: 198: 194: 191: 188: 184: 181: 178: 175: 172: 168: 165: 162: 158: 155: 152: 145: 142: 139: 48: 38: 35: 30:left recursive 13: 10: 9: 6: 4: 3: 2: 1064: 1053: 1050: 1049: 1047: 1032: 1029: 1027: 1024: 1022: 1019: 1017: 1014: 1012: 1009: 1007: 1004: 1000: 997: 996: 995: 992: 990: 987: 985: 982: 980: 977: 975: 972: 970: 967: 966: 964: 960: 953: 949: 946: 945: 944: 941: 939: 936: 935: 933: 929: 923: 920: 918: 915: 913: 910: 906: 903: 901: 898: 896: 893: 891: 888: 887: 886: 883: 877: 876:Shunting-yard 874: 873: 872: 869: 867: 864: 863: 861: 860: 858: 856: 852: 844: 841: 840: 839: 836: 834: 831: 829: 826: 825: 823: 821: 817: 813: 806: 801: 799: 794: 792: 787: 786: 783: 777: 774: 773: 769: 765: 762: 761: 757: 217: 215: 204: 192: 182: 176: 166: 163: 156: 150: 149: 143: 137: 136: 135: 133: 46: 44: 36: 34: 31: 27: 23: 19: 931:Mixed, other 922:Shift-reduce 842: 629:replace_tree 605:replace_tree 581:replace_tree 563:replace_tree 551:replace_tree 461:replace_tree 437:replace_tree 413:replace_tree 395:replace_tree 383:replace_tree 211: 129: 40: 21: 15: 989:Memoization 954:Statistical 948:Left corner 905:Generalized 862:Precedence 536:'*' 368:'+' 98:'*' 71:'+' 1006:Parse tree 938:Combinator 895:Look-ahead 737:next_token 683:alloc_tree 599:next_token 557:alloc_tree 431:next_token 389:alloc_tree 122:identifier 900:Canonical 855:Bottom-up 731:cur_token 575:cur_token 527:cur_token 407:cur_token 359:cur_token 41:Given an 1046:Category 871:Operator 820:Top-down 758:See also 239:_exptree 227:_exptree 764:META II 671:exptree 656:parse_i 650:exptree 641:first_i 623:first_i 617:parse_i 593:first_i 545:exptree 515:parse_i 509:first_i 503:exptree 488:parse_f 482:exptree 473:first_f 455:first_f 449:parse_f 425:first_f 377:exptree 347:parse_f 341:first_f 335:exptree 320:parse_t 314:exptree 305:parse_t 287:parse_e 281:exptree 266:exptree 254:exptree 230:exptree 221:typedef 205:Return 37:Example 890:Simple 866:Simple 828:Earley 743:return 638:return 470:return 302:return 236:struct 224:struct 943:Chart 725:token 722:-> 707:right 704:-> 692:-> 611:right 608:-> 584:-> 569:token 566:-> 521:while 443:right 440:-> 416:-> 401:token 398:-> 353:while 272:right 248:token 999:LALR 713:NULL 695:left 662:void 587:left 494:void 419:left 326:void 293:void 260:left 245:char 193:Set 183:Set 167:Set 157:Set 134:is: 125:> 119:< 43:EBNF 1011:AST 969:PEG 912:CYK 740:(); 734:(); 686:(); 620:(); 602:(); 578:(); 560:(); 518:(); 452:(); 434:(); 410:(); 392:(); 350:(); 308:(); 197:to 16:In 1048:: 885:LR 833:LL 533:== 530:() 365:== 362:() 278:}; 101:I 92:F 74:F 65:T 20:, 804:e 797:t 790:v 752:} 749:; 746:i 728:= 719:i 716:; 710:= 701:i 698:= 689:i 680:= 677:i 674:* 668:{ 665:) 659:( 653:* 647:} 644:; 635:} 632:; 626:= 614:= 596:; 590:= 572:= 554:= 548:* 542:{ 539:) 524:( 512:= 506:* 500:{ 497:) 491:( 485:* 479:} 476:; 467:} 464:; 458:= 446:= 428:; 422:= 404:= 386:= 380:* 374:{ 371:) 356:( 344:= 338:* 332:{ 329:) 323:( 317:* 311:} 299:{ 296:) 290:( 284:* 275:; 269:* 263:; 257:* 251:; 242:{ 233:; 214:C 207:N 199:N 195:F 189:X 185:N 179:X 173:F 169:N 159:N 153:N 146:T 140:F 116:: 113:I 110:I 107:| 104:} 95:{ 89:: 86:F 83:F 80:| 77:} 68:{ 62:: 59:T 56:T 53:: 50:E

Index

computer science
recursive descent parsers
left recursive
EBNF
abstract syntax tree
C
META II
Article in the January 2006 edition of Dr. Dobbs Journal, "Recursive Descent, Tail Recursion, & the Dreaded Double Divide"
v
t
e
Parsing algorithms
Top-down
Earley
LL
Recursive descent
Tail recursive
Bottom-up
Simple
Operator
Shunting-yard
LR
Simple
Look-ahead
Canonical
Generalized
CYK
Recursive ascent
Shift-reduce
Combinator

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