Construct Context free Grammar

大兔子大兔子 提交于 2019-12-06 04:42:16

The grammar is :

  1. S1 -> a S1 d | S2

  2. S2 -> S3 S4

  3. S3 -> a S3 b | epsilon

  4. S4 -> S5 S6

  5. S5 -> b S5 c | epsilon

  6. S6 -> c S6 d | epsilon

Rule 1 adds equal number of a's and d's.

Rule 3 adds equal number of a's and b's.

Rule 5 adds equal number of b's and c's.

Rule 6 adds equal number of c's and d's

The rules also ensure that the ordering of the alphabets are maintained according to the language given.

How's about this:

S1 -> a S2 d                   # at least one a and d
S2 -> a S2 d
S2 -> S3 S4                    # no more d, split into ab and bc parts
S2 -> S4 S5                    # no more a, split into bc and cd parts

S3 -> a S3 b
S3 ->                          # already ensured at least one a and b
S4 -> b S4 c                  
S4 -> b c                      # at least one b and c
S5 -> c S5 d   
S5 ->                          # already ensured at least one c and d

The key to this is how you group... (i.e. "parts" rather than non-terminals.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!