Exemplo n.º 1
0
		public void min_dfa(Spec s)
		{
			this.set(s);
			this.minimize();
			this.reduce();
			this.reset();
		}
Exemplo n.º 2
0
		public DTrans(Spec s, Dfa dfa)
		{
			this.dtrans = new int[s.dtrans_ncols];
			this.label = s.dtrans_list.Count;
			this.accept = dfa.GetAccept();
			this.anchor = dfa.GetAnchor();
		}
Exemplo n.º 3
0
		public static Nfa NewNfa(Spec spec)
		{
			Nfa nfa = new Nfa();
			spec.nfa_states.Add(nfa);
			nfa.Edge = '';
			return nfa;
		}
Exemplo n.º 4
0
		private static int in_dstates(Spec s, Bunch bunch)
		{
			Dfa dfa;
			if (!s.dfa_sets.TryGetValue(bunch.GetNFABit(), out dfa))
			{
				return -1;
			}
			return dfa.Label;
		}
Exemplo n.º 5
0
		private static int add_to_dstates(Spec s, Bunch bunch)
		{
			Dfa dfa = Alloc.NewDfa(s);
			dfa.SetNFASet(new List<Nfa>(bunch.GetNFASet()));
			dfa.SetNFABit(new BitSet(bunch.GetNFABit()));
			dfa.SetAccept(bunch.GetAccept());
			dfa.SetAnchor(bunch.GetAnchor());
			dfa.ClearMarked();
			s.dfa_sets[dfa.GetNFABit()] = dfa;
			return dfa.Label;
		}
Exemplo n.º 6
0
		private static void computeClasses(Spec spec)
		{
			SimplifyNfa.original_charset_size = spec.dtrans_ncols;
			SimplifyNfa.ccls = new char[SimplifyNfa.original_charset_size];
			char c = '\u0001';
			BitSet bitSet = new BitSet();
			BitSet bitSet2 = new BitSet();
			Dictionary<char, char> dictionary = new Dictionary<char, char>();
			Console.WriteLine("Working on character classes.");
			for (int i = 0; i < spec.nfa_states.Count; i++)
			{
				Nfa nfa = spec.nfa_states[i];
				if (nfa.Edge != '�' && nfa.Edge != '')
				{
					bitSet.ClearAll();
					bitSet2.ClearAll();
					for (int j = 0; j < SimplifyNfa.ccls.Length; j++)
					{
						if ((int)nfa.Edge == j || (nfa.Edge == '￾' && nfa.GetCharSet().contains(j)))
						{
							bitSet.Set((int)SimplifyNfa.ccls[j], true);
						}
						else
						{
							bitSet2.Set((int)SimplifyNfa.ccls[j], true);
						}
					}
					bitSet.And(bitSet2);
					if (bitSet.GetLength() != 0)
					{
						dictionary.Clear();
						for (int k = 0; k < SimplifyNfa.ccls.Length; k++)
						{
							if (bitSet.Get((int)SimplifyNfa.ccls[k]) && ((int)nfa.Edge == k || (nfa.Edge == '￾' && nfa.GetCharSet().contains(k))))
							{
								char c2 = SimplifyNfa.ccls[k];
								if (!dictionary.ContainsKey(c2))
								{
									Dictionary<char, char> arg_14F_0 = dictionary;
									char arg_14F_1 = c2;
									char expr_14A = c;
									c = (char)(expr_14A + '\u0001');
									arg_14F_0.Add(arg_14F_1, expr_14A);
								}
								SimplifyNfa.ccls[k] = dictionary[c2];
							}
						}
					}
				}
			}
			SimplifyNfa.mapped_charset_size = (int)c;
		}
