Пример #1
0
		public static void Write(DfaState start, string fileName)
		{
			var dfa = new Dfa(start);

			XmlSerializer xs = new XmlSerializer(typeof(Dfa));
			using (Stream s = File.Create(fileName))
				xs.Serialize(s, dfa);
		}
Пример #2
0
		public void ParseDeclaration(DfaState dfa)
		{
			_varibalesTree = new VariableTreeItem("root");

			dfa.ForEachNR((state) =>
			{
				_varibalesTree.AddVariables(state);
			});
		}
Пример #3
0
		private void GenerateParseMethod(DfaState dfa, int errorState)
		{
			_main.WriteLine("partial void OnBeforeParse();");
			_main.WriteLine("partial void OnAfterParse();");

			_main.WriteLine("#region int Parse(..)");

			_main.WriteLine("public int Parse(byte[] bytes, int offset, int length)");
			_main.WriteLine("{");
			_main.WriteLine("OnBeforeParse();");

			if (dfa != null)
			{
				_main.WriteLine("int i = offset;");

				GenerateSwitch(dfa, errorState, SwitchMode.JumpOnly);

				//_main.WriteLine("if (state == State{0})", errorState);
				//_main.WriteLine("goto exit1;");

				_main.WriteLine("i++;");

				_main.WriteLine("int end = offset + length;");
				_main.WriteLine("for( ; i < end; i++)");
				_main.WriteLine("{");

				GenerateSwitch(dfa, errorState, SwitchMode.ActionJump);

				_main.WriteLine("}");

				GenerateSwitch(dfa, errorState, SwitchMode.ActionOnly);

				_main.WriteLine("exit1: ;");
				_main.WriteLine("OnAfterParse();");
				_main.WriteLine("return i - offset;");
			}
			else
			{
				_main.WriteLine("OnAfterParse();");
				_main.WriteLine("return 0;");
			}

			_main.WriteLine("}");

			_main.WriteLine("#endregion");
		}
Пример #4
0
		public static DfaState Intersect(DfaState dfa1, DfaState dfa2)
		{
			var list1 = new List<DfaState>();
			var list2 = new List<DfaState>();

			dfa1.ForEachNR((state) => { list1.Add(state); });
			dfa2.ForEachNR((state) => { list2.Add(state); });

			var states = new DfaState[list1.Count, list2.Count];

			for (int i = 0; i < list1.Count; i++)
				for (int j = 0; j < list2.Count; j++)
				{
					list1[i].Index = i;
					list2[j].Index = j;

					var ids = new List<int>();
					ids.AddRange(list1[i].NfaIds);
					ids.AddRange(list2[j].NfaIds);

					states[i, j] = new DfaState(ids.ToArray());
				}

			for (int i = 0; i < list1.Count; i++)
				for (int j = 0; j < list2.Count; j++)
				{
					var state = states[i, j];

					for (int ch = 0; ch <= 255; ch++)
					{
						byte key = (byte)ch;
						var node1 = list1[i].Transition[key];
						var node2 = list2[j].Transition[key];

						if (node1 != null && node2 != null)
							state.AddTransition(key, states[node1.Index, node2.Index]);
					}
				}

			var result = states[0, 0];
			//result.Minimize4(false);

			return result;
		}
Пример #5
0
		private static bool IsTransitedToSameSets4(DfaState state1, DfaState state2)
		{
			for (int ch = 0; ch <= byte.MaxValue; ch++)
			{
				if (state1.Transition[ch] != null)
				{
					if (state2.Transition[ch] != null)
					{
						if (state1.Transition[ch].SetId != state2.Transition[ch].SetId)
							return false;
					}
					else
						return false;
				}
				else
				{
					if (state2.Transition[ch] != null)
						return false;
				}
			}

			return true;
		}
Пример #6
0
		private static void CopyToSet(List<HashSet<DfaState>> sets, DfaState state, int setId)
		{
			while (sets.Count <= setId)
				sets.Add(new HashSet<DfaState>());

			state.SetId = setId;
			sets[state.SetId].Add(state);
		}
Пример #7
0
		private static bool IsTransitedToSameSets(DfaState state1, DfaState state2)
		{
			for (int ch = 0; ch <= byte.MaxValue; ch++)
				if (state1.GetTransitedSetId(ch) != state2.GetTransitedSetId(ch))
					return false;
			return true;
		}
Пример #8
0
		private void GenerateTables(DfaState dfa, int errorState)
		{
			if (dfa != null)
			{
				_main.WriteLine("#region States Tables");

				dfa.ForEachNR((state) =>
					{
						_main.WriteLine("private static int[] table{0};", state.Index);

						int next;
						DfaState nextState;
						for (int i = 0; i <= byte.MaxValue; i++)
						{
							next = errorState;
							nextState = state.Transition[i];
							if (nextState != null)
								next = nextState.Index;
							_table.Write((Int32)next);
						}
					});

				_main.WriteLine("#endregion");
			}
			else
				_main.WriteLine("// NO TABLES");
		}
