Next | Higher-Order Parsing | 14 |
Each grammar rule has a corresponding function
The job of the function expression() is to parse an expression
If it succeeds, it returns a data structure representing the expression
If not, it returns a failure indication
Suppose you have a rule like this:
expression → "(" expression ")" | term ("+" expression | nothing)
You will have functions called expression() and term()
expression() gets the token list as an argument
It looks to see if the next token is (
If so, it calls itself recursively, and then looks for the )
Otherwise it calls term() to look for a term
If term() fails, expression() does too
Otherwise it looks to see if there's a + sign and another expression
Next | Copyright © 2007 M. J. Dominus |