Exemplo n.º 7
0
		private static void make_dtrans(Spec s)
		{
			Console.WriteLine("Working on DFA states.");
			s.InitUnmarkedDFA();
			int num = s.state_rules.Length;
			s.state_dtrans = new int[num];
			for (int i = 0; i < num; i++)
			{
				Bunch bunch = new Bunch(s.state_rules[i]);
				bunch.e_closure();
				Nfa2Dfa.add_to_dstates(s, bunch);
				s.state_dtrans[i] = s.dtrans_list.Count;
				Dfa nextUnmarkedDFA;
				while ((nextUnmarkedDFA = s.GetNextUnmarkedDFA()) != null)
				{
					nextUnmarkedDFA.SetMarked();
					DTrans dTrans = new DTrans(s, nextUnmarkedDFA);
					for (int j = 0; j < s.dtrans_ncols; j++)
					{
						bunch.move(nextUnmarkedDFA, j);
						if (!bunch.IsEmpty())
						{
							bunch.e_closure();
						}
						int num2;
						if (bunch.IsEmpty())
						{
							num2 = -1;
						}
						else
						{
							num2 = Nfa2Dfa.in_dstates(s, bunch);
							if (num2 == -1)
							{
								num2 = Nfa2Dfa.add_to_dstates(s, bunch);
							}
						}
						dTrans.SetDTrans(j, num2);
					}
					s.dtrans_list.Add(dTrans);
				}
			}
			Console.WriteLine("");
		}
Exemplo n.º 8
0
/*
 * Function: Write
 * Description: High-level access function to module.
 */
        public void Write(Spec spec, StreamWriter o)
        {
            set(spec, o);

#if DEBUG
            Utility.assert(null != spec);
            Utility.assert(null != o);
#endif

#if OLD_DEBUG
            print_details();
#endif

            Header();
            Construct();
            Helpers();
            Driver();
            Footer();

            reset();
        }
Exemplo n.º 9
0
		internal static void simplify(Spec spec)
		{
			SimplifyNfa.computeClasses(spec);
			for (int i = 0; i < spec.nfa_states.Count; i++)
			{
				Nfa nfa = spec.nfa_states[i];
				if (nfa.Edge != '�' && nfa.Edge != '')
				{
					if (nfa.Edge == '￾')
					{
						CharSet charSet = new CharSet();
						charSet.map(nfa.GetCharSet(), SimplifyNfa.ccls);
						nfa.SetCharSet(charSet);
					}
					else
					{
						nfa.Edge = SimplifyNfa.ccls[(int)nfa.Edge];
					}
				}
			}
			spec.ccls_map = SimplifyNfa.ccls;
			spec.dtrans_ncols = SimplifyNfa.mapped_charset_size;
		}
Exemplo n.º 10
0
 internal static void simplify(Spec spec)
 {
     SimplifyNfa.computeClasses(spec);
     for (int i = 0; i < spec.nfa_states.Count; i++)
     {
         Nfa nfa = spec.nfa_states[i];
         if (nfa.Edge != '�' && nfa.Edge != '')
         {
             if (nfa.Edge == '￾')
             {
                 CharSet charSet = new CharSet();
                 charSet.map(nfa.GetCharSet(), SimplifyNfa.ccls);
                 nfa.SetCharSet(charSet);
             }
             else
             {
                 nfa.Edge = SimplifyNfa.ccls[(int)nfa.Edge];
             }
         }
     }
     spec.ccls_map     = SimplifyNfa.ccls;
     spec.dtrans_ncols = SimplifyNfa.mapped_charset_size;
 }
Exemplo n.º 11
0
		public static NfaPair NewNLPair(Spec spec)
		{
			NfaPair nfaPair = Alloc.NewNfaPair();
			nfaPair.end = Alloc.NewNfa(spec);
			nfaPair.start = Alloc.NewNfa(spec);
			Nfa start = nfaPair.start;
			start.Next = Alloc.NewNfa(spec);
			Nfa next = start.Next;
			next.Edge = '￾';
			next.SetCharSet(new CharSet());
			next.GetCharSet().add(10);
			next.Next = nfaPair.end;
			start.Sibling = Alloc.NewNfa(spec);
			Nfa sibling = start.Sibling;
			sibling.Edge = '\r';
			sibling.Next = Alloc.NewNfa(spec);
			Nfa next2 = sibling.Next;
			next2.Next = null;
			next2.Sibling = Alloc.NewNfa(spec);
			next2.Sibling.Edge = '\n';
			next2.Sibling.Next = nfaPair.end;
			return nfaPair;
		}
