I'm trying to implement parser combinators for educational purposes. A parser is simply defined as an std::function<T(input&)>
. I have come to a point that I would be able to define a simple expression grammar, using something like this code:
auto expression; // the type does not matter for the context
auto factor = number
| (token('(') >= expression >= token(')'))
;
auto term = factor >= *( (token('*') | token('/')) >= factor );
expression = term >= *( (token('+') | token('-')) >= term );
As it can be seen, the problem is with the circular dependencies. I can not figure out a way to resolve the circular dependency that the grouped factor causes. The operators constructing the factor parser need the expression parser but that is only constructed at the very end. How could I resolve this?
Aucun commentaire:
Enregistrer un commentaire