Example #1
0
        public void Define(object key, object value)
        {
            for (int i = 0; i < this._items.Count; i++)
            {
                object obj = this._items.GetByIndex(i);

                if (!(obj is Tuple))
                {
                    throw new System.Exception("Invalid function.");
                }
                Tuple tuple = (Tuple)obj;
                if (tuple.Count != 2)
                {
                    throw new System.Exception("Invalid function.");
                }

                if (tuple[0].Equals(key))
                {
                    this._items.Remove(tuple);
                    break;
                }
            }

            object item = new Surf.Tuple(new object[] { key, value });

            this._items.Add(item, item);
        }
Example #2
0
        private static Surf.Set Split(Surf.Set S, char character, Surf.Set transitions, Surf.Set P2)
        {
            //System.Console.WriteLine("START Split2     " + System.DateTime.Now.ToString());

            Surf.Set f_nP = PartitionMap(P2);

            Surf.Set partition_map = new Surf.Set();

            foreach (int fromState in S)
            {
                object key = new Surf.Tuple(new object[] { fromState, character });
                if (transitions.IsDefined(key))
                {
                    object value     = transitions.Apply(key);
                    int    toState   = (int)value;
                    int    partition = (int)f_nP.Apply(toState);
                    partition_map.Add(new Surf.Tuple(new object[] { partition, fromState }));
                }
                else
                {
                    partition_map.Add(new Surf.Tuple(new object[] { -1, fromState }));
                }
            }

            //System.Console.WriteLine("END   Split2     " + System.DateTime.Now.ToString());
            return(partition_map.Nest().Range());
        }
Example #3
0
        private void RecalculateDependencies(string name)
        {
            Surf.Set dependents = this._dependencies.Unnest().Transpose().Nest();

            Surf.Set recalculationList = new Surf.Set();
            RecalculationList(name, dependents, recalculationList);

            Surf.Set dependencyGraph = new Surf.Set();
            DependencyGraph(this._dependencies, recalculationList, dependencyGraph);

            if (dependencyGraph.Count > 0)
            {
                Surf.Tuple topologicalSort = new Surf.Tuple();
                TopologicalSort(name, dependencyGraph, topologicalSort);

                foreach (string identifier in topologicalSort)
                {
                    object value = Evaluate((Node)this._definitions.Apply(identifier));
                    if (value != null)
                    {
                        this._variables.Define(identifier, value);
                    }
                }
            }
        }
Example #4
0
        private static Surf.Set Split(Surf.Set S, Surf.Set transitions, Surf.Set P2)
        {
            //System.Console.WriteLine("START Split     " + System.DateTime.Now.ToString());

            Surf.Set alphabet = new Surf.Set();
            foreach (int state in S)
            {
                foreach (Surf.Tuple transition in transitions)
                {
                    Surf.Tuple input     = (Surf.Tuple)transition[0];
                    int        fromState = (int)input[0];
                    char       character = (char)input[1];
                    int        toState   = (int)transition[1];

                    if (fromState == state)
                    {
                        alphabet.Add(character);
                    }
                }
            }

            foreach (char character in alphabet)
            {
                Surf.Set s = Split(S, character, transitions, P2);
                if (s.Count > 1)
                {
                    //System.Console.WriteLine("END   Split     " + System.DateTime.Now.ToString());
                    return(s);
                }
            }

            //System.Console.WriteLine("END   Split     " + System.DateTime.Now.ToString());
            return(new Surf.Set(new object[] { S }));
        }
Example #5
0
 private static Surf.Tuple Tuple(Surf.Set a)
 {
     Surf.Tuple b = new Surf.Tuple();
     foreach (object item in a)
     {
         b.Add(item);
     }
     return(b);
 }
Example #6
0
 protected override object EvaluateTuple(Node node)
 {
     Surf.Tuple tuple = new Surf.Tuple();
     System.Collections.ArrayList items = (System.Collections.ArrayList)Evaluate(node.Nodes[1]);
     foreach (object item in items)
     {
         tuple.Add(item);
     }
     return(tuple);
 }
