I';我正在尝试做NFA->;C#中的DFA翻译,使用unicode引入的powerset构造和处理大范围
我使用单字符值在DFA上成功地实现了powerset构造(我认为是huffman最小化),但是我修改了FA状态,以便能够处理unicode范围。 现在它们看起来像这样:I';我正在尝试做NFA->;C#中的DFA翻译,使用unicode引入的powerset构造和处理大范围,c#,dfa,nfa,C#,Dfa,Nfa,我使用单字符值在DFA上成功地实现了powerset构造(我认为是huffman最小化),但是我修改了FA状态,以便能够处理unicode范围。 现在它们看起来像这样: sealed partial class FA { public readonly Dictionary<KeyValuePair<int,int>,FA> InputTransitions = new Dictionary<KeyValuePair<int,int>,FA>
sealed partial class FA
{
public readonly Dictionary<KeyValuePair<int,int>,FA> InputTransitions = new Dictionary<KeyValuePair<int,int>,FA>();
public readonly HashSet<FA> EpsilonTransitions = new HashSet<FA>();
public int AcceptSymbol = -1;
public bool IsAccepting = false;
密封部分类FA
{
公共只读词典InputTransitions=新词典();
public readonly HashSet EpsilonTransitions=new HashSet();
公共int-AcceptSymbol=-1;
公共bool IsAccepting=false;
我的字符存储为UTF-32值整数,因此我不必处理代理项
每个输入转换可以采用一个范围,其起始和结束UTF-32值编码为keyvaluepair
我已经修改了我的ToDfa()例程(以前用于单个值)以处理范围,但它无法正常工作。NormalizeSortedRangeList()接受预排序的keyvaluepair范围列表并组合重叠范围(用包含所有范围的单个范围替换多个重叠范围).\u SetComparer只对列表和集合进行无序比较
public FA ToDfa(IProgress<FAProgress> progress=null)
{
// if it's already a DFA we don't need to do this transformation.
// however, we still need to clone the state machine it because
// the consumer expects a copy, not the original state.
if (IsDfa)
return Clone();
// The DFA states are keyed by the set of NFA states they represent.
var dfaMap = new Dictionary<List<FA>, FA>(_SetComparer.Default);
var unmarked = new HashSet<FA>();
// compute the epsilon closure of the initial state in the NFA
var states = new List<FA>();
FillEpsilonClosure(states);
// create a new state to represent the current set of states. If one
// of those states is accepting, set this whole state to be accepting.
FA dfa = new FA();
var al = new List<int>();
// find the accepting symbols for the current states
foreach (var fa in states)
if (fa.IsAccepting)
if (!al.Contains(fa.AcceptSymbol))
al.Add(fa.AcceptSymbol);
// here we assign the appropriate accepting symbol
int ac = al.Count;
//if (1 == ac)
if(0<ac)
dfa.AcceptSymbol = al[0];
//else if (1 < ac)
// dfa.AcceptSymbol = al[0]; // could throw, just choose the first one
dfa.IsAccepting = 0 < ac;
FA result = dfa; // store the initial state for later, so we can return it.
// add it to the dfa map
dfaMap.Add(states, dfa);
// add it to the unmarked states, signalling that we still have work to do.
unmarked.Add(dfa);
bool done = false;
var j = 0;
while (!done)
{
if(null!=progress)
{
progress.Report(new FAProgress(FAStatus.DfaTransform, j));
}
done = true;
// a new hashset used to hold our current key states
var mapKeys = new HashSet<List<FA>>(dfaMap.Keys, _SetComparer.Default);
foreach (var mapKey in mapKeys)
{
dfa = dfaMap[mapKey];
if (unmarked.Contains(dfa))
{
// when we get here, mapKey represents the epsilon closure of our
// current dfa state, which is indicated by kvp.Value
// build the transition list for the new state by combining the transitions
// from each of the old states
// retrieve every possible input for these states
var inputs = new List<KeyValuePair<int,int>>();
foreach (var state in mapKey)
{
foreach (var trns in state.InputTransitions)
if(!inputs.Contains(trns.Key))
inputs.Add(trns.Key);
}
inputs.Sort((x, y) => {
var c = x.Key.CompareTo(y.Key);
if (0 == c)
c = x.Value.CompareTo(y.Value);
return c;
});
RangeUtility.NormalizeSortedRangeList(inputs);
// for each input, create a new transition
foreach (var input in inputs)
{
var acc = new List<int>();
var ns = new List<FA>();
foreach (var state in mapKey)
{
foreach (var trns in state.InputTransitions)
{
if (RangeUtility.Intersects(trns.Key, input))
{
FA dst = trns.Value;
foreach (var d in dst.FillEpsilonClosure())
{
// add the accepting symbols
if (d.IsAccepting)
if (!acc.Contains(d.AcceptSymbol))
acc.Add(d.AcceptSymbol);
if (!ns.Contains(d))
ns.Add(d);
}
}
}
}
FA ndfa;
if (!dfaMap.TryGetValue(ns, out ndfa))
{
ac = acc.Count;
ndfa = new FA(0 < ac);
// assign the appropriate accepting symbol
//if (1 == ac)
if(0<ac)
ndfa.AcceptSymbol = acc[0];
//else if (1 < ac)
// ndfa.AcceptSymbol = acc[0]; // could throw, instead just set it to the first state's accept
dfaMap.Add(ns, ndfa);
// work on this new state
unmarked.Add(ndfa);
done = false;
}
dfa.InputTransitions.Add(input, ndfa);
}
// we're done with this state
unmarked.Remove(dfa);
}
}
++j;
}
return result;
}
公共FA ToDfa(IProgress progress=null)
{
//如果它已经是DFA,我们不需要进行这种转换。
//但是,我们仍然需要克隆状态机,因为
//消费者需要的是副本,而不是原始状态。
国际单项体育联合会(IsDfa)
返回克隆();
//DFA状态由它们所代表的NFA状态集进行键控。
var dfaMap=新字典(_SetComparer.Default);
var unmarked=新HashSet();
//计算NFA中初始状态的ε闭包
var states=新列表();
FillEpsilonClosure(状态);
//创建新状态以表示当前状态集。如果
//如果这些状态中有一个是接受的,则将整个状态设置为接受。
FA dfa=新的FA();
var al=新列表();
//查找当前状态的接受符号
foreach(美国的var fa)
如果(fa.正在接受)
如果(!al.包含(fa.AcceptSymbol))
所有添加(fa.符号);
//这里我们指定适当的接受符号
int ac=总计数;
//如果(1==ac)
如果(0
你到底是如何在powerset构造中实现范围的?有几个库实现了这一点,但没有一个是我能理解的我终于在自己的代码中采用了Fare/bric的解决方案
// Portions of this code adopted from Fare, itself adopted from brics
// original copyright notice included.
// This is the only file this applies to.
/*
* dk.brics.automaton
*
* Copyright (c) 2001-2011 Anders Moeller
* All rights reserved.
* http://github.com/moodmosaic/Fare/
* Original Java code:
* http://www.brics.dk/automaton/
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public static FA _Determinize(FA fa, IProgress<FAProgress> progress = null)
{
if (null != progress)
progress.Report(new FAProgress(FAStatus.DfaTransform, 0));
var p = new HashSet<int>();
var closure = new List<FA>();
fa.FillClosure(closure);
for(int ic=closure.Count,i=0;i<ic;++i)
{
var ffa = closure[i];
p.Add(0);
foreach (var t in ffa.InputTransitions)
{
p.Add(t.Key.Key);
if (t.Key.Value < 0x10ffff)
{
p.Add((t.Key.Value + 1));
}
}
}
var points = new int[p.Count];
p.CopyTo(points, 0);
Array.Sort(points);
var comparer = _SetComparer.Default;
var sets = new Dictionary<ICollection<FA>, ICollection<FA>>(comparer);
var working = new Queue<ICollection<FA>>();
var dfaMap = new Dictionary<ICollection<FA>, FA>(comparer);
var initial = fa.FillEpsilonClosure();
sets.Add(initial, initial);
working.Enqueue(initial);
var result = new FA();
foreach(var afa in initial)
{
if(afa.IsAccepting)
{
result.IsAccepting = true;
result.AcceptSymbol = afa.AcceptSymbol;
break;
}
}
dfaMap.Add(initial, result);
var j = 1;
while (working.Count > 0)
{
ICollection<FA> s = working.Dequeue();
var ecs = FillEpsilonClosure(s);
FA dfa;
dfaMap.TryGetValue(s, out dfa);
foreach (FA q in ecs)
{
if (q.IsAccepting)
{
dfa.IsAccepting = true;
dfa.AcceptSymbol = q.AcceptSymbol;
break;
}
}
for (var i = 0; i < points.Length; i++)
{
var set = new HashSet<FA>();
foreach (FA c in ecs)
{
foreach (var trns in c.InputTransitions)
{
if (trns.Key.Key <= points[i] && points[i] <= trns.Key.Value)
{
foreach (var efa in trns.Value.FillEpsilonClosure())
set.Add(trns.Value);
}
}
}
if (!sets.ContainsKey(set))
{
sets.Add(set, set);
working.Enqueue(set);
dfaMap.Add(set, new FA());
}
FA dst;
dfaMap.TryGetValue(set, out dst);
int first = points[i];
int last;
if (i + 1 < points.Length)
last = (points[i + 1] - 1);
else
last = 0x10ffff;
dfa.InputTransitions.Add(new KeyValuePair<int, int>(first, last), dst);
}
if (null != progress)
progress.Report(new FAProgress(FAStatus.DfaTransform, j));
++j;
}
// remove dead transitions
foreach(var ffa in result.FillClosure())
{
var itrns = new List<KeyValuePair<KeyValuePair<int, int>, FA>>(ffa.InputTransitions);
ffa.InputTransitions.Clear();
foreach(var trns in itrns)
{
if(null!=trns.Value.FirstAcceptingState)
{
ffa.InputTransitions.Add(trns.Key, trns.Value);
}
}
if (null != progress)
progress.Report(new FAProgress(FAStatus.DfaTransform, j));
++j;
}
return result;
}
//本规范的部分内容采用了Fare,其本身也采用了金砖四国的标准
//包括原始版权声明。
//这是唯一适用于此的文件。
/*
*dk.brics.automaton
*
*版权所有(c)2001-2011安德斯·默勒
*版权所有。
* http://github.com/moodmosaic/Fare/
*原始Java代码:
* http://www.brics.dk/automaton/
*
*以源代码和二进制形式重新分发和使用,带或不带
*如果满足以下条件,则允许进行修改
*满足以下条件:
*1.源代码的重新分发必须保留上述版权
*请注意,此条件列表和以下免责声明。
*2.以二进制形式重新分发必须复制上述版权
*请注意,此条件列表和中的以下免责声明
*随分发提供的文件和/或其他材料。
*3.作者的姓名不得用于产品的宣传或推广
*未经事先书面许可,从本软件派生。
*
*本软件由作者“按原样”和任何明示或暗示的
*默示保证,包括但不限于默示保证
*对于适销性和特定用途的适用性不作任何声明。
*在任何情况下,提交人均不对任何直接、间接,
*附带、特殊、惩戒性或后果性损害(包括但不限于
*不限于,替代货物或服务的采购;使用损失,
*数据或利润;或业务中断),无论是何种原因造成的
*责任理论,无论是合同责任、严格责任还是侵权责任
*(包括疏忽或其他)因使用
*此软件,即使已告知可能发生此类损坏。
*/
公共静态FA_Determinize(FA-FA,IProgress progress=null)
{
如果(null!=进度)
进度报告(新FAProgress(FAStatus.DfaTransform,0));
var p=新的HashSet();
var closure=新列表();
fa.关闭(关闭);
for(intic=closure.Count,i=0;i0)
{
ICollection s=working.Dequeue();
var ecs=填充ε封闭;
FA-dfa;
dfaMap.TryGetValue(s,out dfa);
foreach(ecs中的faq)
{
如果(q.IsAccepting)
{
dfa.IsAccepting=真;
dfa.AcceptSymbol=q.AcceptSymbol;
打破
}
}
对于(变量i=0;i 如果(trns.Key.Key问题到底是什么?现在,这是一个广泛的调试帮助请求。为了让它成为主题,您需要为工作和非工作情况提供示例输入和预期与实际输出。这将有助于缩小焦点。这似乎是一个向您的解决方案添加单元测试项目的好时机n并构建一套完整的单元测试。当您尝试拨入范围处理时,我可以看到回归的可能性。这也将帮助其他人理解它应该做什么。抱歉,我没有说得更清楚。这不是调试问题。我希望是。这甚至是我处理的方式的问题。只是错了。我不知道是什么问题正确的方法是,我只知道有一个。问题是“一个人究竟如何用范围而不是单个字符来构造powerset?”