Tail recursive parser

Tail recursive parser

Tail recursive parsers are derived from the more common Recursive descent parsers. Tail recursive parsers are commonly used to parse left recursive 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 makes this allowable. Given a EBNF Grammar such as the following: E: T T: T ('+' F)* | F F: F ('*' I)* | I I: 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 Abstract syntax tree is:
#Parse the next level of the grammar and get its output tree, designate it the first tree, F
#While there is terminating token, T, that can be put as the parent of this node:
##Allocate a new node, N
##Set N's current operator as the current input token
##Advance the input one token
##Set N's left subtree as F
##Parse another level down again and store this as the next tree, X
##Set N's right subtree as X
##Set F to N
#Return NA C programming language implementation of this parser is shown here: typedef struct _exptree exptree; struct _exptree { char token; exptree *left; exptree *right; }; exptree *parse_e(void) { return parse_t(); } exptree *parse_t(void) { exptree *first_f = parse_f(); while(cur_token() = '+') { exptree *replace_tree = alloc_tree(); replace_tree->token = cur_token(); replace_tree->left = first_f; next_token(); replace_tree->right = parse_f(); first_f = replace_tree; } return first_f; } exptree *parse_f(void) { exptree *first_i = parse_i(); while(cur_token() = '*') { exptree *replace_tree = alloc_tree(); replace_tree->token = cur_token(); replace_tree->left = first_i; next_token(); replace_tree->right = parse_i(); first_i = replace_tree; } return first_i; } exptree *parse_i(void) { exptree *i = alloc_tree(); exptree->left = exptree->right = NULL; exptree->token = cur_token(); next_token(); return i; }

Further reading

* [http://www.ddj.com/documents/s=9938/ddj0601e/0601e.html Article in the January 2006 edition of Dr. Dobbs Journal, "Recursive Descent, Tail Recursion, & the Dreaded Double Divide"]


Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Recursive descent parser — A recursive descent parser is a top down parser built from a set of mutually recursive procedures (or a non recursive equivalent) where each such procedure usually implements one of the production rules of the grammar. Thus the structure of the… …   Wikipedia

  • Recursion (computer science) — Recursion in computer science is a way of thinking about and solving problems. It is, in fact, one of the central ideas of computer science. [cite book last = Epp first = Susanna title = Discrete Mathematics with Applications year=1995… …   Wikipedia

  • Lisp (programming language) — Infobox programming language name = Lisp paradigm = multi paradigm: functional, procedural, reflective generation = 3GL year = 1958 designer = John McCarthy developer = Steve Russell, Timothy P. Hart, and Mike Levin latest release version =… …   Wikipedia

  • Prolog — infobox programming language paradigm = Logic programming year = 1972 designer = Alain Colmerauer implementations = BProlog, ECLiPSe, Ciao Prolog, GNU Prolog, Quintus, SICStus, Strawberry, SWI Prolog, YAP Prolog, tuProlog dialects = ISO Prolog,… …   Wikipedia

  • Left recursion — In computer science, left recursion is a special case of recursion. In terms of context free grammar, a non terminal r is left recursive if the left most symbol in any of r’s ‘alternatives’ either immediately (direct left recursive) or through… …   Wikipedia

  • Dynamic programming — For the programming paradigm, see Dynamic programming language. In mathematics and computer science, dynamic programming is a method for solving complex problems by breaking them down into simpler subproblems. It is applicable to problems… …   Wikipedia

  • Recursion — Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition. The term is also used more generally to describe a process of repeating objects in a self… …   Wikipedia

  • OCaml — Paradigm(s) multi paradigm: imperative, functional, object oriented Appeared in 1996 Developer INRIA Stable release 3.12.1 (July 4, 2011; 4 months ago ( …   Wikipedia

  • Objective Caml — Infobox programming language name = Objective Caml paradigm = multi paradigm: imperative, functional, object oriented developer = INRIA latest release version = 3.10.2 latest release date = Release date and age|2008|02|29 operating system = Cross …   Wikipedia

  • Rekursionsanfang — Dieser Artikel erläutert die Technik der rekursiven Definition; zum Begriff rekursive Menge siehe entscheidbar. Als Rekursion (lat. recurrere „zurücklaufen“) bezeichnet man die Technik in Mathematik, Logik und Informatik, eine Funktion durch sich …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”