简体   繁体   English

如何将常规语法转换为有限自动机?

[英]How to convert a regular grammar to finite automaton?

How does one convert a regular grammar into a finite automaton (FA)?如何将常规语法转换为有限自动机(FA)? For instance, what would a finite automaton corresponding to the following regular grammar look like?例如,对应于以下常规语法的有限自动机会是什么样子?

VN = {S, B, D} (nonterminals)
VT = {a, b, c} (terminals)
 P = {S -> aB, S -> bB, B -> bD, D -> b, D -> aD, B -> cB, B -> aS} (productions)

The good news is that this is not too hard.好消息是这并不太难。 The idea is that each of the nonterminals will become a state in a nondeterministic finite automaton accepting the language of the grammar, and productions will become transitions.这个想法是每个非终结符都将成为接受语法语言的非确定性有限自动机中的一个状态,产生式将成为转换。 Our NFA will have states S, B and D, and will transition among those states according to the production rules.我们的 NFA 将具有状态 S、B 和 D,并将根据产生式规则在这些状态之间转换。 Our NFA looks like this:我们的 NFA 看起来像这样:

       ___a__      _a_
      /      \    /   \
      |      |    \   /
      V      |     \ /
----->S-a,b->B--b-->D
            / \
           /   \
           \_c_/

There was one dangling production D -> b which we haven't added yet.有一个悬而未决的生产D -> b我们还没有添加。 We need to introduce another state, not corresponding to an nonterminal symbol, to allow us to transition from D on b and accept some strings:我们需要引入另一种状态,不对应于非终结符,以允许我们从 b 上的 D 转换并接受一些字符串:

       ___a__      _a_
      /      \    /   \
      |      |    \   /
      V      |     \ /
----->S-a,b->B--b-->D--b-->Q
            / \
           /   \
           \_c_/

Now if we make S the initial state and Q the accepting state, we have an NFA that works.现在,如果我们让 S 成为初始状态,Q 成为接受状态,我们就有了一个有效的 NFA。 If we want a DFA, we might notice that this FA is only nondeterministic because we are lacking required transitions from states S, D and Q. We can add the missing transitions by introducing a new dead state X which will keep track of the NFA we just derived having crashed at some point during its processing:如果我们想要一个 DFA,我们可能会注意到这个 FA 只是不确定的,因为我们缺少从状态 S、D 和 Q 所需的转换。我们可以通过引入一个新的死状态 X 来添加丢失的转换,这将跟踪我们的 NFA刚刚得出在处理过程中的某个时刻崩溃了:

       ___a__      _a_
      /      \    /   \
      |      |    \   /
      V      |     \ /
----->S-a,b->B--b-->D--b-->Q
      |     / \     |      |
      |    /   \    |      |    a,b,c
      c    \_c_/    c    a,b,c  /   \
      |             |      |    \   /
      V             V      V     \ /
      +-------------+------+----->X

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

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