Given the production
<expression> ::= <term> (<addop> <term>) *
A recursive-descent parser would have a function:
Phrase * ParseExpression () {
Phrase * Term1;
Phrase * Term2;
Phrase * Operator;
Term1 = ParseTerm ();
while (Current.IsAddop) {
Operator = ParseAddop ();
Term2 = ParseTerm ();
Term1 = new Term (Term1, Operator, Term2);
}
return Term1;
}
Given the production
<term> ::= <factor> (<mulop> <factor>) *
A recursive-descent parser would have a function:
Phrase * ParseTerm () {
Phrase * Factor1;
Phrase * Factor2;
Phrase * Operator;
Factor1 = ParseFactor ();
while (Current.IsMulop) {
Operator = ParseMulop ();
Factor2 = ParseFactor ();
Factor1 = new Term (Factor1, Operator, Factor2);
}
return Factor1;
}
Given the production
<factor> ::= '(' <expression> ')' | <Variable>
A recursive-descent parser would have a function:
Phrase * ParseFactor () {
Phrase * Factor;
if (Current.IsLeftParen) {
TakeIt (); // pass the leftparen;
Factor = ParseExpression ();
TakeIt (RightParen);
} else {
Factor = ParseVariable ();
}
return Factor;
}