构建此文法的LL(1)分析表,要求包含#,18. S -> A | B | C 19. A -> T A' A' -> + T A' | - T A' | ε 20. T -> F T' T' -> * F T' | / F T' | ε 21. F -> Q | (A) 22. Q -> id | int 23. B -> X B' B' -> or X B' | ε 24. X -> Y X' X' -> and Y X' | ε 25. Y -> P Y' Y' -> not Y | ε 26. P -> b | id | (B) id R id | (A) R (A) 27. C -> D 28. D -> c | id 29. R -> < | <> | <= | >= | > | =
时间: 2023-06-15 21:08:02 浏览: 87
首先,我们需要求出所有非终结符的 FIRST 集和 FOLLOW 集:
- FIRST(A) = { id, int }
- FIRST(A') = { +, -, ε }
- FIRST(T) = { id, int }
- FIRST(T') = { *, /, ε }
- FIRST(F) = { id, int, ( }
- FIRST(Q) = { id, int }
- FIRST(B) = { id, b, ε }
- FIRST(B') = { or, ε }
- FIRST(X) = { id, int, b, not, ε }
- FIRST(X') = { and, ε }
- FIRST(Y) = { id, int, b, ε }
- FIRST(Y') = { not, ε }
- FIRST(P) = { b, id, ( }
- FIRST(D) = { c, id }
- FIRST(R) = { <, <>, <=, >=, >, = }
- FOLLOW(S) = { # }
- FOLLOW(A) = { or, # }
- FOLLOW(A') = { or, # }
- FOLLOW(T) = { +, -, or, # }
- FOLLOW(T') = { +, -, or, # }
- FOLLOW(F) = { *, /, +, -, or, # }
- FOLLOW(Q) = { *, /, +, -, or, # }
- FOLLOW(B) = { or, # }
- FOLLOW(B') = { or, # }
- FOLLOW(X) = { and, or, # }
- FOLLOW(X') = { and, or, # }
- FOLLOW(Y) = { not, and, or, # }
- FOLLOW(Y') = { not, and, or, # }
- FOLLOW(P) = { *, /, +, -, or, ), # }
- FOLLOW(D) = { or, # }
- FOLLOW(R) = { (, id, int, b, not, (, c, # }
然后,我们可以根据 FIRST 集和 FOLLOW 集求出分析表:
| | id | int | b | c | + | - | * | / | ( | ) | or | and | not | < | <> | <= | >= | > | = | # |
| --- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- |
| S | A | A | C | | | | | | | | B | | | | | | | | | |
| A | T A' | T A' | | | | | | | | | | | | | | | | | | |
| A' | + T A' | - T A' | ε | ε | | | | | | | or ε | | | | | | | | # |
| T | F T' | F T' | | | | | | | | | | | | | | | | | | |
| T' | ε | ε | ε | ε | + F T' | - F T' | * F T' | / F T' | | | or ε | and ε | not ε | | | | | | # |
| F | Q | Q | ( A ) | | | | | | | | | | | | | | | | | |
| Q | id | int | | | | | | | | | | | | | | | | | | |
| B | X B' | X B' | b | c | | | | | | | ε | | | | | | | | | |
| B' | or X B' | ε | | | | | | | | | or ε | | | | | | | | # |
| X | Y X' | Y X' | b | c | | | | | | | | and ε | not ε | | | | | | # |
| X' | ε | ε | ε | ε | ε | ε | ε | ε | or ε | or ε | and ε | and ε | ε | | | | | | # |
| Y | P | P | b | c | | | | | | | | | not ε | | | | | | # |
| Y' | not Y | ε | | | | | | | | | | | ε | | | | | | # |
| P | b | id | ( B ) id R id | ( A ) R ( A ) | | | | | ( A ) | | | | | | | | | | | |
| D | id | int | | c | | | | | | | | | | | | | | | | # |
| R | < | <> | <= | >= | > | = | | | | | | | | | | | | | | |
其中,ε 表示空串。
阅读全文