Пример #9
0
        public void Generate(string filename, string classname, string namespace1, DfaState dfa, bool writeDfaFile)
        {
            using (_main = File.CreateText(filename + ".cs"))
            using (_table = new BinaryWriter((writeDfaFile) ?
                    new DeflateStream(
                        new BufferedStream(File.Create(namespace1 + ".dfa"), 65536), CompressionMode.Compress)
                    : Stream.Null))
            {

                if (dfa != null)
                    ParseDeclaration(dfa);

                WriteIfOptimizedStatement();

                _main.WriteLine("using System;");
                _main.WriteLine("using System.Text;");
                _main.WriteLine("using System.IO;");
                _main.WriteLine("using System.IO.Compression;");
                _main.WriteLine("using Base.Message;");
                if (_optimized == OptimizationMode.IndexedArray)
                    _main.WriteLine("using System.Threading;");
                _main.WriteLine();

                _main.WriteLine("namespace {0}", namespace1);
                _main.WriteLine("{");

                GenerateEnums(_varibalesTree, new List<string>());
                GenerateGlobalStructs(_varibalesTree, new List<string>());

                _main.WriteLine("public partial class {0}", classname);
                if (_optimized == OptimizationMode.IndexedArray)
                    _main.WriteLine(":IDisposable");
                _main.WriteLine("{");

                if (_optimized == OptimizationMode.SingleStatic)
                {
                    _main.WriteLine("[ThreadStatic]");
                    _main.WriteLine("public static byte[] Bytes;");

                    _main.WriteLine("/// <summary>");
                    _main.WriteLine("/// ATT: must be call each time when thread get control for optimized version");
                    _main.WriteLine("/// </summary>");
                    _main.WriteLine("public void SetArray(byte[] bytes)");
                    _main.WriteLine("{");
                    _main.WriteLine("Bytes=bytes;");
                    _main.WriteLine("}");
                }
                else if (_optimized == OptimizationMode.IndexedArray)
                {
                    _main.WriteLine("internal static byte[][] bytes = new byte[32][];");
                    _main.WriteLine("private int index;");

                    _main.WriteLine("public void SetArray(byte[] bytes1)");
                    _main.WriteLine("{");
                    _main.WriteLine("lock (sync)");
                    _main.WriteLine("bytes[index]=bytes1;");
                    _main.WriteLine("}");

                    _main.WriteLine("public {0}()", classname);
                    _main.WriteLine("{");
                    _main.WriteLine("AcquireArraySlot();");
                    _main.WriteLine("}");

                    _main.WriteLine("~{0}()", classname);
                    _main.WriteLine("{");
                    _main.WriteLine("ReleaseArraySlot();");
                    _main.WriteLine("}");

                    _main.WriteLine("public void Dispose()");
                    _main.WriteLine("{");
                    _main.WriteLine("ReleaseArraySlot();");
                    _main.WriteLine("GC.SuppressFinalize(this);");
                    _main.WriteLine("index=-1;");
                    _main.WriteLine("}");
                }

                _main.WriteLine("public bool Final;");
                _main.WriteLine("public bool IsFinal { get { return Final; }}");

                _main.WriteLine("public bool Error;");
                _main.WriteLine("public bool IsError { get { return Error; }}");

                _main.WriteLine("private int state;");
                _main.WriteLine("private int boolExPosition;");

                GenerateVariables(_varibalesTree, true);

                int countStates = 0;
                if (dfa != null)
                    dfa.ForEachNR((state) => { state.Index = countStates++; });
                else
                    countStates = 1;

                GenerateStateConsts(countStates + 1);
                GenerateTables(dfa, countStates);
                GenerateLoadFunction3(countStates, dfa == null, namespace1, classname);
                GenerateParseMethod(dfa, countStates);
                GenerateGetHexDigitFunction();

                _main.WriteLine("}");
                _main.WriteLine("}");

                _main.WriteLine("#endif");

                _main.Flush();
                _table.Flush();
            }
        }
Пример #10
0
        public static DfaState ToDfa3(this State start, out int dfaCount, bool showProgress)
        {
            DfaState dfaStart;

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            if (showProgress)
            {
                Console.WriteLine("NFA to DFA v.3 Converting...");
                Console.WriteLine("{0}", DateTime.Now);
            }

            var dfaStates = new Dictionary<int[], DfaState>(3000000, new IntArrayComparer());
            var dfaStatesSync = new object();
            var notMarkedStates = new Queue<DfaState>(500000);
            var notMarkedSync = new object();

            dfaStart = new DfaState(DfaState.GetNfaIds(start.Eclosure()));
            dfaStates.Add(dfaStart.NfaIds, dfaStart);
            notMarkedStates.Enqueue(dfaStart);

            Thread[] threads = new Thread[Environment.ProcessorCount * 2];

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(ToDfaThread);
                threads[i].Start(new ThreadParams(i == 0, showProgress, dfaStates,
                    dfaStatesSync, notMarkedStates, notMarkedSync));

                if (i == 0)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Thread.Sleep(1000);
                        if (threads[0].IsAlive == false)
                            break;
                    }
                    if (threads[0].IsAlive == false)
                        break;
                }
            }

            for (int i = 0; i < threads.Length; i++)
                if (threads[i] != null)
                    threads[i].Join();

            dfaCount = dfaStates.Count;
            if (showProgress)
            {
                stopwatch.Stop();
                Console.WriteLine("Done (States: {0}, Elapsed: {1})\t\t\t\t", dfaCount, stopwatch.Elapsed);
            }

            return dfaStart;
        }
