簡體   English   中英

查找無上下文語法

[英]Finding context-free grammar

我在執行此任務時遇到了一個問題:

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的位置,否則會產生太多情況。 同樣,這僅僅是對案件的列舉,而不是“一般解決方案”,這顯然不是目標。

我建議介紹輔助的非終端符號:

  • M5 = a the number of a's plus 2 times the number of b's modulo 5 in w is 0
  • M4 = a the number of a's plus 2 times the number of b's modulo 4 in w is 0
  • M3 = a the number of a's plus 2 times the number of b's modulo 3 in w is 0
  • M2 = a 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM