现在的位置: 首页 > 综合 > 正文

正则表达式引擎的构建——基于编译原理DFA(龙书第三章)——5 DFA最小化

2017年09月27日 ⁄ 综合 ⁄ 共 6729字 ⁄ 字号 评论关闭

分类: C++ 算法 编译原理 421人阅读 评论(0) 收藏 举报

目录(?)[+]


完整引擎代码在github上,地址为:https://github.com/sun2043430/RegularExpression_Engine.git

DFA最小化的算法原理

“DFA状态最小化算法的工作原理是将一个DFA的状态集合分划成多个组,每个组中的各个状态之间相互不可区分。然后,将每个组中的状态合并成状态最少DFA的一个状态。算法在执行过程中维护了状态集合的一个分划,分划中的每个组内的各个状态不能区分,但是来自不同组的任意两个状态是可区分的。当任意一个组不能再被分解为更小的组时,这个分划就不能再进一步精化,此时我们就得到了状态最少的DFA。”

                  ——《编译原理》例3.38

起始时,该分划包含两个组:接受状态组和非接受状态组。


实例

如图(编译原理
图3-36)

首先我们将ABCDE分划到两个组中{ABCD}和{E},{E}是接受状态组,且不可被再分割。

{ABCD}是可分割的,所以我们考虑所有可能的转换。

先看转换字符a:

A->a->B

B->a->B

C->a->B

D->a->B

所以ABCD经过a到达的集合是{B},而{B}属于{ABCD}这一个集合,所以我们说{ABCD}在输入字符为a时是不可分划的。

在来看转换字符b:

A->b->C

B->b->D

C->b->C

D->b->E

到达的集合是{CDE},其中CD属于{ABCD}分划,E属于{E}分划。

所以我们说{ABCD}在输入字符为b时是不可分划的。按照输入b转换到的组,我们将{ABCD}分划为{ABC}和{D}两个组。同时将{ABCD}从组集合中删除。因为{ABCD}已经分划为{ABC}和{D}。

接下来看{ABC},先看在字符a上的转换:

A->a->B

B->a->B

C->a->B

因为全部都是到达的B,所以不可分划。

再看在字符b上的转换:

A->b->C

B->b->D

C->b->C

其中C属于{ABC}组,D属于{D}组。所以{ABC}可以分划为{AC}和{B}。

最后看{AC}组,A和C在字符a上都转换到B,在字符b上都转换到C,所以{AC}是不可分划的组。

最后得到的分组情况为:

{AC},{B},{D},{E}。

同一个组中只需要保留一个节点即可(因为同一个组的节点在转换上都是相同的),所以我们直接将C节点去除,保留A节点(因为A节点是开始状态节点)。最终得到的状态最小DFA的转换表为:


代码实现(关键代码)

  1. BOOL CDFA::FindRelationNode(list<DFANodeRelation> &lstNodeRelation,   
  2.                             int nIdxFrom, unsigned char ch, int &nMapToIdx)  
  3. {  
  4.     list<DFANodeRelation>::iterator it = lstNodeRelation.begin();  
  5.     for ( ; it != lstNodeRelation.end(); it++)  
  6.     {  
  7.         if (it->m_nIdxFrom == nIdxFrom && it->m_ch == ch)  
  8.         {  
  9.             nMapToIdx = it->m_nIdxTo;  
  10.             return TRUE;  
  11.         }  
  12.     }  
  13.     return FALSE;  
  14. }  
  15.   
  16. int CDFA::FindIdxInListSet(int nMapToIdx, list<set<int>> &lstSet)  
  17. {  
  18.     int i = 0;  
  19.     for (list<set<int>>::iterator it = lstSet.begin(); it != lstSet.end(); it++, i++)  
  20.     {  
  21.         set<int> & setIdx = *it;  
  22.         for (set<int>::iterator itInt = setIdx.begin(); itInt != setIdx.end(); itInt++)  
  23.         {  
  24.             if (nMapToIdx == *itInt)  
  25.             {  
  26.                 return i;  
  27.             }  
  28.         }  
  29.     }  
  30.     return -1;  
  31. }  
  32.   
  33. BOOL CDFA::PartitionOneGroup(list<set<int>> &lstSet, set<int> &setOneGroup,   
  34.                              list<DFANodeRelation> &lstNodeRelation,   
  35.                              map<int, set<int>> &mapPartitionInfo)  
  36. {  
  37.     BOOL            bRet            = FALSE;  
  38.     list<DFANodeRelation>::iterator itRelation;  
  39.     set<unsigned char>              setChar;  
  40.     set<int>                        setMapToIdx;  
  41.   
  42.     try  
  43.     {  
  44.         // collect each node's translation char in the set  
  45.         for (set<int>::iterator it = setOneGroup.begin(); it != setOneGroup.end(); it++)  
  46.         {  
  47.             for (itRelation = lstNodeRelation.begin(); itRelation != lstNodeRelation.end(); itRelation++)  
  48.             {  
  49.                 if (itRelation->m_nIdxFrom == *it)  
  50.                 {  
  51.                     setChar.insert(itRelation->m_ch);  
  52.                 }  
  53.             }  
  54.         }  
  55.         // end collect  
  56.   
  57.         for (set<unsigned char>::iterator it = setChar.begin(); it != setChar.end(); it++)  
  58.         {  
  59.             mapPartitionInfo.clear();  
  60.             int nMapToIdx = -1; // indicate map to a dead state, there no translation for this pair of node/char  
  61.             for (set<int>::iterator itNodeId = setOneGroup.begin(); itNodeId != setOneGroup.end(); itNodeId++)  
  62.             {  
  63.                 if (FindRelationNode(lstNodeRelation, *itNodeId, *it, nMapToIdx))  
  64.                 {  
  65.                     int nIdx = FindIdxInListSet(nMapToIdx, lstSet);  
  66.                     if (nIdx == -1)  
  67.                         assert(FALSE);  
  68.                     mapPartitionInfo[nIdx].insert(*itNodeId);  
  69.                 }  
  70.                 else  
  71.                     mapPartitionInfo[-1].insert(*itNodeId);  
  72.             }  
  73.             if (mapPartitionInfo.size() > 1)// had distinguish  
  74.             {  
  75.                 break;  
  76.             }  
  77.         }  
  78.     }  
  79.     catch (...)  
  80.     {  
  81.         goto Exit0;  
  82.     }  
  83.   
  84.     bRet = TRUE;  
  85. Exit0:  
  86.     return bRet;  
  87. }  
  88.   
  89. BOOL CDFA::PartitionGroups(list<set<int>> &lstSet, list<DFANodeRelation> &lstNodeRelation)  
  90. {  
  91.     BOOL                        bRet   = FALSE;  
  92.     list<set<int>>::iterator    it     = lstSet.begin();  
  93.     map<int, set<int>>          mapPartitionInfo;  
  94.     //  used map to record the node can translate to which group,   
  95.     // the int(map key) is group id.  
  96.     // the set<int> contain the node ID that can translate to the group.  
  97.   
  98.     for ( ; it != lstSet.end(); )  
  99.     {  
  100.         mapPartitionInfo.clear();  
  101.         set<int> &setOneGroup = *it;  
  102.         CHECK_BOOL ( PartitionOneGroup(lstSet, setOneGroup, lstNodeRelation, mapPartitionInfo) );  
  103.         if (mapPartitionInfo.size() > 1)// means that current group can partition  
  104.         {  
  105.             map<int, set<int>>::iterator itM = mapPartitionInfo.begin();  
  106.             for ( ; itM != mapPartitionInfo.end(); itM++)  
  107.             {  
  108.                 try  
  109.                 {  
  110.                     lstSet.push_back(itM->second);  
  111.                 }  
  112.                 catch (...)  
  113.                 {  
  114.                     goto Exit0;  
  115.                 }  
  116.             }  
  117.             it = lstSet.erase(it);// if a group had partition, the group need delete in the list  
  118.   
  119.         }  
  120.         else  
  121.              it++;  
  122.     }  
  123.   
  124.     bRet = TRUE;  
  125. Exit0:  
  126.     return bRet;  
  127. }  
  128.   
  129. /** 
  130.     @brief     Minimize DFA 
  131.     @param     nSetSize            node count  
  132.     @param     lstNodeRelation     node relation table 
  133.     @param     setAcceptingIdx     set for Accepting status node's index 
  134.     @param     lstSet              for save the result 
  135.     @return    TRUE, success; otherwise means fail. 
  136. */  
  137. BOOL CDFA::MinimizeDFA(int                     nNodeCount,  
  138.                        list<DFANodeRelation>   &lstNodeRelation,  
  139.                        set<int>                &setAcceptingIdx,  
  140.                        list<set<int>>          &lstSet  
  141. )  
  142. {  
  143.     BOOL            bRet            = FALSE;  
  144.     set<int>        setUnAccepting;  
  145.   
  146.     assert(nNodeCount >= 1);  
  147.     assert(setAcceptingIdx.size() != 0);  
  148.     assert(lstNodeRelation.size() != 0);  
  149.   
  150.     lstSet.clear();  
  151.   
  152.     try   
  153.     {  
  154.         lstSet.push_back(setAcceptingIdx);  
  155.   
  156.         // get unAccepting set  
  157.         for (int i = 0; i < nNodeCount; i++)  
  158.         {  
  159.             if (setAcceptingIdx.find(i) == setAcceptingIdx.end())  
  160.             {  
  161.                 setUnAccepting.insert(i);  
  162.             }  
  163.         }  
  164.         if (setUnAccepting.size() > 0)  
  165.         {  
  166.             lstSet.push_back(setUnAccepting);  
  167.         }  
  168.     }  
  169.     catch (...)  
  170.     {  
  171.         goto Exit0;  
  172.     }  
  173.   
  174.     CHECK_BOOL ( PartitionGroups(lstSet, lstNodeRelation) );  
  175.   
  176.     bRet = TRUE;  
  177. Exit0:  
  178.       
  179.     return bRet;  
  180. }  

完整引擎代码在github上,地址为:https://github.com/sun2043430/RegularExpression_Engine.git

抱歉!评论已关闭.