简体   繁体   English

如果我们获得了语言L的语法,该如何找到L *的常规语法?

[英]How can I find regular grammar for L* if we are given a grammar for language L?

Is there any general method to do so? 有什么通用的方法吗? For example, we have a general method to find grammar for L1 U L2 by adding a production S-> S1 | 例如,我们有一种通用的方法,通过添加生产S-> S1 | | | | | | | | | | | | | | | |,来查找L1 U L2的语法。 S2 where S1 and S2 are start symbols for grammars of L1 and L2 respectively. S2,其中S1和S2分别是L1和L2语法的起始符号。 Thanks in advance.. 提前致谢..

In general, given a grammar G such that L(G) = L' , there is no algorithm which always produces a regular grammar G' such that L(G') = (L')* . 通常,给定语法G使得L(G) = L' ,没有算法总是产生规则语法G'使得L(G') = (L')* For starters, (L')* may not be a regular language. 对于初学者, (L')*可能不是常规语言。 Even if you allow the procedure to recognize this case and print "not a regular language" in such a case, this cannot be generally possible since it would allow us to determine whether arbitrary unrestricted grammars generate particular strings (the construction is not too hard but I won't provide it unless desired). 即使您允许该过程识别这种情况并在这种情况下打印“非常规语言”,这通常也是不可能的,因为这将使我们能够确定任意不受限制的语法是否会生成特定的字符串(这种构造不是很困难,但是除非需要,否则我不会提供。 This is an undecidable problem, so we can't recognize regular languages in unrestricted grammars. 这是一个无法确定的问题,因此我们无法识别无限制语法中的常规语言。

Perhaps your question is whether there is a neat construction to do this if initially given a regular grammar. 也许您的问题是,如果最初使用常规语法,是否有一个整洁的结构可以做到这一点。 In that case, the answer is a definite and clear, "yes!" 在这种情况下,答案是明确明确的“是!” Here is one easily described (though possibly inefficient in practice) procedure for doing just that: 这是一个很容易描述的过程(尽管在实践中可能效率低下),仅用于此操作:

  1. Convert the regular grammar into a nondeterministic finite automaton using the typical construction for doing so. 使用典型的构造将规则语法转换为不确定的有限自动机。 There are easy constructions for left-regular and right-regular grammars. 左规则和右规则语法有简单的构造。
  2. Construct a regular expression from the nondeterministic finite automaton using any known construction. 使用任何已知的构造从不确定的有限自动机构造一个正则表达式。 One such construction is typically used in proving equivalence. 通常在证明等效性时使用一种这样的构造。
  3. Construct a new regular expression which is the Kleene closure of the one from the last step. 构造一个新的正则表达式,它是最后一步中的Kleene闭包。
  4. Construct a nondeterministic finite automaton from the regular expression from the last step, using a standard construction. 使用标准构造,从最后一步的正则表达式构造一个不确定的有限自动机。
  5. Construct a regular grammar from the nondeterministic finite automaton from the last step. 从最后一步开始,从不确定的有限自动机构造规则语法。 There are known constructions for this. 有已知的构造。

Thus, we can mechanically go from regular grammar for L to regular grammar for L* . 因此,我们可以机械地从L的常规语法变为L*常规语法。

If you just want ANY grammar for L* , the simplest would probably be to introduce a new start state S' and productions S' := S'S' | S 如果只希望L*任何语法,最简单的方法可能是引入一个新的开始状态S'和产生式S' := S'S' | S S' := S'S' | S where S is the start symbol of your input grammar. S' := S'S' | S ,其中S是输入语法的开始符号。 This obviously does not give a regular grammar, however - if the input grammar generates a regular language, this one will do so as well. 这显然不能给出规则的语法,但是-如果输入语法生成了规则的语言,则该语法也可以。

Example: given the regular grammar 示例:给出常规语法

S := 0S | 1T
T := 0S | 1T | 1

A construction gives us this nondeterministic finite automaton: 构造为我们提供了这种不确定的有限自动机:

q    s    q'
-    -    -
S    0    S
S    1    T
T    0    S
T    1    T
T    1    (H)

A construction gives us the regular expression: 构造为我们提供了正则表达式:

(0*1)(0*1)*1

The Kleene closure of this is: Kleene的闭包为:

((0*1)(0*1)*1)*

We recognize from the standard construction that this automaton is equivalent: 我们从标准构造中认识到此自动机是等效的:

q    s    q'
-    -    -
(I)  -    S
S    0    S
S    1    T
T    0    S
T    1    T
T    1    H
H    -    (I)

Whence the following regular grammar: 遵循以下常规语法:

I := S | -
S := 0S | 1T
T := 0S | 1T | H
H := I

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

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