|
发表于 2021-5-2 22:34:37
|
显示全部楼层
我也改完了,这是最终版本
具体实现思路可以参考编译原理中的递归下降分析算法
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- /*
- * aPbATca
- * aPAbTc
- * a: (first: ('A', '\0'), follow: ('P'))
- * P: (first: ('P'), follow: ('A'))
- * A: (first: ('A'), follow: ('A', 'T'))
- * b: (first: ('A', '\0'), follow: ('T'))
- * T: (first: ('T'), follow: ('A', '\n'))
- * c: (first: ('A', '\0'), follow: ('\n'))
- */
- bool exist(int ch, int data[], size_t size) {
- for(size_t i = 0; i < size; ++i) {
- if(ch == data[i]) return true;
- }
- return false;
- }
- bool a(int first[], size_t first_size, int follow[], size_t follow_size, size_t depth) {
- int ch = getchar();
- if(exist(ch, first, first_size)) {
- return a(first, first_size, follow, follow_size, depth + 1);
- }
- if(depth == 0) {
- if(!exist('\0', first, first_size)) return false;
- }
- if(!exist(ch, follow, follow_size)) return false;
- ungetc(ch, stdin);
- return true;
- }
- bool P(int first[], size_t first_size, int follow[], size_t follow_size, size_t depth) {
- if(depth == 2) return false;
- int ch = getchar();
- if(exist(ch, first, first_size)) {
- return a(first, first_size, follow, follow_size, depth + 1);
- }
- if(depth == 0) {
- if(!exist('\0', first, first_size)) return false;
- }
- if(!exist(ch, follow, follow_size)) return false;
- ungetc(ch, stdin);
- return true;
- }
- bool A(int first[], size_t first_size, int follow[], size_t follow_size, size_t depth) {
- if(depth == 2) return false;
- int ch = getchar();
- if(exist(ch, first, first_size)) {
- return a(first, first_size, follow, follow_size, depth + 1);
- }
- if(depth == 0) {
- if(!exist('\0', first, first_size)) return false;
- }
- if(!exist(ch, follow, follow_size)) return false;
- ungetc(ch, stdin);
- return true;
- }
- bool b(int first[], size_t first_size, int follow[], size_t follow_size, size_t depth) {
- int ch = getchar();
- if(exist(ch, first, first_size)) {
- return a(first, first_size, follow, follow_size, depth + 1);
- }
- if(depth == 0) {
- if(!exist('\0', first, first_size)) return false;
- }
- if(!exist(ch, follow, follow_size)) return false;
- ungetc(ch, stdin);
- return true;
- }
- bool T(int first[], size_t first_size, int follow[], size_t follow_size, size_t depth) {
- if(depth == 2) return false;
- int ch = getchar();
- if(exist(ch, first, first_size)) {
- return a(first, first_size, follow, follow_size, depth + 1);
- }
- if(depth == 0) {
- if(!exist('\0', first, first_size)) return false;
- }
- if(!exist(ch, follow, follow_size)) return false;
- ungetc(ch, stdin);
- return true;
- }
- bool c(int first[], size_t first_size, int follow[], size_t follow_size, size_t depth) {
- int ch = getchar();
- if(exist(ch, first, first_size)) {
- return a(first, first_size, follow, follow_size, depth + 1);
- }
- if(depth == 0) {
- if(!exist('\0', first, first_size)) return false;
- }
- if(!exist(ch, follow, follow_size)) return false;
- ungetc(ch, stdin);
- return true;
- }
- int main(void) {
- size_t n;
- char ch;
- scanf("%lu", &n);
- while(scanf("%c", &ch), ch != '\n');
- bool result[n];
- for(size_t i = 0; i < n; ++i) result[i] = true;
- for(size_t i = 0; i < n; ++i) {
- do {
- if((result[i] = a((int []){'A', '\0'}, 2, (int []){'P'}, 1, 0)) == false) break;
- if((result[i] = P((int []){'P'}, 1, (int []){'A'}, 1, 0)) == false) break;
- if((result[i] = A((int []){'A'}, 1, (int []){'A', 'T'}, 2, 0)) == false) break;
- if((result[i] = b((int []){'A', '\0'}, 2, (int []){'T'}, 1, 0)) == false) break;
- if((result[i] = T((int []){'T', '\0'}, 2, (int []){'A', '\n'}, 2, 0)) == false) break;
- if((result[i] = c((int []){'A', '\0'}, 2, (int []){'\n'}, 1, 0)) == false) break;
- } while(0);
- while(scanf("%c", &ch), ch != '\n');
- }
- for(size_t i = 0; i < n; ++i) {
- result[i] ? puts("YES") : puts("NO");
- }
- return 0;
- }
复制代码 |
|