I'm interested in writing a parser (syntax analyser) from a BNF grammar without using a generator tool like yacc or bison.
I take as example the BNF for simple arithmetic expression : (extracted from the Dragon Book 2.2.6)
expr -> expr + term | expire - term | term
term -> term * factor | term / factor | factor
factor -> number | (expr)
Suppose I would like to create the equivalent code.
I guess I should create three functions called parseExpr
, parseTerm
and parseFactor
.
How do I construct these functions regarding to the BNF defined upwards ?
For the parseFactor
it seems to be obvious :
- Get the token type
- If type number return a node for number type
- If the token represents an opening parenthesis, get the node returned by
parseExpr
- Check that next token is a closing parenthesis. If yes, return the node obtained at 3. If no, throw an error
For the parseExpr
, I'm a little bit confused about how to start and interpret the production : expr -> expr + term | expire - term | term
How to translate this production ? How to detect which case applies ? Same question for the last production ?