private void Run(object sender, RoutedEventArgs e)
        {
            inputstr = input.Text;


            List <Token> tokens = WordAnalyser.Analyse(inputstr).Tokens;

            if (tokens == null)
            {
                MessageBox.Show("没有文本");
                return;
            }

            foreach (Token token in tokens)
            {
                output.Text += $"<{token.StrValue},{token.TokenType}>\n";
            }
            foreach (RowTabel item in RowAnalyser.run(inputstr))
            {
                output.Text += $"<{item.Name},{item.Id},{item.Row},{item.Num}>\n";
            }
        }
Example #2
0
 /// <summary>
 /// 调用词法分析程序
 /// </summary>
 /// <returns>词法分析的结果</returns>
 public TokenResult WordAnalyse()
 {
     return(WordAnalyser.Analyse(this.SourceCode));
 }
        /**
         * 用于自动补全
         *
         */
        void textEditor_TextArea_TextEntered(object sender, TextCompositionEventArgs e)
        {
            variableList     = WordAnalyser.GetAllVariables(input.Text);
            completionWindow = new CompletionWindow(input.TextArea);
            IList <ICompletionData> data = completionWindow.CompletionList.CompletionData;

            switch (e.Text)
            {
            case ".":
            {
                data.Add(new MyCompletionData("自动补全"));
                completionWindow.Show();
                completionWindow.Closed += delegate {
                    completionWindow = null;
                };
                break;
            }

            case "i":
            {
                var varList = from variables in variableList
                              where variables.StartsWith("i")
                              select variables;
                foreach (String v in varList)
                {
                    data.Add(new MyCompletionData(v));
                }
                data.Add(new MyCompletionData("int"));
                data.Add(new MyCompletionData("if"));
                completionWindow.Show();
                completionWindow.Closed += delegate {
                    completionWindow = null;
                };
                break;
            }

            case "r":
            {
                var varList = from variables in variableList
                              where variables.StartsWith("r")
                              select variables;
                foreach (String v in varList)
                {
                    data.Add(new MyCompletionData(v));
                }
                data.Add(new MyCompletionData("real"));
                data.Add(new MyCompletionData("read"));
                completionWindow.Show();
                completionWindow.Closed += delegate {
                    completionWindow = null;
                };
                break;
            }

            case "w":
            {
                var varList = from variables in variableList
                              where variables.StartsWith("w")
                              select variables;
                foreach (String v in varList)
                {
                    data.Add(new MyCompletionData(v));
                }
                data.Add(new MyCompletionData("write"));
                data.Add(new MyCompletionData("while"));
                completionWindow.Show();
                completionWindow.Closed += delegate {
                    completionWindow = null;
                };
                break;
            }

            case "e":
            {
                var varList = from variables in variableList
                              where variables.StartsWith("e")
                              select variables;
                foreach (String v in varList)
                {
                    data.Add(new MyCompletionData(v));
                }
                data.Add(new MyCompletionData("else"));
                completionWindow.Show();
                completionWindow.Closed += delegate {
                    completionWindow = null;
                };
                break;
            }

            default:
            {
                var varList = from variables in variableList
                              where variables.StartsWith(e.Text)
                              select variables;
                if (varList.Any())
                {
                    foreach (String v in varList)
                    {
                        data.Add(new MyCompletionData(v));
                    }
                    completionWindow.Show();
                    completionWindow.Closed += delegate {
                        completionWindow = null;
                    };
                }
                break;
            }
            }
        }
Example #4
0
        public static List <RowTabel> run(string input)
        {
            int             line     = 1; //第几行
            int             n        = 1; //第几个
            int             count    = 0; //总的表下标
            List <RowTabel> rowTabel = new List <RowTabel>();
            //总的表
            TokenResult result = WordAnalyser.Analyse(input);

            //每一行字符串
            System.IO.StringReader sr = new System.IO.StringReader(input);
            string str = sr.ReadLine();
            //每一行的表
            TokenResult temp = WordAnalyser.Analyse(str);

            for (; ;)
            {
                //每一个单词逐步对应
                for (int i = 0; i < temp.Tokens.Count; i++)
                {
                    //如果相等,直接打印
                    if (temp.Tokens[i].StrValue == result.Tokens[count].StrValue)
                    {
                        rowTabel.Add(new RowTabel()
                        {
                            Name = result.Tokens[count].StrValue,
                            Id   = result.Tokens[count].TokenType,
                            Row  = line,
                            Num  = n
                        });
                        n++;
                        count++;
                    }
                    //如果不相等
                    //1、说明表中下一个单词是多行的
                    else if (result.Tokens[count].StrValue.IndexOf(temp.Tokens[i].StrValue) == 0)
                    {
                        //获取多行单词的行数
                        Console.WriteLine(result.Tokens[count].StrValue + "    " + line + "    " + n);
                        int t = huanHangCiShu(result.Tokens[count].StrValue);
                        while (t > 1)
                        {
                            str = sr.ReadLine();
                            line++;
                            t--;
                        }
                        count++;
                        break;
                    }
                    //2、上一个多行单词“残留”在本行
                    //①只有该残留的单词
                    else if (temp.Tokens.Count == 1)
                    {
                        break;
                    }
                    else
                    //②残留单词后方还有单词
                    {
                        n++;
                    }
                }
                //重新获取下一行的数据
                str = sr.ReadLine();
                if (str == null)
                {
                    break;
                }
                temp = WordAnalyser.Analyse(str);
                line++;
                n = 1;
                //直到读完才退出
                if (count == result.Tokens.Count)
                {
                    break;
                }
            }
            return(rowTabel);
        }