Tail recursive parser
Appearance
Tail Recursive Parser
A Tail Recursive Parser is on that uses a tail recursive parsing technique, it is an alternative to complex bottom-up parsers. Tail Recursive Parsing is a method of parsing a Context Free Grammar, it is usually used in a Compiler for semantic analysis of a source program. Tail Recursive parsers are typically used because they can be easily written (given their closeness to Recursive Descent parsers), and use little stack space. Given a EBNF Grammar such as the following:
E: T T: T ('+' F)* | F F: I ('*' I)* | I I: <identifier>
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