Example #7
0
        private static Surf.Set EpsilonClosure(Surf.Set states, Surf.Set transitions, Surf.Set epsilonCache)
        {
            Surf.Tuple key = Tuple(states);
            if (epsilonCache.IsDefined(key))
            {
                return((Surf.Set)epsilonCache.Apply(key));
            }

            //System.Console.WriteLine("START EpsilonClosure     " + System.DateTime.Now.ToString());
            Surf.Set epsilonClosure = new Surf.Set();

            Stack <int> worklist = new Stack <int>();

            foreach (int state in states)
            {
                epsilonClosure.Add(state);
                worklist.Push(state);
            }

            while (worklist.Count > 0)
            {
                int state = worklist.Pop();

                foreach (Surf.Tuple transition in transitions)
                {
                    Surf.Tuple input = (Surf.Tuple)transition[0];

                    if (input.Count == 1 && (int)input[0] == state)
                    {
                        int toState = (int)transition[1];
                        if (!epsilonClosure.Contains(toState))
                        {
                            epsilonClosure.Add(toState);
                            worklist.Push(toState);
                        }
                    }
                }
            }

            epsilonCache.Add(new Surf.Tuple(new object[] { key, epsilonClosure }));
            //System.Console.WriteLine("END   EpsilonClosure     " + System.DateTime.Now.ToString());
            return(epsilonClosure);
        }
Example #8
0
        private static void TopologicalSort(string name, Surf.Set dependencyGraph, Surf.Tuple topologicalSort)
        {
            Surf.Set completed = new Surf.Set();
            completed.Add(name);

            while (dependencyGraph.Count > 0)
            {
                for (int i = 0; i < dependencyGraph.Count; i++)
                {
                    Surf.Tuple tuple = (Surf.Tuple)dependencyGraph[i];

                    if (((Surf.Set)tuple[1]).Difference(completed).Count == 0)
                    {
                        topologicalSort.Add(tuple[0]);
                        completed.Add(tuple[0]);
                        dependencyGraph.Remove(i);
                        break;
                    }
                }
            }
        }
Example #9
0
        // Hopcroft's Algorithm
        public static FiniteAutomaton Minimize(FiniteAutomaton dfa)
        {
            System.Console.WriteLine("START Minimize     " + System.DateTime.Now.ToString());

            Surf.Set dfa_transitions = Set(dfa.Transitions);

            // Split final states and non-final states.
            Surf.Set worklist = new Surf.Set();
            Surf.Set P        = new Surf.Set();

            Surf.Set f = SplitFinalStates(dfa.Tokens);
            foreach (Surf.Set f2 in f)
            {
                worklist.Add(f2);
                P.Add(f2);
            }
            Surf.Set nonFinalStates = new Surf.Set(dfa.States).Difference(new Surf.Set(dfa.FinalStates));
            worklist.Add(nonFinalStates);
            P.Add(nonFinalStates);

            // While there are more states to split.
            while (worklist.Count > 0)
            {
                Surf.Set p = (Surf.Set)worklist[0];
                worklist.Remove(0);

                Surf.Set t = Split(p, dfa_transitions, P);

                if (t.Count > 1)
                {
                    int i = 0;
                    foreach (Surf.Set p2 in P)
                    {
                        if (p2.Equals(p))
                        {
                            P.Remove(i);
                            break;
                        }
                        i++;
                    }
                    foreach (Surf.Set t2 in t)
                    {
                        worklist.Add(t2);
                        P.Add(t2);
                    }
                }
            }

            /*
             * // Split final states and non-final states.
             * Surf.Set P = new Surf.Set();
             * Surf.Set f = SplitFinalStates(dfa.Tokens);
             * foreach (Surf.Set f2 in f)
             * {
             *      P.Add(f2);
             * }
             * P.Add(new Surf.Set(dfa.States).Difference(new Surf.Set(dfa.FinalStates)));
             *
             * // While there are more states to split.
             * bool isChanging = true;
             * while (isChanging)
             * {
             *      isChanging = false;
             *
             *      Surf.Set T = new Surf.Set();
             *      foreach (Surf.Set p in P)
             *      {
             *              Surf.Set t = Split(p, dfa_transitions, P);
             *
             *              if (t.Count > 1)
             *              {
             *                      isChanging = true;
             *              }
             *              foreach (Surf.Set t2 in t)
             *              {
             *                      T.Add(t2);
             *              }
             *      }
             *      P = T;
             * }
             */



            Surf.Set states      = new Surf.Set();
            Surf.Set alphabet    = new Surf.Set();
            Surf.Set transitions = new Surf.Set();
            int      startState;

            Surf.Set finalStates = new Surf.Set();
            Surf.Set tokens      = new Surf.Set();

            Surf.Set P_map = PartitionMap(P);

            foreach (int state in dfa.States)
            {
                states.Add(P_map.Apply(state));
            }
            alphabet = new Surf.Set(dfa.Alphabet);
            foreach (Surf.Tuple transition in dfa_transitions)
            {
                Surf.Tuple input     = (Surf.Tuple)transition[0];
                int        fromState = (int)P_map.Apply(input[0]);
                char       character = (char)input[1];
                int        toState   = (int)P_map.Apply(transition[1]);
                transitions.Add(new Surf.Tuple(new object[] { new Surf.Tuple(new object[] { fromState, character }), toState }));
            }
            startState = (int)P_map.Apply(dfa.StartState);
            foreach (int state in dfa.FinalStates)
            {
                finalStates.Add(P_map.Apply(state));
                if (!tokens.IsDefined(P_map.Apply(state)))
                {
                    tokens.Add(new Surf.Tuple(new object[] { P_map.Apply(state), Lookup(dfa.Tokens, state) }));
                }
            }

            System.Console.WriteLine("END   Minimize     " + System.DateTime.Now.ToString());
            return(Reorder(new FiniteAutomaton(states, alphabet, transitions, startState, finalStates, tokens)));
        }
