对文法G[S]:S→a|∧|(T);T→T,S|S。对文法进行改写,然后对每个非终结符写出不带回溯的递归子程序
时间: 2023-06-01 13:02:22 浏览: 125
改写后的文法:
S → aS' | ∧
S' → TS' | S'
T → TS' | S
递归子程序:
void S() {
if (lookahead == 'a') {
match('a');
S_prime();
} else if (lookahead == '∧') {
match('∧');
} else if (lookahead == '(') {
match('(');
S();
match(')');
S_prime();
} else {
// 报错
}
}
void S_prime() {
if (lookahead == 'T' || lookahead == 'a' || lookahead == '∧' || lookahead == '(') {
T();
S_prime();
} else if (lookahead == 'S') {
match('S');
S_prime();
} else {
// 不做任何操作,即 S' → ε
}
}
void T() {
if (lookahead == 'T' || lookahead == 'a' || lookahead == '∧' || lookahead == '(') {
S();
S_prime();
} else {
// 报错
}
}
阅读全文