Official problem:
Write Java method to perform recursive descent parse of the following production:
<repeat_statement> -> REPEAT <statement> UNTIL <expression> ;
This is what I've come up with:
void repeatStatement() {
if(token == REPEAT) {
token = getNextToken();
if(parseStatement()) {
if(token == UNTIL) {
token = getNextToken();
if(parseExpression()) {
if(token == ;) {
return true
}
}
}
} return false
}
I'm pretty confident that I have the general idea here, but I was hoping that someone could help me polish this/ confirm that I'm on the right track.
It looks (vaguely) like you're trying to evaluate the repeat statement. That's not what recursive descent parsing does. I'd expect something like this (in pseudocode):
RepeatStatement repeat_statement() throws ParseException {
if (!consume("REPEAT")) {
throw new ParseException("no REPEAT token");
}
Statement statement = statement();
if (!consume("UNTIL")) {
throw new ParseException("no UNTILtoken");
}
Expression expression = expression();
if (!consume(";")) {
throw new ParseException("no closing semicolon");
}
return new RepeatStatement(statement, expression);
}