Пример #11
0
		public Dfa(DfaState start)
		{
			this.start = start;
		}
Пример #12
0
		private void ReadStates(XmlReader reader, Dictionary<int, IMark> marks)
		{
			var states = new Dictionary<int, DfaState>();

			reader.ReadStartElement("States");
			reader.MoveToElement();

			while (reader.IsStartElement("State"))
			{
				int index = Convert.ToInt32(reader.GetAttribute("id"));
				var state = GetOrCreateState(states, index);

				if (states.Count == 1)
					start = state;

				reader.ReadStartElement("State");

				ReadStateTransition(reader, state, states);
				ReadStateMarks(reader, state, marks);

				reader.ReadEndElement(); // State

				reader.MoveToElement();
			}

			reader.ReadEndElement();
		}
Пример #13
0
        private static void ToDfaThread(Object stateInfo)
        {
            var p = stateInfo as ThreadParams;

            int i        = 0;
            int time     = Environment.TickCount;
            var allChars = new bool[byte.MaxValue + 1];

            long memStart = 0;

            if (p.Main)
            {
                GC.Collect();
                GC.WaitForFullGCComplete();
                GC.WaitForPendingFinalizers();
                memStart = GC.GetTotalMemory(true);
            }
            else
            {
                Thread.Sleep(20000);
            }

            int memPerState = 0;

            for (; ; i++)
            {
                DfaState t = null;
                lock (p.NotMarkedSync)
                {
                    if (p.NotMarkedStates.Count <= 0)
                    {
                        break;
                    }
                    t = p.NotMarkedStates.Dequeue();
                }

                if (p.ShowProgress && (i % 100) == 0)
                {
                    if ((i % 25000) == 0 && i > 0)
                    {
                        memPerState = (int)((GC.GetTotalMemory(true) - memStart) / (long)p.DfaStates.Count);
                    }
                    int time1 = Environment.TickCount;
                    Console.Write("{2}\t({3} ms)\t\t{0} ({4} b)\t\t{1}  ", p.DfaStates.Count, p.NotMarkedStates.Count, p.DfaStates.Count - p.NotMarkedStates.Count, time1 - time, memPerState);
                    Console.Write("\r");
                    time = time1;
                }

                if (t != null)
                {
                    for (int j = 0; j <= byte.MaxValue; j++)
                    {
                        allChars[j] = false;
                    }
                    foreach (var nfaState in t.NfaStates)
                    {
                        nfaState.Transition.ForEachNotNullKeys((nb) => { allChars[(int)nb] = true; });
                    }

                    for (int j = 0; j <= byte.MaxValue; j++)
                    {
                        if (allChars[j])
                        {
                            byte char1 = (byte)j;

                            var uUnique = t.NfaStates.GetSortedMoveEclosureIds(char1);

                            DfaState u;
                            lock (p.DfaStatesSync)
                            {
                                if (p.DfaStates.TryGetValue(uUnique, out u) == false)
                                {
                                    u = new DfaState(uUnique);
                                    p.DfaStates.Add(uUnique, u);
                                    lock (p.NotMarkedSync)
                                        p.NotMarkedStates.Enqueue(u);
                                }
                            }

                            t.AddTransition(char1, u);
                        }
                    }

                    if (p.Main)
                    {
                        if (i % 10000 == 0)
                        {
                            GC.Collect();
                            GC.WaitForFullGCComplete();
                            GC.WaitForPendingFinalizers();
                        }
                    }
                }
            }
        }
