Here is the source code for a simple example of a recursive descent parser. It parses a simple grammar similar to an assignment statement.
When run with a legal assignment statement for input, it prints a representation of the structure of the document. For instance:
The Scanner object (hdr, imple) breaks the input up into tokens. The Parser object contains a Scanner object which provides it with the next input symbol. Tokens are either names or integers, or a relevant operator or punctuation symbol.
The Parser object (hdr, imple) has a matcher method for each non-terminal symbol in the grammar. A matcher method consumes the portion of the input containing the symbol it is matching, and leaves the scanner pointing to the following token. It then returns an object representing the parse tree rooted at the symbol. If the matcher does not find its symbol in the input, it throws and exception.
Each matcher is implemented by looking for each right-hand side defined for its symbol. The fact function checks for each of its three alternatives listed in the Fact rule, and returns the one it finds. The expr function finds as many Terms as it can, separated by + or -, and returns them in tree form.
The matchers call each other as specified in the grammar rules. The function to parse the input simply calls the matcher for Asst.