Exemplo n.º 12
0
/*
 * function: add_to_dstates
 * Description: Takes as input a CBunch with details of
 * a dfa state that needs to be created.
 * 1) Allocates a new dfa state and saves it in the appropriate Spec list
 * 2) Initializes the fields of the dfa state with the information in the CBunch.
 * 3) Returns index of new dfa.
 */
        private static int add_to_dstates(Spec s, Bunch bunch)
        {
            Dfa dfa;

#if DEBUG
            Utility.assert(null != bunch.GetNFASet());
            Utility.assert(null != bunch.GetNFABit());
            Utility.assert(null != bunch.GetAccept() || Spec.NONE == bunch.GetAnchor());
#endif

            /* Allocate, passing Spec so dfa label can be set. */
            dfa = Alloc.NewDfa(s);

            /* Initialize fields, including the mark field. */
            dfa.SetNFASet(new ArrayList(bunch.GetNFASet()));
            dfa.SetNFABit(new BitSet(bunch.GetNFABit()));
            dfa.SetAccept(bunch.GetAccept());
            dfa.SetAnchor(bunch.GetAnchor());
            dfa.ClearMarked();

#if OLD_DUMP_DEBUG
            Console.WriteLine("[Created new dfa_state #" + Int32.ToString(dfa.GetLabel()) + "]");
            dfa.dump();
#endif

            /* Register dfa state using BitSet in spec Hashtable. */
            s.dfa_sets[dfa.GetNFABit()] = dfa;

#if OLD_DUMP_DEBUG
            Console.Write("Registering set : ");
            Print_Set(dfa.GetNFASet());
            Console.WriteLine("");
#endif

            return(dfa.GetLabel());
        }
Exemplo n.º 13
0
		public static void CreateMachine(Gen cmg, Spec cms, Input cmi)
		{
			MakeNfa.spec = cms;
			MakeNfa.gen = cmg;
			MakeNfa.input = cmi;
			MakeNfa.spec.AddInitialState();
			int count = MakeNfa.spec.States.Count;
			MakeNfa.spec.state_rules = new List<Nfa>[count];
			for (int i = 0; i < count; i++)
			{
				MakeNfa.spec.state_rules[i] = new List<Nfa>();
			}
			MakeNfa.spec.nfa_start = MakeNfa.machine();
			count = MakeNfa.spec.nfa_states.Count;
			for (int i = 0; i < count; i++)
			{
				Nfa nfa = MakeNfa.spec.nfa_states[i];
				nfa.Label = i;
			}
			if (MakeNfa.spec.verbose)
			{
				Console.WriteLine("NFA comprised of " + (MakeNfa.spec.nfa_states.Count + 1) + " states.");
			}
		}
Exemplo n.º 14
0
 public static void MakeDFA(Spec s)
 {
     Nfa2Dfa.make_dtrans(s);
     Nfa2Dfa.free_nfa_states(s);
     Nfa2Dfa.free_dfa_states(s);
 }
Exemplo n.º 15
0
 private static void free_nfa_states(Spec s)
 {
     s.nfa_states  = null;
     s.nfa_start   = null;
     s.state_rules = null;
 }
Exemplo n.º 16
0
		private static void free_nfa_states(Spec s)
		{
			s.nfa_states = null;
			s.nfa_start = null;
			s.state_rules = null;
		}