Пример #14
0
        public static DfaState ToDfa3(this State start, out int dfaCount, bool showProgress)
        {
            DfaState dfaStart;

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            if (showProgress)
            {
                Console.WriteLine("NFA to DFA v.3 Converting...");
                Console.WriteLine("{0}", DateTime.Now);
            }

            var dfaStates       = new Dictionary <int[], DfaState>(3000000, new IntArrayComparer());
            var dfaStatesSync   = new object();
            var notMarkedStates = new Queue <DfaState>(500000);
            var notMarkedSync   = new object();

            dfaStart = new DfaState(DfaState.GetNfaIds(start.Eclosure()));
            dfaStates.Add(dfaStart.NfaIds, dfaStart);
            notMarkedStates.Enqueue(dfaStart);

            Thread[] threads = new Thread[Environment.ProcessorCount * 2];

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(ToDfaThread);
                threads[i].Start(new ThreadParams(i == 0, showProgress, dfaStates,
                                                  dfaStatesSync, notMarkedStates, notMarkedSync));

                if (i == 0)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Thread.Sleep(1000);
                        if (threads[0].IsAlive == false)
                        {
                            break;
                        }
                    }
                    if (threads[0].IsAlive == false)
                    {
                        break;
                    }
                }
            }

            for (int i = 0; i < threads.Length; i++)
            {
                if (threads[i] != null)
                {
                    threads[i].Join();
                }
            }

            dfaCount = dfaStates.Count;
            if (showProgress)
            {
                stopwatch.Stop();
                Console.WriteLine("Done (States: {0}, Elapsed: {1})\t\t\t\t", dfaCount, stopwatch.Elapsed);
            }

            return(dfaStart);
        }
Пример #15
0
        public static DfaState ToDfa2(this State start, out int dfaCount, bool showProgress)
        {
            DfaState dfaStart;

            if (showProgress)
            {
                Console.WriteLine("NFA to DFA v.2 Converting...");
                Console.WriteLine("{0}", DateTime.Now);
            }

            var dfaStates       = new Dictionary <int[], DfaState>(3000000, new IntArrayComparer());
            var notMarkedStates = new Queue <DfaState>(500000);

            dfaStart = new DfaState(DfaState.GetNfaIds(start.Eclosure()));
            dfaStates.Add(dfaStart.NfaIds, dfaStart);
            notMarkedStates.Enqueue(dfaStart);

            int i        = 0;
            int time     = Environment.TickCount;
            var allChars = new bool[byte.MaxValue + 1];

            GC.Collect();
            long memStart = GC.GetTotalMemory(true);

            while (notMarkedStates.Count > 0)
            {
                DfaState t = notMarkedStates.Dequeue();

                if (showProgress && i++ % 100 == 0)
                {
                    int memPerState = (int)((GC.GetTotalMemory(true) - memStart) / (long)dfaStates.Count);
                    int time1       = Environment.TickCount;
                    Console.Write("{2}\t({3} ms)\t\t{0} ({4} b)\t\t{1}\t\t", dfaStates.Count, notMarkedStates.Count, dfaStates.Count - notMarkedStates.Count, time1 - time, memPerState);
                    Console.Write("\r");
                    time = time1;
                }

                if (t != null)
                {
                    for (int j = 0; j <= byte.MaxValue; j++)
                    {
                        allChars[j] = false;
                    }
                    foreach (var nfaState in t.NfaStates)
                    {
                        nfaState.Transition.ForEachNotNullKeys((nb) => { allChars[(int)nb] = true; });
                    }

                    for (int j = 0; j <= byte.MaxValue; j++)
                    {
                        if (allChars[j])
                        {
                            byte char1 = (byte)j;

                            var states = t.NfaStates.GetMove2(char1).GetEclosure();

                            var uUnique = DfaState.GetNfaIds(states);

                            DfaState u;
                            if (dfaStates.TryGetValue(uUnique, out u) == false)
                            {
                                u = new DfaState(uUnique);
                                dfaStates.Add(uUnique, u);
                                notMarkedStates.Enqueue(u);
                            }

                            t.AddTransition(char1, u);
                        }
                    }

                    if (i % 1000 == 0)
                    {
                        GC.Collect();
                        GC.WaitForFullGCComplete();
                        GC.WaitForPendingFinalizers();
                    }
                }
            }

            dfaCount = dfaStates.Count;

            if (showProgress)
            {
                Console.WriteLine("Done ({0})\t\t\t\t\t\t\t\t", dfaCount);
            }

            return(dfaStart);
        }
Пример #16
0
		private static DfaState GetOrCreateState(Dictionary<int, DfaState> states, int index)
		{
			DfaState state;

			if (states.TryGetValue(index, out state) == false)
				states.Add(index, state = new DfaState());

			return state;
		}
Пример #17
0
		public void SetTransition(byte char1, DfaState state)
		{
			if (_trasition[char1].SetId != state.SetId)
				throw new InvalidProgramException("Invlid Transition");
			_trasition[char1] = state;
		}
Пример #18
0
		private static void ReadStateTransition(XmlReader reader, DfaState state, Dictionary<int, DfaState> states)
		{
			reader.MoveToElement();

			if (reader.IsEmptyElement)
			{
				reader.Read();
			}
			else
			{
				reader.ReadStartElement("Transitions");
				reader.MoveToElement();

				while (reader.IsStartElement("Transition"))
				{
					int character = Convert.ToInt32(reader.GetAttribute("char"));
					int nextIndex = Convert.ToInt32(reader.GetAttribute("stateId"));

					state.AddTransition((byte)character, GetOrCreateState(states, nextIndex));

					reader.Read();
					reader.MoveToElement();
				}

				reader.ReadEndElement();
			}
		}