Example #10
0
        public static FiniteAutomaton DFA(Bamboo.Parsing.FiniteAutomata.FiniteAutomaton nfa, Counter counter)
        {
            System.Console.WriteLine("START DFA     " + System.DateTime.Now.ToString());

            Surf.Set epsilonCache = new Surf.Set();
            Surf.Set moveCache    = new Surf.Set();

            Surf.Set nfa_transitions = Set(nfa.Transitions);

            Dictionary <string, int> nfa_transitions_lookup = new Dictionary <string, int>();

            foreach (Surf.Tuple transition in nfa_transitions)
            {
                Surf.Tuple input = (Surf.Tuple)transition[0];

                if (input.Count == 2)
                {
                    int  state = (int)input[0];
                    char ch    = (char)input[1];

                    nfa_transitions_lookup.Add("" + state + "::" + ch, (int)transition[1]);
                }
            }

            Surf.Set q0 = EpsilonClosure(new Surf.Set(new object[] { nfa.StartState }), nfa_transitions, epsilonCache);
            Surf.Set Q  = new Surf.Set();            // A set whose element are sets of states that are subsets of N.
            Q.Add(q0);
            Surf.Set         T        = new Surf.Set();
            Stack <Surf.Set> worklist = new Stack <Surf.Set>();

            worklist.Push(q0);

            while (worklist.Count > 0)
            {
                Surf.Set q = worklist.Pop();

                foreach (char character in nfa.Alphabet)
                {
                    Surf.Set t = EpsilonClosure(Move(q, character, nfa_transitions_lookup), nfa_transitions, epsilonCache);

                    if (t.Count > 0)
                    {
                        T.Add(new Surf.Tuple(new object[] { new Surf.Tuple(new object[] { q, character }), t }));

                        if (!Q.Contains(t))
                        {
                            Q.Add(t);
                            worklist.Push(t);
                        }
                    }
                }
            }



            Surf.Set states      = new Surf.Set();
            Surf.Set alphabet    = new Surf.Set();
            Surf.Set transitions = new Surf.Set();
            int      startState;

            Surf.Set finalStates = new Surf.Set();
            Surf.Set tokens      = new Surf.Set();

            Surf.Set D = new Surf.Set();
            foreach (Surf.Set q in Q)
            {
                D.Add(new Surf.Tuple(new object[] { q, counter.Next() }));
            }



            foreach (Surf.Set q in Q)
            {
                states.Add(D.Apply(q));
            }
            alphabet = new Surf.Set(nfa.Alphabet);
            foreach (Surf.Tuple t in T)
            {
                Surf.Tuple input     = (Surf.Tuple)t[0];
                int        fromState = (int)D.Apply(input[0]);
                char       character = (char)input[1];
                int        toState   = (int)D.Apply(t[1]);
                transitions.Add(new Surf.Tuple(new object[] { new Surf.Tuple(new object[] { fromState, character }), toState }));
            }
            startState = (int)states[0];
            foreach (int state in nfa.FinalStates)
            {
                foreach (Surf.Tuple d in D)
                {
                    Surf.Set q = (Surf.Set)d[0];
                    if (q.Contains(state))
                    {
                        finalStates.Add(d[1]);
                        foreach (int q_state in q)
                        {
                            string token = Lookup(nfa.Tokens, (int)q_state);
                            if (token.Length > 0)
                            {
                                tokens.Add(new Surf.Tuple(new object[] { d[1], token }));
                                break;
                            }
                        }
                    }
                }
            }

            System.Console.WriteLine("END   DFA     " + System.DateTime.Now.ToString());
            //foreach (Token token in nfa.Tokens)
            //{
            //    System.Console.WriteLine(token.Name + " " + token.Number);
            //}
            //System.Console.WriteLine();
            //Surf.Printer.Print(tokens, System.Console.Out);
            //System.Console.WriteLine();
            //System.Console.WriteLine();
            return(Minimize(Reorder(new FiniteAutomaton(states, alphabet, transitions, startState, finalStates, tokens))));
        }
