2014-10-30 62 views
1

我已经有相当多的问题,此任务:查找上下文无关文法

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的位置,并且会产生太多的情况。而且它只是列举案例,而不是一个“通用解决方案”,这显然不是目标。

回答

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 
+1

这是有道理的。怎么样的话w其中x = nr。一个+ 2 * nr。的B和X = 10 ... 10模5是0以及你的例子我不能建立abbab(这将在语言中),或者我可以吗? .... S-> M5S是否可能的解决方案? – R6D1H2 2014-10-30 20:02:10

+0

当然你是对的! 我修改了启动规则,以便M5的倍数是可能的。 现在:'abbbab < - abbb M3 < - abb M5 < - ab M2 M5 < - a M4 M5 < - M5 M5 = M5 M5ε< - M5 M5 S < - M5 S < - S' – 2014-10-30 20:13:12