Пример #19
0
        public static DfaState ToDfa2(this State start, out int dfaCount, bool showProgress)
        {
            DfaState dfaStart;

            if (showProgress)
            {
                Console.WriteLine("NFA to DFA v.2 Converting...");
                Console.WriteLine("{0}", DateTime.Now);
            }

            var dfaStates = new Dictionary<int[], DfaState>(3000000, new IntArrayComparer());
            var notMarkedStates = new Queue<DfaState>(500000);

            dfaStart = new DfaState(DfaState.GetNfaIds(start.Eclosure()));
            dfaStates.Add(dfaStart.NfaIds, dfaStart);
            notMarkedStates.Enqueue(dfaStart);

            int i = 0;
            int time = Environment.TickCount;
            var allChars = new bool[byte.MaxValue + 1];

            GC.Collect();
            long memStart = GC.GetTotalMemory(true);

            while (notMarkedStates.Count > 0)
            {
                DfaState t = notMarkedStates.Dequeue();

                if (showProgress && i++ % 100 == 0)
                {
                    int memPerState = (int)((GC.GetTotalMemory(true) - memStart) / (long)dfaStates.Count);
                    int time1 = Environment.TickCount;
                    Console.Write("{2}\t({3} ms)\t\t{0} ({4} b)\t\t{1}\t\t", dfaStates.Count, notMarkedStates.Count, dfaStates.Count - notMarkedStates.Count, time1 - time, memPerState);
                    Console.Write("\r");
                    time = time1;
                }

                if (t != null)
                {
                    for (int j = 0; j <= byte.MaxValue; j++)
                        allChars[j] = false;
                    foreach (var nfaState in t.NfaStates)
                        nfaState.Transition.ForEachNotNullKeys((nb) => { allChars[(int)nb] = true; });

                    for (int j = 0; j <= byte.MaxValue; j++)
                    {
                        if (allChars[j])
                        {
                            byte char1 = (byte)j;

                            var states = t.NfaStates.GetMove2(char1).GetEclosure();

                            var uUnique = DfaState.GetNfaIds(states);

                            DfaState u;
                            if (dfaStates.TryGetValue(uUnique, out u) == false)
                            {
                                u = new DfaState(uUnique);
                                dfaStates.Add(uUnique, u);
                                notMarkedStates.Enqueue(u);
                            }

                            t.AddTransition(char1, u);
                        }
                    }

                    if (i % 1000 == 0)
                    {
                        GC.Collect();
                        GC.WaitForFullGCComplete();
                        GC.WaitForPendingFinalizers();
                    }
                }
            }

            dfaCount = dfaStates.Count;

            if (showProgress)
                Console.WriteLine("Done ({0})\t\t\t\t\t\t\t\t", dfaCount);

            return dfaStart;
        }
Пример #20
0
		private static void ReadStateMarks(XmlReader reader, DfaState state, Dictionary<int, IMark> marks)
		{
			reader.MoveToContent();

			if (reader.IsEmptyElement)
			{
				reader.Read();
				state.SetMarks(new List<IMark>());
			}
			else
			{
				var stateMarks = new List<IMark>();

				reader.ReadStartElement("Marks");
				reader.MoveToElement();

				while (reader.IsStartElement("Id"))
				{
					reader.ReadStartElement();
					int markIndex = reader.ReadContentAsInt();
					reader.ReadEndElement();

					stateMarks.Add(marks[markIndex]);

					reader.MoveToElement();
				}

				state.SetMarks(stateMarks);

				reader.ReadEndElement();
			}
		}
Пример #21
0
        private static void ToDfaThread(Object stateInfo)
        {
            var p = stateInfo as ThreadParams;

            int i = 0;
            int time = Environment.TickCount;
            var allChars = new bool[byte.MaxValue + 1];

            long memStart = 0;
            if (p.Main)
            {
                GC.Collect();
                GC.WaitForFullGCComplete();
                GC.WaitForPendingFinalizers();
                memStart = GC.GetTotalMemory(true);
            }
            else
            {
                Thread.Sleep(20000);
            }

            int memPerState = 0;
            for (; ; i++)
            {
                DfaState t = null;
                lock (p.NotMarkedSync)
                {
                    if (p.NotMarkedStates.Count <= 0)
                        break;
                    t = p.NotMarkedStates.Dequeue();
                }

                if (p.ShowProgress && (i % 100) == 0)
                {
                    if ((i % 25000) == 0 && i > 0)
                        memPerState = (int)((GC.GetTotalMemory(true) - memStart) / (long)p.DfaStates.Count);
                    int time1 = Environment.TickCount;
                    Console.Write("{2}\t({3} ms)\t\t{0} ({4} b)\t\t{1}  ", p.DfaStates.Count, p.NotMarkedStates.Count, p.DfaStates.Count - p.NotMarkedStates.Count, time1 - time, memPerState);
                    Console.Write("\r");
                    time = time1;
                }

                if (t != null)
                {
                    for (int j = 0; j <= byte.MaxValue; j++)
                        allChars[j] = false;
                    foreach (var nfaState in t.NfaStates)
                        nfaState.Transition.ForEachNotNullKeys((nb) => { allChars[(int)nb] = true; });

                    for (int j = 0; j <= byte.MaxValue; j++)
                    {
                        if (allChars[j])
                        {
                            byte char1 = (byte)j;

                            var uUnique = t.NfaStates.GetSortedMoveEclosureIds(char1);

                            DfaState u;
                            lock (p.DfaStatesSync)
                            {
                                if (p.DfaStates.TryGetValue(uUnique, out u) == false)
                                {
                                    u = new DfaState(uUnique);
                                    p.DfaStates.Add(uUnique, u);
                                    lock (p.NotMarkedSync)
                                        p.NotMarkedStates.Enqueue(u);
                                }
                            }

                            t.AddTransition(char1, u);
                        }
                    }

                    if (p.Main)
                    {
                        if (i % 10000 == 0)
                        {
                            GC.Collect();
                            GC.WaitForFullGCComplete();
                            GC.WaitForPendingFinalizers();
                        }
                    }
                }
            }
        }