Example #11
0
        public FiniteAutomaton(Surf.Set states, Surf.Set alphabet, Surf.Set transitions, int startState, Surf.Set finalStates, Surf.Set tokens)
        {
            int[] states2 = new int[states.Count];
            for (int i = 0; i < states2.Length; i++)
            {
                states2[i] = (int)states[i];
            }

            char[] alphabet2 = new char[alphabet.Count];
            for (int i = 0; i < alphabet2.Length; i++)
            {
                alphabet2[i] = (char)alphabet[i];
            }

            Transition[] transitions2 = new Transition[transitions.Count];
            for (int i = 0; i < transitions2.Length; i++)
            {
                Surf.Tuple tuple   = (Surf.Tuple)transitions[i];
                Surf.Tuple input   = (Surf.Tuple)tuple[0];
                int        toState = (int)tuple[1];

                Transition transition;
                if (input.Count == 1)
                {
                    int fromState = (int)input[0];
                    transition = new Transition(fromState, toState);
                }
                else if (input.Count == 2)
                {
                    int  fromState = (int)input[0];
                    char character = (char)input[1];
                    transition = new Transition(fromState, character, toState);
                }
                else
                {
                    throw new System.Exception("UNEXPECTED.");
                }
                transitions2[i] = transition;
            }

            int[] finalStates2 = new int[finalStates.Count];
            for (int i = 0; i < finalStates2.Length; i++)
            {
                finalStates2[i] = (int)finalStates[i];
            }

            Token[] tokens2 = new Token[tokens.Count];
            for (int i = 0; i < tokens2.Length; i++)
            {
                Surf.Tuple tuple = (Surf.Tuple)tokens[i];

                int    number = (int)tuple[0];
                string name   = (string)tuple[1];

                tokens2[i] = new Token(name, number);
            }

            this.States      = states2;
            this.Alphabet    = alphabet2;
            this.Transitions = transitions2;
            this.StartState  = startState;
            this.FinalStates = finalStates2;
            this.Tokens      = tokens2;
        }