Exemplo n.º 17
0
/*
 * Compute minimum set of character classes needed to disambiguate
 * edges.  We optimistically assume that every character belongs to
 * a single character class, and then incrementally split classes
 * as we see edges that require discrimination between characters in
 * the class.
 */
        static private void computeClasses(Spec spec)
        {
            original_charset_size = spec.dtrans_ncols;
            ccls = new int[original_charset_size]; // initially all zero.

            int       nextcls = 1;
            BitSet    clsA    = new BitSet();
            BitSet    clsB    = new BitSet();
            Hashtable h       = new Hashtable();

            Console.WriteLine("Working on character classes.");
            for (int index = 0; index < spec.nfa_states.Count; index++)
            {
                Nfa nfa = (Nfa)spec.nfa_states[index];
                if (nfa.GetEdge() == Nfa.EMPTY || nfa.GetEdge() == Nfa.EPSILON)
                {
                    continue;   // no discriminatory information.
                }
                clsA.ClearAll();
                clsB.ClearAll();
                for (int i = 0; i < ccls.Length; i++)
                {
                    if (nfa.GetEdge() == i ||         // edge labeled with a character
                        nfa.GetEdge() == Nfa.CCL &&
                        nfa.GetCharSet().contains(i)) // set of characters
                    {
                        clsA.Set(ccls[i], true);
                    }
                    else
                    {
                        clsB.Set(ccls[i], true);
                    }
                }

                /*
                 * now figure out which character classes we need to split.
                 */
                clsA.And(clsB); // split the classes which show up on both sides of edge
                if (clsA.GetLength() == 0)
                {
                    Console.Write(".");
                    continue;
                }
                Console.Write(":");

                /*
                 * and split them.
                 */
                h.Clear(); // h will map old to new class name
                for (int i = 0; i < ccls.Length; i++)
                {
                    if (clsA.Get(ccls[i])) // a split class
                    {
                        if (nfa.GetEdge() == i ||
                            nfa.GetEdge() == Nfa.CCL &&
                            nfa.GetCharSet().contains(i))
                        { // on A side
                            int split = ccls[i];
                            if (!h.ContainsKey(split))
                            {
                                h.Add(split, nextcls++); // make new class
#if DEBUG
                                Console.WriteLine("Adding char " + (nextcls - 1) + " split=" + split + " i=" + i);
#endif
                            }
                            ccls[i] = (int)h[split];
                        }
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine("NFA has " + nextcls + " distinct character classes.");
            mapped_charset_size = nextcls;
        }
Exemplo n.º 18
0
/*
 * Function: reset
 * Description: Clears member variables.
 */
        private void reset()
        {
            spec      = null;
            outstream = null;
        }
Exemplo n.º 19
0
 public static void Allocate_BOL_EOF(Spec s)
 {
     s.BOL = (char)s.dtrans_ncols++;
     s.EOF = (char)s.dtrans_ncols++;
 }
Exemplo n.º 20
0
		private void reset()
		{
			this.spec = null;
			this.group = null;
			this.ingroup = null;
		}
Exemplo n.º 21
0
 private void set(Spec s)
 {
     this.spec    = s;
     this.group   = null;
     this.ingroup = null;
 }
Exemplo n.º 22
0
 private void reset()
 {
     this.spec    = null;
     this.group   = null;
     this.ingroup = null;
 }
Exemplo n.º 23
0
 private void reset()
 {
     this.spec      = null;
     this.outstream = null;
 }
Exemplo n.º 24
0
		public static void MakeDFA(Spec s)
		{
			Nfa2Dfa.make_dtrans(s);
			Nfa2Dfa.free_nfa_states(s);
			Nfa2Dfa.free_dfa_states(s);
		}
Exemplo n.º 25
0
 private void set(Spec spec, IndentedTextWriter outstream, string inputFilePath)
 {
     this.spec          = spec;
     this.outstream     = outstream;
     this.inputFilePath = inputFilePath;
 }
Exemplo n.º 26
0
		public Gen(string filename, string outfile, int version)
		{
			this.init_flag = false;
			this.inputFilePath = Path.GetFullPath(filename);
			this.instream = File.OpenText(this.inputFilePath);
			this.outstream = new IndentedTextWriter(File.CreateText(outfile), "\t");
			this.outstream.Indent = 2;
			this.ibuf = new Input(this.instream);
			this.spec = new Spec();
			this.spec.Version = version;
			this.nfa2dfa = new Nfa2Dfa();
			this.minimize = new Minimize();
			this.makeNfa = new MakeNfa();
			this.emit = new Emit();
			this.init_flag = true;
		}
Exemplo n.º 27
0
		public static void Allocate_BOL_EOF(Spec s)
		{
			s.BOL = (char)s.dtrans_ncols++;
			s.EOF = (char)s.dtrans_ncols++;
		}
Exemplo n.º 28
0
		private void reset()
		{
			this.spec = null;
			this.outstream = null;
		}
Exemplo n.º 29
0
		public static Dfa NewDfa(Spec spec)
		{
			Dfa dfa = new Dfa(spec.dfa_states.Count);
			spec.dfa_states.Add(dfa);
			return dfa;
		}
Exemplo n.º 30
0
		private void set(Spec spec, IndentedTextWriter outstream, string inputFilePath)
		{
			this.spec = spec;
			this.outstream = outstream;
			this.inputFilePath = inputFilePath;
		}
Exemplo n.º 31
0
/*
 * Function: free_dfa_states
 */
//private void free_dfa_states()
        private static void free_dfa_states(Spec s)
        {
            s.dfa_states = null;
            s.dfa_sets   = null;
        }
Exemplo n.º 32
0
		public void Write(Spec spec, IndentedTextWriter o, string inputFilePath)
		{
			this.set(spec, o, inputFilePath);
			this.Header();
			this.Construct();
			this.Helpers();
			this.Driver();
			this.Footer();
			this.reset();
		}
Exemplo n.º 33
0
/*
 * Function: make_dtrans
 * Description: Creates uncompressed CDTrans transition table.
 */
//private void make_dtrans()
        private static void make_dtrans(Spec s)
        {
            Dfa dfa;
            int nextstate;

            Console.WriteLine("Working on DFA states.");

            /* Reference passing type and initializations. */
            s.InitUnmarkedDFA();

            Console.WriteLine("done init");
            /* Allocate mapping array. */
            int nstates = s.state_rules.Length;

            s.state_dtrans = new int[nstates];

            for (int istate = 0; istate < nstates; istate++)
            {
                /* Create start state and initialize fields. */

                Bunch bunch = new Bunch(s.state_rules[istate]);

                bunch.e_closure();
                add_to_dstates(s, bunch);

                s.state_dtrans[istate] = s.dtrans_list.Count;

                /* Main loop of DTrans creation. */
                while (null != (dfa = s.GetNextUnmarkedDFA()))
                {
                    Console.Write(".");
#if DEBUG
                    Utility.assert(!dfa.IsMarked());
#endif
                    /* Get first unmarked node, then mark it. */
                    dfa.SetMarked();

                    /* Allocate new DTrans, then initialize fields. */
                    DTrans dt = new DTrans(s, dfa);

                    /* Set dt array for each character transition. */
                    for (int i = 0; i < s.dtrans_ncols; i++)
                    {
                        /* Create new dfa set by attempting character transition. */
                        bunch.move(dfa, i);
                        if (!bunch.IsEmpty())
                        {
                            bunch.e_closure();
                        }
#if DEBUG
                        Utility.assert((null == bunch.GetNFASet() &&
                                        null == bunch.GetNFABit()) ||
                                       (null != bunch.GetNFASet() &&
                                        null != bunch.GetNFABit()));
#endif
                        /* Create new state or set state to empty. */
                        if (bunch.IsEmpty())
                        {
                            nextstate = DTrans.F;
                        }
                        else
                        {
                            nextstate = in_dstates(s, bunch);

                            if (nextstate == NOT_IN_DSTATES)
                            {
                                nextstate = add_to_dstates(s, bunch);
                            }
                        }
#if DEBUG
                        Utility.assert(nextstate < s.dfa_states.Count);
#endif
                        dt.SetDTrans(i, nextstate);
                    }
#if DEBUG
                    Utility.assert(s.dtrans_list.Count == dfa.GetLabel());
#endif
#if DEBUG
                    StringBuilder sb1 = new StringBuilder(Lex.MAXSTR);
                    sb1.Append("Current count = " + s.dtrans_list.Count.ToString() + "\n");
                    for (int i1 = 0; i1 < dt.GetDTransLength(); i1++)
                    {
                        sb1.Append(dt.GetDTrans(i1).ToString() + ",");
                    }
                    sb1.Append("end\n");
                    Console.Write(sb1.ToString());
#endif
                    s.dtrans_list.Add(dt);
                }
            }
            Console.WriteLine("");
        }
Exemplo n.º 34
0
/*
 * Function: reset
 * Description: Resets member variables.
 */
        private void reset()
        {
            spec    = null;
            group   = null;
            ingroup = null;
        }
Exemplo n.º 35
0
		private void set(Spec s)
		{
			this.spec = s;
			this.group = null;
			this.ingroup = null;
		}
Exemplo n.º 36
0
		private static void free_dfa_states(Spec s)
		{
			s.dfa_states = null;
			s.dfa_sets = null;
		}