Пример #22
0
		public static State Substract(State nfa1, State nfa2)
		{
			nfa1.FindEnd().AddMark(Marks.Service1);
			nfa2.FindEnd().AddMark(Marks.Service2);

			int count;
			var dfa1 = nfa1.ToDfa3(out count, false);
			var dfa2 = nfa2.ToDfa3(out count, false);

			dfa1.Minimize(false);
			dfa2.Minimize(false);

			var error = new DfaState(new[] { new State().Id, });
			for (int i = 0; i <= 255; i++)
				error.AddTransition((byte)i, error);
			dfa2.ForEachNR((state) =>
			{
				if (state != error)
				{
					for (int i = 0; i <= 255; i++)
					{
						byte key = (byte)i;
						if (state.Transition[i] == null)
							state.AddTransition(key, error);
					}
				}
			});



			var nfa3 = DfaIntersect.Intersect(dfa1, dfa2).ToNfa2();


			var ends = new List<State>();
			nfa3.ForEach((state) =>
				{
					bool s1 = false, s2 = false;

					state.RemoveAllMarks((mark) =>
					{
						if (mark.Mark == Marks.Service1)
							s1 = true;

						if (mark.Mark == Marks.Service2)
							s2 = true;

						return mark.Mark == Marks.Service1 || mark.Mark == Marks.Service2;
					});

					if (s1 == true && s2 == false)
						ends.Add(state);
				});


			var end = new State();
			foreach (var item in ends)
				item.Transition.Add(Epsilon, end);


			return nfa3;
		}
Пример #23
0
		private void GenerateParseMethod(DfaState dfa, int errorState)
		{
			_main.WriteLine("partial void OnBeforeParse();");
			_main.WriteLine("partial void OnAfterParse();");

			_main.WriteLine("#region int Parse(..)");

			_main.WriteLine("public bool ParseAll(ArraySegment<byte> data)");
			_main.WriteLine("{");
			_main.WriteLine("return ParseAll(data.Array, data.Offset, data.Count);");
			_main.WriteLine("}");

			_main.WriteLine("public bool ParseAll(byte[] bytes, int offset, int length)");
			_main.WriteLine("{");
			_main.WriteLine("int parsed = 0;");
			_main.WriteLine("do");
			_main.WriteLine("{");
			_main.WriteLine("Final = false;");
			_main.WriteLine("parsed += Parse(bytes, offset + parsed, length - parsed);");
			_main.WriteLine("} while (parsed < length && IsFinal);");
			_main.WriteLine("return IsFinal;");
			_main.WriteLine("}");

			_main.WriteLine("public int Parse(ArraySegment<byte> data)");
			_main.WriteLine("{");
			_main.WriteLine("return Parse(data.Array, data.Offset, data.Count);");
			_main.WriteLine("}");

			_main.WriteLine("public int Parse(byte[] bytes, int offset, int length)");
			_main.WriteLine("{");
			_main.WriteLine("OnBeforeParse();");

			if (dfa != null)
			{
				_main.WriteLine("int i = offset;");

				GenerateSwitch(dfa, errorState, SwitchMode.JumpOnly);

				//_main.WriteLine("if (state == State{0})", errorState);
				//_main.WriteLine("goto exit1;");

				_main.WriteLine("i++;");

				_main.WriteLine("int end = offset + length;");
				_main.WriteLine("for( ; i < end; i++)");
				_main.WriteLine("{");

				GenerateSwitch(dfa, errorState, SwitchMode.ActionJump);

				_main.WriteLine("}");

				GenerateSwitch(dfa, errorState, SwitchMode.ActionOnly);

				_main.WriteLine("exit1: ;");
				_main.WriteLine("OnAfterParse();");
				_main.WriteLine("return i - offset;");
			}
			else
			{
				_main.WriteLine("OnAfterParse();");
				_main.WriteLine("return 0;");
			}

			_main.WriteLine("}");

			_main.WriteLine("#endregion");
		}
