我正在尝试使用Powerset构造并使用unicode引入的大范围

时间:2020-01-26 07:21:30

标签: c# dfa nfa

我使用单个字符值在DFA上成功实现了powerset构造(霍夫曼最小化),但是我修改了FA状态以能够处理unicode范围。 现在他们看起来像这样:

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;

我的字符存储为UTF-32值整数,因此我不必处理代理 每个输入转换都可以采用一个范围,其起始和终止UTF-32值被编码为键值对

我已经修改了ToDfa()例程(以前仅适用于单个值)以使用范围,但是它无法正常工作。 NormalizeSortedRangeList()接受键值对范围的预排序列表,并合并重叠范围(将多个重叠范围替换为包含所有重叠范围的单个范围)。 _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;
}

它可以工作,但不是真的。问题是我的范围处理不好,但是我不知道怎么做。我已经将GraphViz点保存为我的状态,但是我不太了解它出了什么问题。

我尝试查看Brics(Java)和Fare(C#),但是我对它们的NFA-> DFA例程没有多大意义,该例程会创建一堆垃圾状态,然后将其删除。

我不太确定需要为您澄清什么,读者,因为我只是不太了解问题领域,因此请在评论中发表您对我的任何疑问,我会尽我所能。

此处https://github.com/codewitch-honey-crisis/Rolex/tree/master/Rolex/FA

有完整的来源

在动力装置构造中,您如何做范围?有几个库可以做到,我没有一个可以找到

1 个答案:

答案 0 :(得分:0)

我终于设法将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;
}


相关问题