Exemplo n.º 1
0
        private void Button1_Click(object sender, EventArgs e)
        {
            richTextBoxTree.Clear();
            richTextBoxOutput.Clear();
            richTextBoxGenerator.Clear();
            listViewLexems.Items.Clear();
            try
            {
                AstNode program = MathLangParser.Parse(richTextBoxInput.Text);
                richTextBoxTree.Text      = AstNodePrinter.Print(program);
                richTextBoxGenerator.Text = MathLangIntepreter.Execute(program);
                buttonOptimize.Enabled    = true;
            }
            catch (Exception exc)
            {
                richTextBoxOutput.Text += "Error: " + exc;
                return;
            }
            var analizator = new Analizator();

            analizator.Analyze(richTextBoxInput.Text);
            richTextBoxOutput.Text = analizator.Output;
            foreach (var item in analizator.tablesRows)
            {
                ListViewItem lvi = new ListViewItem();
                ListViewItem.ListViewSubItem lvsi1 = new ListViewItem.ListViewSubItem();
                ListViewItem.ListViewSubItem lvsi2 = new ListViewItem.ListViewSubItem();
                lvi.Text   = $"lex{ item.number}";
                lvsi1.Text = $"{item.lexeme}";
                lvsi2.Text = $"{item.type}";
                lvi.SubItems.Add(lvsi1);
                lvi.SubItems.Add(lvsi2);
                listViewLexems.Items.Add(lvi);
            }
        }
Exemplo n.º 2
0
        // статическая реализации предыдущего метода (для удобства)
        public static AstNode Parse(string source)
        {
            MathLangParser mlp = new MathLangParser(source);

            return(mlp.Parse());
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            Lexer lex = new Lexer();

            /*
             *     lex.SourceText = @"{
             *        int i; int j; float v; float x; float[100] a;
             * while( true ) {
             * do i=i+1; while(a[i]<v);
             * do j=j-1; while(a[j]>v);
             * if(i>=j) break;
             * x=a[i];a[i]=a[j];a[j]=x;
             * }
             * }
             *     ";
             * b = 1; c=2;d=3;*/
            lex.SourceText = @"{int a; int b; int c;
                                 c=(a+b)/(a+4);
                        }";

            /*
             *                           if ((a+b)==1)
             *                  if(1==1)
             *                      b=a+b*3;
             *               a=1;
             */
            /*
             * Parser parse = new Parser(lex);
             *
             * parse.program();
             *
             * Console.WriteLine();
             * Console.WriteLine();
             * Var<int> nVal = new Var<int>(250);
             * Var<string> sVal = new Var<string>("String value");
             * List<int>[] arList = new List<int>[5];
             *
             * List<Variable> VarList = new List<Variable>();
             *
             * Registr[] reg = new Registr[5];
             *
             * VarList.Add(new StringVariable());
             * VarList.Add(new NumberVariable());
             *
             * for (int i = 0; i < 5; i++)
             *  reg[i] = new Registr();
             *
             * reg[0].IAX = 5;
             * reg[1].IAX = 5;
             * (VarList[0] as StringVariable).Value = "string value";
             * (VarList[1] as NumberVariable).Value = 50;
             *
             * nVal.Value = 500;
             *
             *
             * Console.WriteLine("value = " + (reg[0].IAX + reg[1].IAX));
             */

            /*
             * double result = MathExprIntepreter.Execute("3+4*(2+7-3)+5*10");
             *
             * Console.WriteLine("result = "+result.ToString());
             * Console.ReadLine();
             */
            // в зависимости от наличия параметров командной строки
            // разбираем либо файл с именем, переданным первым параметром
            // командной строки, либо стандартный ввод
            //TextReader reader = args.Length >= 1 ? new StreamReader(args[0]) : Console.In;
            //String source = reader.ReadToEnd();
            String source = @"a=2 
b=3
c=a+b*a
print c";

            try
            {
                AstNode program = MathLangParser.Parse(source);
                AstNodePrinter.Print(program);
                Console.WriteLine("------------------------");
                MathLangIntepreter.Execute(program);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e);
            }
            Console.ReadLine();
        }
Exemplo n.º 4
0
 public static AstNode Parse(string source) {
   MathLangParser mlp = new MathLangParser(source);
   return mlp.Parse();
 }