Пример #24
0
		public bool IsSame(DfaState state)
		{
			if (AllMarks.Count != state.AllMarks.Count)
				return false;

			for (int i = 0; i < AllMarks.Count; i++)
				if (AllMarks[i].IsSame(state.AllMarks[i]) == false)
					return false;

			return true;
		}
Пример #25
0
		private void GenerateSwitch(DfaState dfa, int errorState, SwitchMode mode)
		{
			_main.WriteLine("switch(state)");
			_main.WriteLine("{");

			dfa.ForEachNR((state) =>
			{
				_main.WriteLine("case State{0}:", state.Index);

				if (mode == SwitchMode.ActionJump || mode == SwitchMode.ActionOnly)
				{
					foreach (var nfa1 in state.AllMarks)
					{
						if (nfa1.Mark == Marks.ResetRange)
						{
							var name = GetVarname(nfa1.Name, "");
							// #1 Do NOT use SetDefaultValue, it clears bytes too -> wrong!
							// #1 Should to create special method for this
							// #2 Ok for IndexArray optimimized 
							_main.WriteLine("{0}." + GetSetDefauleValueCall() + ";", name);
						}

						if (nfa1.Mark == Marks.ResetRangeIfInvalid)
						{
							var name = GetVarname(nfa1.Name, "");
							_main.WriteLine("if({0}.End <0) {0}.Begin = int.MinValue;", name);
						}

						if (nfa1.Mark == Marks.Custom)
						{
							var name = GetVarname(nfa1.Name, "");
							_main.WriteLine(nfa1.Value.Replace("Var", name));
						}
					}

					foreach (var nfa1 in state.AllMarks)//.NfaStates)
					{
						if (nfa1.Mark == Marks.Count)
							_main.WriteLine("{0}++;", GetVarname(nfa1.Name, "Count."));
					}

					foreach (var mark in state.AllMarks)
					{
						if (mark.Mark == Marks.ContinueRange)
						{
							var ifv = GetCountComparation(RemoveExtraInfo(mark.Name));
							if (ifv != "")
								ifv += " && ";
							_main.WriteLine("if({1}{0}.End == i-1) {0}.End = i;", GetVarname(mark.Name, ""), ifv);
						}
					}

					foreach (var nfa1 in state.AllMarks)//.NfaStates)
					{
						switch (nfa1.Mark)
						{
							case Marks.BeginRange:
							case Marks.EndRange:
							case Marks.EndRangeIfInvalid:

								var varName = GetVarname(nfa1.Name, "") +
									((nfa1.Mark == Marks.BeginRange) ? ".Begin" : ".End");

								var condition = GetCountComparation(RemoveExtraInfo(nfa1.Name));
								if (nfa1.Mark != Marks.EndRange)
								{
									if (condition != "")
										condition += " && ";
									condition = varName + " < 0";
								}

								if (condition != "")
									_main.Write("if({0})", condition);

								_main.Write("{0} = i", varName);

								if (nfa1.Offset != 0)
									_main.Write("{0} {1}", (nfa1.Offset > 0) ? "+" : "-", Math.Abs(nfa1.Offset));

								_main.WriteLine(";");

								break;


							case Marks.BoolEx:
								_main.WriteLine("boolExPosition = i;");
								goto case Marks.Bool;
							case Marks.Bool:
								_main.WriteLine("{0} = true;", GetVarname(nfa1.Name, ""));
								break;

							case Marks.BoolExNot:
								_main.WriteLine("if(boolExPosition == i-1) {0} = false;", GetVarname(nfa1.Name, ""));
								break;


							case Marks.Final:
								_main.WriteLine("Final = true;");
								break;
						}
					}

					//if (mode == SwitchMode.ActionJump || mode == SwitchMode.ActionOnly)
					//{
					if (state.HasMarks)
					{
						foreach (var decimal1 in state.Decimals)
							_main.WriteLine("{0} = ({0} << 1) * 5 + bytes[i - 1] - 48;", GetVarname(decimal1.Name, ""));

						foreach (var hex1 in state.Hexes)
							_main.WriteLine("{0} = ({0} << 4) + AsciiCodeToHex[bytes[i - 1]];", GetVarname(hex1.Name, ""));
					}
					//}

					if (state.Consts.Count > 0)
					{
						foreach (var pair in state.ConstNameValues)
						{
							var ifv = GetCountComparation(RemoveExtraInfo(pair.Key));

							if (ifv != "")
								_main.Write("if(" + ifv + ") ");

							_main.WriteLine("{0} = {1}s.{2};",
								AddCountPrefix(RemoveExtraInfo(pair.Key)),
								RemoveBrackets(VariableInfo.GetShortName(pair.Key)),
								pair.Value);
						}
					}
				}

				if (state.IsFinal)
				{
					if (mode == SwitchMode.JumpOnly)
					{
						_main.WriteLine("state = table{0}[bytes[i]];", state.Index);
						_main.WriteLine("break;");
					}
					else
					{
						_main.WriteLine("goto exit1;");
					}
				}
				else
				{
					if (mode == SwitchMode.ActionJump || mode == SwitchMode.JumpOnly)
						_main.WriteLine("state = table{0}[bytes[i]];", state.Index);
					_main.WriteLine("break;");
				}

			}); // ForEach state

			_main.WriteLine("case State{0}:", errorState);
			if (mode == SwitchMode.ActionJump || mode == SwitchMode.ActionOnly)
				_main.WriteLine("i--;");
			_main.WriteLine("Error = true;");
			_main.WriteLine("goto exit1;");

			_main.WriteLine("}");
		}
