对文法G[S]:S→a|∧|(T);T→T,S|S。对文法进行改写,对每个非终结符写出不带回溯的递归子程序
时间: 2023-06-01 22:02:23 浏览: 151
改写后的文法如下:
S → aS' | ∧
S' → TS' | S'
T → T,S | S
对应的不带回溯的递归子程序如下:
```
void S() {
if (lookahead == 'a') {
match('a');
S'();
} else if (lookahead == EPSILON) {
// do nothing
} else {
error();
}
}
void S'() {
if (lookahead == 'a') {
match('a');
S'();
} else if (lookahead == '(' || lookahead == 'a') {
T();
S'();
} else if (lookahead == ')' || lookahead == '$') {
// do nothing
} else {
error();
}
}
void T() {
if (lookahead == '(' || lookahead == 'a') {
S();
T_();
} else {
error();
}
}
void T_() {
if (lookahead == ',') {
match(',');
S();
T_();
} else if (lookahead == ')' || lookahead == '$') {
// do nothing
} else {
error();
}
}
```
阅读全文