简体   繁体   English

长度为3的字符串的CFG

[英]CFG for string of length 3

I'm trying to create context free grammar for the following language: 我正在尝试为以下语言创建上下文无关的语法:

L={w∈{a,b}* | L = {w∈{a,b} * | w is of the form a^nxb^n+2, where x is any string of length 3 in {a,b}*} w的格式为a ^ nxb ^ n + 2,其中x是{a,b} *}中任何长度为3的字符串

(a is raised to the power of n, and b is raised to the power of n+2) (a提高到n的幂,b提高到n + 2的幂)

My solution this far is ( _ is epsilon): 到目前为止,我的解决方案是(_是epsilon):

S -> aSbb | S-> aSbb | T | T | _ _

T -> aT | T-> aT | bT | bT | _ _

However this is not correct as it for example accepts string abbb. 但是,这是不正确的,因为它例如接受字符串abbb。 My problem is that I don't understand how to make the T part of the grammar, how can I limit the string to be size of 3? 我的问题是我不了解如何使T成为语法的一部分,如何限制字符串的大小为3? It would not make any sense to list all the strings of length 3 in {a,b}*, or is it the only way? 在{a,b} *中列出所有长度为3的字符串是没有任何意义的,还是唯一的方法?

Also I'm not 100% confident if the aSbb is correct, because I think my solution is now a^nxb^2n, or am I wrong? 另外,我不确定aSbb是否正确,因为我认为我的解决方案现在是a ^ nxb ^ 2n,还是我100%不确定,还是我错了?

The correct CFG for the language is: 该语言的正确CFG为:

S -> aSb | T  
T -> UUUbb  
U -> a | b

Thanks everybody for help! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM