[英]Automata: CFG for following language
Please have look at following language : 请查看以下语言:
(a, b, c)* − {a n b n c n |n≥0}
My question is: How do I write a Context-Free Grammar for it ? 我的问题是:如何为它编写上下文无关的语法?
In general how can I write a grammar when something is excludes from it (ie has "-" sign) ? 通常,当某些东西被排除在外时(即带有“-”号),我该如何编写语法?
There is no algorithm to determine whether a language is context-free. 没有确定语言是否上下文无关的算法。 So it is not surprising that there is no general solution to the question you pose. 因此,对于您提出的问题没有通用的解决方案也就不足为奇了。
Context-free languages are not closed under complementation, set difference or intersection. 上下文无关的语言在补语,集合差异或交集下不会封闭。 (But they are closed under concatenation, set union, and Kleene star.) In any event (但它们在连接,并集和Kleene星的作用下是关闭的。)
{a n b n c n |n≥0}
is not a context-free language, but its complement (as in your question) is context-free. 不是上下文无关的语言,但它的补语(如您的问题)是上下文无关的。 The proof of this fact (by constructing a context-free grammar for the complement) is a standard example for the non-closure of CFGs under complementation. 这一事实的证明(通过为补语构建上下文无关的语法)是在补语下不封闭CFG的标准示例。
In outline, your language L can be composed from the union of: 概括而言,您的语言L可以由以下各项的组合组成:
All strings over the alphabet {a,b,c}
where the letters are not in order. 字母{a,b,c}
上字母不按顺序排列的所有字符串。 In other words, all strings which contain the substring ba
, cb
or ca
. 换句话说,所有包含子字符串ba
, cb
或ca
的字符串。
{a i b j c k |i,j,k≥0∧i≠j}
{a i b j c k |i,j,k≥0∧j≠k}
(a, b, c)* − {a^nb^nc^n|n≥0}
This says you can choose any of a, b or c, and have any of them repeated eg abccbaabaab or abca or bccc 这表示您可以选择a,b或c中的任何一个,并重复其中的任何一个,例如abccbaabaab或abca或bccc
For a*, you use A->aA |e
or alternatively A->AA | a | e
对于a *,请使用A->aA |e
或A->AA | a | e
A->AA | a | e
A->AA | a | e
. A->AA | a | e
。
Using that rule, you can do: 使用该规则,您可以执行以下操作:
S -> A | B | C A->aA |e | AS B->bB |e | BS C->cC |e | CS
The inclusion of the S in each of A, B and C is what can be used if the whole thing has a Kleene star on it (....)*
, and allows you to go back to the start and add another symbol. 如果整个对象上都有一个Kleene星(....)*
,则可以使用A,B和C中的每个S,并允许您回到起点并添加另一个符号。
I don't know how to make a grammar where a symbol is excluded, but logically if -
is not an available terminal symbol, you have already excluded it. 我不知道如何制作排除符号的语法,但是从逻辑上讲,如果-
不是可用的终端符号,则您已经排除了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.