예제 #1
0
 public static void DrawTree(ExpressionTree tree)
 {
     var brush = new SolidBrush(Color.Red);
     System.Drawing.Image im = System.Drawing.Image.FromFile(@"d:\1.png");
     var graphics = Graphics.FromImage(im);
     DrawNode(graphics, im.Width / 2 - 100, 30, im.Width / 2 + 300, tree.Root);
     graphics.Save();
     im.Save(@"d:\2.png");
 }
예제 #2
0
        public static Object DoFunction(String text)
        {
            try
            {
                UserSession us = HttpContext.Current.Session["UserSession"] as UserSession;
                if (us == null || us.Machine == null || us.MagMachine == null || us.Helper == null)
                {
                    throw new Exception("No lexic found");
                }

                List<Object> ret = new List<object>();
                String il = "";
                int i = 0;
                us.Machine.Reset(text.Replace("\n", " "));
                while (us.Machine.HasNext())
                {
                    Token token = us.Machine.GetNextToken();
                    us.Machine.tm.AddToAll(i, token);
                    i += token.Value.Length;
                }
                foreach (Token t in us.Machine.tm.Tokens)
                    ret.Add(new { type = t.Type, value = t.Value });

                ExpressionTree tree = new ExpressionTree();
                us.MagMachine.AnalyzeString(us.Grammar, us.Machine, tree, text.Replace("\n", " "), us.Helper);

                //CodeGenerator.TryGetIlCode();
                //CodeGenerator.Count(0, 2, 1);
                string ilcode = "";
                CodeGenerator cg = new CodeGenerator();
                var func = cg.GetFunctionCount(tree.Root, out ilcode);

                DrawTree(tree);

                return new { dict = ret, il = il };
            }
            catch (Exception e)
            {
                return null;
            }
        }
예제 #3
0
        public void AnalyzeString(Grammar g, ComplexMachine m, ExpressionTree e, String s, Helper h)
        {
            List<Int32> rules = new List<int>();
            List<Token> usedTokens = new List<Token>();
            System.Diagnostics.Debug.WriteLine("Analyzing [" + s + "]");
            Stack<Token> st = new Stack<Token>();
            st.Push(new Token() { Type = TokenType.Q, Value = "0" });

            m.Reset(s);
            Token linetok = null;
            if (m.HasNext()) linetok = m.GetNextToken();
            while (linetok.Type == TokenType.DELIMITER) linetok = m.GetNextToken();

            while (st.Count > 0)
            {
                //Q
                Token stacktok = st.Peek();
                if (stacktok.Type != TokenType.Q) { System.Diagnostics.Debug.WriteLine("Unexpected token in stack"); return; }

                int q = Int32.Parse(stacktok.Value);

                String x = h.TableF.ContainsKey(linetok.Value)
                    ? linetok.Value : linetok.Type.ToString();

                if (!h.TableF.ContainsKey(x))
                {
                    System.Diagnostics.Debug.WriteLine("Unknown token " + x);
                    return;
                }

                String command = h.TableF[x][q];
                if (command.StartsWith("shift"))
                {
                    st.Push(linetok);
                    linetok = m.GetNextToken();
                    while (linetok.Type == TokenType.DELIMITER) linetok = m.GetNextToken();
                    if (linetok.Value == String.Empty) linetok.Value = "lambda";
                }
                else if (command.StartsWith("reduce"))
                {
                    int rulenum = Int32.Parse(command.Substring(6));
                    Rule rule = g.Rules[rulenum];
                    for (int r = rule.Right.Count - 1; r >= 0; r--)
                    {
                        st.Pop();
                        Token right = st.Pop();
                        if (!right.Value.Equals(rule.Right[r]) &&
                            !right.Type.ToString().Equals(rule.Right[r]))
                            System.Diagnostics.Debug.WriteLine("Error while reducing according to rule " + rulenum +
                                ": expected " + rule.Right[r] + ", got " + right);
                    }
                    st.Push(new Token() { Type = TokenType.NONTERM, Value = rule.Left });
                    //Console.WriteLine("Reduced according to rule " + rulenum);
                    System.Diagnostics.Debug.WriteLine("--------- " + rule.Action);
                    rules.Add(rulenum);
                }
                else if (command.StartsWith("accept"))
                {
                    st.Pop();
                    Token start = st.Pop();
                    Token q0 = st.Pop();
                    if ((st.Count == 0) && start.Value.Equals(g.Start.Right[0])
                        && q0.Value.Equals("0") && (q0.Type == TokenType.Q))
                    {
                        System.Diagnostics.Debug.WriteLine("ACCEPTED");
                        e.BuildByRules(g,rules, usedTokens);
                    }
                    else
                        System.Diagnostics.Debug.WriteLine("Unexpected end of file");
                    return;
                }
                else
                {
                    String allowed = String.Empty;
                    foreach (var key in h.TableF.Keys)
                    {
                        if (h.TableF[key][q] != String.Empty)
                        {
                            allowed = key;
                            break;
                        }
                    }
                    throw new Exception("Error: got " + linetok.Value + " while expecting " + allowed);

                }

                //not q
                stacktok = st.Pop();
                Token stacktok2 = st.Pop();
                q = Int32.Parse(stacktok2.Value);
                st.Push(stacktok2);
                st.Push(stacktok);

                x = h.TableG.ContainsKey(stacktok.Value)
                    ? stacktok.Value : stacktok.Type.ToString();

                if (!h.TableG.ContainsKey(x))
                {
                    System.Diagnostics.Debug.WriteLine("Unknown token " + x);
                    return;
                }

                if (stacktok.Type != TokenType.NONTERM)
                {
                    usedTokens.Add(stacktok);
                    System.Diagnostics.Debug.WriteLine("---PUSH " + stacktok.Value);
                }

                int gt = h.TableG[x][q];
                st.Push(new Token() { Type = TokenType.Q, Value = gt.ToString() });
            }
        }