In computer science, tail recursive parsers are a derivation 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.
Example
Given an EBNF Grammar such as the following:
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 N
A C programming language implementation of this parser is shown here:
See also
META II
Further reading
Article in the January 2006 edition of Dr. Dobbs Journal, "Recursive Descent, Tail Recursion, & the Dreaded Double Divide"