Example #12
0
        public static void Generate(string name, string nspace, Bamboo.Parsing.FiniteAutomata.FiniteAutomaton finiteAutomaton, System.IO.TextWriter writer)
        {
            writer.WriteLine("//");
            writer.WriteLine("// AUTOGENERATED " + System.DateTime.Now + "");
            writer.WriteLine("//");
            writer.WriteLine("using System;");
            writer.WriteLine("");
            writer.WriteLine("namespace " + nspace + "");
            writer.WriteLine("{");
            writer.WriteLine("	public class "+ name + "Tokenizer");
            writer.WriteLine("	{");
            writer.WriteLine("		private static readonly int[,] TABLE = new int[,] {");

            Surf.Set transitions = new Surf.Set();
            foreach (Bamboo.Parsing.FiniteAutomata.Transition transition in finiteAutomaton.Transitions)
            {
                transitions.Add(new Surf.Tuple(new object[] { new Surf.Tuple(new object[] { transition.FromState, transition.Character }), transition.ToState }));
            }
            foreach (int state in finiteAutomaton.States)
            {
                writer.Write("			{	");
                foreach (object character in finiteAutomaton.Alphabet)
                {
                    Surf.Tuple key = new Surf.Tuple(new object[] { state, character });
                    if (transitions.IsDefined(key))
                    {
                        int toState = (int)transitions.Apply(key);
                        writer.Write("" + toState + ",		");
                    }
                    else
                    {
                        writer.Write("-1,		");
                    }
                }
                writer.WriteLine("},");
            }

            writer.WriteLine("		};");
            writer.WriteLine("");
            writer.WriteLine("		private System.Text.StringBuilder _stringBuilder = new System.Text.StringBuilder();");
            writer.WriteLine("");
            writer.WriteLine("		public "+ name + "Tokenizer()");
            writer.WriteLine("		{");
            writer.WriteLine("		}");
            writer.WriteLine("");
            writer.WriteLine("		public "+ name + "Token Tokenize(" + name + "TextReader reader)");
            writer.WriteLine("		{");
            writer.WriteLine("			int n;");
            writer.WriteLine("			char ch;");
            writer.WriteLine("");
            writer.WriteLine("			_stringBuilder.Length = 0;");
            writer.WriteLine("");

            /*
             * //
             * // Trim whitespace
             * //
             * while ((n = reader.Peek()) != -1)
             * {
             *      ch = (char)n;
             *      switch (ch)
             *      {
             *              case ' ':
             *              case '\t':
             *              case '\r':
             *              case '\n':
             *                      {
             *                              break;
             *                      }
             *              default:
             *                      {
             *                              goto s0;
             *                      }
             *      }
             * }
             */
            writer.WriteLine("			n = reader.Peek();");
            writer.WriteLine("			ch = (char)0;");
            writer.WriteLine("			int state = 0;");
            writer.WriteLine("");
            writer.WriteLine("			if (n == -1)");
            writer.WriteLine("			{");
            writer.WriteLine("				return new "+ name + "Token(" + name + "TokenType._EOF_);");
            writer.WriteLine("			}");
            writer.WriteLine("");
            writer.WriteLine("			while(n != -1 && state != -1)");
            writer.WriteLine("			{");
            writer.WriteLine("				ch = (char)n;");
            writer.WriteLine("");
            writer.WriteLine("				switch(ch)");
            writer.WriteLine("				{");

            int i = 0;

            foreach (char character in finiteAutomaton.Alphabet)
            {
                writer.WriteLine("					case '"+ Escape(character) + "':");
                writer.WriteLine("					{");
                writer.WriteLine("						int state2 = TABLE[state, "+ i + "];");
                writer.WriteLine("						if(state2 == -1)");
                writer.WriteLine("						{");
                writer.WriteLine("							goto EXIT;");
                writer.WriteLine("						}");
                writer.WriteLine("");
                writer.WriteLine("						state = state2;");
                writer.WriteLine("						_stringBuilder.Append(ch);");
                writer.WriteLine("						reader.Read();");
                writer.WriteLine("						n = reader.Peek();");
                writer.WriteLine("						break;");
                writer.WriteLine("					}");
                i++;
            }

            writer.WriteLine("					default:");
            writer.WriteLine("					{");
            writer.WriteLine("						goto EXIT;");
            writer.WriteLine("					}");
            writer.WriteLine("				}");
            writer.WriteLine("			}");
            writer.WriteLine("");
            writer.WriteLine("		EXIT:");
            writer.WriteLine("			switch(state)");
            writer.WriteLine("			{");

            foreach (int finalState in finiteAutomaton.FinalStates)
            {
                Bamboo.Parsing.FiniteAutomata.Token token = GetToken(finalState, finiteAutomaton.Tokens);

                writer.WriteLine("				case "+ finalState + ":");
                writer.WriteLine("				{");
                writer.WriteLine("					return new "+ name + "Token(" + name + "TokenType." + token.Name + ", _stringBuilder.ToString());");
                writer.WriteLine("				}");
            }

            writer.WriteLine("				default:");
            writer.WriteLine("				{");
            writer.WriteLine("					if (n > -1)");
            writer.WriteLine("					{");
            writer.WriteLine("						_stringBuilder.Append(ch);");
            writer.WriteLine("					}");
            writer.WriteLine("					return new "+ name + "Token(" + name + "TokenType._ERROR_, _stringBuilder.ToString());");
            writer.WriteLine("				}");
            writer.WriteLine("			}");
            writer.WriteLine("		}");
            writer.WriteLine("");
            writer.WriteLine("	}");
            writer.WriteLine("}");
        }