这个任务我遇到了很多问题:
L = {w element of {a,b}* |
the number of a's plus 2 times the number of b's modulo 5 in w is 0}
我想到了:
S -> ε
S -> abbS
S -> babS
S -> bbaS
S -> aaaaaS
S -> aaabS
等...
但这不是最佳解决方案,因为你还必须改变S的位置,它会产生太多的情况。此外,它只是一个列举的案例,而不是一个“一般解决方案”,显然不是目标。
答案 0 :(得分:1)
我建议引入辅助非终端符号:
M5
= the number of a's plus 2 times the number of b's modulo 5 in w is 0
M4
= the number of a's plus 2 times the number of b's modulo 4 in w is 0
M3
= the number of a's plus 2 times the number of b's modulo 3 in w is 0
M2
= the number of a's plus 2 times the number of b's modulo 2 in w is 0
然后语法可以表达如下:
S -> ε
S -> M5 S
M5 -> a M4
M5 -> M4 a
M5 -> b M3
M5 -> M3 b
M4 -> a M3
M4 -> M3 a
M4 -> b M2
M4 -> M2 b
M3 -> a M2
M3 -> M2 a
M3 -> b a
M3 -> a b
M2 -> a a
M2 -> b