Пример #26
0
		public static DfaState Intersect(DfaState start1, DfaState start2)
		{
			throw new NotImplementedException();
		}
Пример #27
0
		private static void PrepareArrays(ref DfaState[] firsts, ref int[] ids, int id)
		{
			if (firsts == null)
			{
				firsts = new DfaState[512];
				ids = new int[512];
			}
			else
			{
				if (id > firsts.Length)
				{
					firsts = new DfaState[id + 8192];
					ids = new int[id + 8192];
				}

				Array.Clear(firsts, 0, firsts.Length);
				Array.Clear(ids, 0, ids.Length);
			}
		}
Пример #28
0
		private static void ForEach(DfaState state, HashSet<DfaState> proccessed, Action<DfaState> action)
		{
			action(state);

			proccessed.Add(state);

			for (int i = 0; i <= byte.MaxValue; i++)
				if (state._trasition[i] != null)
				{
					if (proccessed.Contains(state._trasition[i]) == false)
						ForEach(state._trasition[i], proccessed, action);
				}
		}
Пример #29
0
		private static void CreateMiniDfa4(DfaState start, List<DfaState> states, IntSet sets)
		{
			var miniStates = new DfaState[sets.Count];

			for (int i = 0; i < sets.Count; i++)
			{
				if (i == start.SetId)
					miniStates[i] = start;
				else
					miniStates[i] = sets.GetFirst(i);
			}

			foreach (var state in miniStates)
			{
				for (int ch = 0; ch <= byte.MaxValue; ch++)
				{
					int transitedSetId = state.GetTransitedSetId(ch);
					if (transitedSetId >= 0)
						state.SetTransition((byte)ch, miniStates[transitedSetId]);
					else
						if (state.Transition[ch] != null)
							throw new InvalidProgramException();
				}
			}
		}
Пример #30
0
		public void AddTransition(byte char1, DfaState state)
		{
			if (_trasition[char1] != null)
				throw new InvalidProgramException("Transition already exist in DFA state");
			_trasition[char1] = state;
		}
Пример #31
0
		public void AddVariables(DfaState state)
		{
			foreach (var mark in state.AllMarks)
				if (string.IsNullOrEmpty(mark.Name) == false)
					GetItem(mark.Name).AddVariable(mark);
		}
Пример #32
0
        public static State Substract(State nfa1, State nfa2)
        {
            nfa1.FindEnd().AddMark(Marks.Service1);
            nfa2.FindEnd().AddMark(Marks.Service2);

            int count;
            var dfa1 = nfa1.ToDfa3(out count, false);
            var dfa2 = nfa2.ToDfa3(out count, false);

            dfa1.Minimize(false);
            dfa2.Minimize(false);

            var error = new DfaState(new[] { new State().Id, });

            for (int i = 0; i <= 255; i++)
            {
                error.AddTransition((byte)i, error);
            }
            dfa2.ForEachNR((state) =>
            {
                if (state != error)
                {
                    for (int i = 0; i <= 255; i++)
                    {
                        byte key = (byte)i;
                        if (state.Transition[i] == null)
                        {
                            state.AddTransition(key, error);
                        }
                    }
                }
            });



            var nfa3 = DfaIntersect.Intersect(dfa1, dfa2).ToNfa2();


            var ends = new List <State>();

            nfa3.ForEach((state) =>
            {
                bool s1 = false, s2 = false;

                state.RemoveAllMarks((mark) =>
                {
                    if (mark.Mark == Marks.Service1)
                    {
                        s1 = true;
                    }

                    if (mark.Mark == Marks.Service2)
                    {
                        s2 = true;
                    }

                    return(mark.Mark == Marks.Service1 || mark.Mark == Marks.Service2);
                });

                if (s1 == true && s2 == false)
                {
                    ends.Add(state);
                }
            });


            var end = new State();

            foreach (var item in ends)
            {
                item.Transition.Add(Epsilon, end);
            }


            return(nfa3);
        }