英文:
Writing a recursive descent parser with nested if-conditions
问题
// if <cond> <statement>
else if (token.equals("if")) {
token = getToken();
cond1 = (parseCond(token));
cond = (cond1) && (cond);
token = getToken();
execute = (cond) && (execute);
parseStmt(cond, token);
token = getToken();
if (token.equals("else")) {
if (cond) {
execute = false;
token = getToken();
parseStmt(cond, token);
} else {
execute = (cond) && (execute);
token = getToken();
parseStmt(cond, token);
cond = true;
execute = true;
token = getToken();
}
} else {
line = token + line;
}
}
英文:
I am writing a recursive descent parser for parsing a specific grammar, and I have to do the nested if/else in that grammar.
The grammar is as follows:
<statement> ::= if <cond> <statement>
In the parseStmt
we have to do the following: basically the getToken()
method will remove blanks and grab the token.
// if <cond> <statement>
else if (token.equals("if")) {
token = getToken();
cond1 = (parseCond(token));
cond = (cond1) && (cond);
token = getToken();
execute = (cond) && (execute);
parseStmt(cond, token);
token = getToken();
if (token.equals("else")) {
if (cond) {
execute = false;
token = getToken();
parseStmt(cond, token);
} else {
execute = (cond) && (execute);
token = getToken();
parseStmt(cond, token);
cond = true;
execute = true;
token = getToken();
}
} else {
line = token + line;
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论