Esempio n. 1
0
        public void StartLex(Compilourdes_GUI cmp)
        {
            Checker chk1 = new Checker();

            //checks each word for each delimiter
            chk1.checkMain(cmp, txtCode1);
            MessageBox.Show("Lexical Analysis Finished");

            if (cmp.CheckTable() == 1)
            {
                SyntaxInitializer Syntax_Analyzer = new SyntaxInitializer();
                string            syntax_result   = Syntax_Analyzer.Start(tokenDump(chk1.tokens)) + "\n";
                int c = 0;

                foreach (var item in Syntax_Analyzer.SET)
                {
                    string a = (c + 1).ToString();
                    string b = Syntax_Analyzer.PRODUCTION[c].ToString();
                    string d = "->";
                    string e = Syntax_Analyzer.SET[c].ToString();
                    cmp.Syntax1(a, b, d, e);
                    c++;
                }

                if (syntax_result == "Syntax Analyzer Succeeded...\n")
                {
                    MessageBox.Show("Syntax Analysis Finished");
                }

                else if (syntax_result != "Syntax Analyzer Succeeded...\n")
                {
                    if (Syntax_Analyzer.errors.getColumn() == 1)
                    {
                        Syntax_Analyzer.errors.setLines(Syntax_Analyzer.errors.getLines() - 1);
                    }

                    string a         = "SYN-Error";
                    int    errorline = Syntax_Analyzer.errors.getLines();
                    string b         = Syntax_Analyzer.errors.getErrorMessage().ToString();
                    string cc        = errorline.ToString();
                    string e         = Syntax_Analyzer.errors.getErrorMessage().ToString();
                    cmp.Syntax2(a, b, cc);
                    //errornum++;
                }
            }
        }
Esempio n. 2
0
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            SyntaxInitializer S_initialize = new SyntaxInitializer();

            dataGridView1.Rows.Clear();
            int    i = 1;
            string s;

            s = S_initialize.Start(tokenDump(lex.token));
            //Syntax_Output.Text = "< ========== > Syntax Report < ========== > \n" + s + "\n"
            // + "< ========== > End of Report < ========== > \n";

            //Syntax_Table.Rows.Clear();
            //int c = 0;
            //foreach (var item in S_initialize.SET)
            //{
            //    Syntax_Table.Rows.Add(S_initialize.PRODUCTION[c].ToString(), "->", S_initialize.SET[c].ToString());
            //    c++;
            //}

            //MessageBox.Show(S_initialize.recursiveprod);
            if (s != "Syntax Analyzer Succeeded...")
            {
                int errornum = 1;
                dataGridView1.Rows.Clear();
                if (S_initialize.errors.getColumn() == 1)
                {
                    S_initialize.errors.setLines(S_initialize.errors.getLines() - 1);
                }
                dataGridView1.Rows.Add(errornum, S_initialize.errors.getErrorMessage(), S_initialize.errors.getLines());
                errornum++;
            }
            else
            {
                Output.Text += s;
            }
            //SemanticsInitializer semantics = new SemanticsInitializer();
            //semantics = SemanticsStart(tokenDumps(lex.token));
            //string semantics_result = semantics.Start();
            //Output.Text += semantics.error + semantics_result;
        }
Esempio n. 3
0
        private void LexButton_Click(object sender, EventArgs e)
        {
            LexGrid.Rows.Clear();
            DataLexicalError.Rows.Clear();
            DataSyntaxError.Rows.Clear();
            Grid_Syntax.Rows.Clear();
            Grid_Dec.Rows.Clear();
            Grid_Array.Rows.Clear();
            Output.Text = "";
            if (Code.Text != "")
            {
                //LEXICAL ANALYZER
                Output.Text = "[1] Starting Lexical Analyzer\n";
                LexicalAnalyzer    lex     = new LexicalAnalyzer();
                LexicalInitializer Lexical = new LexicalInitializer();
                string             txt     = Code.Text;
                lex = Lexical.Start(txt, lex);
                //DISPLAY TOKENS
                DisplayTokens(lex);

                if (syntax_mode.Checked)
                {
                    if (lex.invalid == 0 && lex.token.Count != 0)
                    {
                        //SYNTAX ANALYZER
                        SyntaxInitializer    Syntax_Analyzer = new SyntaxInitializer();
                        SemanticsInitializer semantics       = new SemanticsInitializer();
                        Output.Text += "\n[2] Starting Syntax Analyzer\n";

                        if (semantics_mode.Checked)
                        {
                            Output.Text += "[3] Starting Static Semantics Analyzer\n";
                        }
                        string syntax_result = Syntax_Analyzer.Start(tokenDump(lex.token)) + "\n";
                        int    c             = 0;
                        Grid_Syntax.Rows.Clear();
                        foreach (var item in Syntax_Analyzer.SET)
                        {
                            Grid_Syntax.Rows.Add((c + 1).ToString(), Syntax_Analyzer.PRODUCTION[c].ToString(), "->", Syntax_Analyzer.SET[c].ToString());
                            c++;
                        }


                        //MessageBox.Show(Syntax_Analyzer.production);
                        //MessageBox.Show(Syntax_Analyzer.recursiveprod);
                        if (syntax_result != "Syntax Analyzer Succeeded...\n")
                        {
                            int errornum = 1;
                            DataSyntaxError.Rows.Clear();
                            //if(Syntax_Analyzer.PRODUCTION[c-1] == "Prod_logop1")
                            //{
                            //    DataSyntaxError.Rows.Add(errornum, Syntax_Analyzer.errors.getLines(), Syntax_Analyzer.errors.getColumn(), "Expected: \"!\", \"id\", \"boollit\", \"intlit\", \"doublelit\", \"charlit\", \"stringlit\".");
                            //}
                            //else if (Syntax_Analyzer.PRODUCTION[c - 1] == "Prod_option" && Syntax_Analyzer.errors.getErrorMessage() == "Expected: Var, Clear, id, Int, Char, Boolean, Double, String, ++, --, Object, Until, Do, For, Array, If, Read, Say, Option, ., Stop, End.")
                            //{
                            //    DataSyntaxError.Rows.Add(errornum, Syntax_Analyzer.errors.getLines(), Syntax_Analyzer.errors.getColumn(), "Expected: \"intlit\", \"charlit\", \"stringlit\".");
                            //}
                            //else
                            if (Syntax_Analyzer.errors.getColumn() == 1)
                            {
                                Syntax_Analyzer.errors.setLines(Syntax_Analyzer.errors.getLines() - 1);
                            }
                            DataSyntaxError.Rows.Add(errornum, Syntax_Analyzer.errors.getLines(), Syntax_Analyzer.errors.getColumn(), Syntax_Analyzer.errors.getErrorMessage());
                            errornum++;
                        }
                        else
                        {
                            Output.Text += syntax_result;
                        }
                        if (semantics_mode.Checked)
                        {
                            semantics = SemanticsStart(tokenDumps(lex.token));
                            string semantics_result = semantics.Start();
                            foreach (var item in semantics.Identifiers)
                            {
                                Grid_Dec.Rows.Add(item.getLines(), item.getId(), item.getDtype(), item.getValue(), item.getScope());
                            }
                            foreach (var item in semantics.Indexes)
                            {
                                Grid_Array.Rows.Add(item.getId(), item.getDatatype(), item.getValue(), item.getIndex_1(), item.getIndex_2());
                            }
                            if (semantics.error != "" || syntax_result != "Syntax Analyzer Succeeded...\n")
                            {
                                Output.Text += semantics.error + semantics_result;
                            }
                            else
                            {
                                Output.Text += "Static Semantics Analyzer Succeeded...";
                                MessageBox.Show("Code compiled with no error.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                                CodeGenInitialize generate = new CodeGenInitialize();
                                generate = CodeGenStart(TokenDump(lex.token), semantics);
                                generate.Start();
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        private void LexButton_Click(object sender, EventArgs e)
        {
            LexGrid.Rows.Clear();
            DataLexicalError.Rows.Clear();
            DataSyntaxError.Rows.Clear();
            Grid_Syntax.Rows.Clear();
            Grid_Dec.Rows.Clear();
            Grid_Array.Rows.Clear();
            Output.Text = "";
            if (Code.Text != "")
            {
                //LEXICAL ANALYZER
                Output.Text = "[1] Starting Lexical Analyzer\n";
                LexicalAnalyzer    lex     = new LexicalAnalyzer();
                LexicalInitializer Lexical = new LexicalInitializer();
                string             txt     = Code.Text;
                lex = Lexical.Start(txt, lex);
                //DISPLAY TOKENS
                DisplayTokens(lex);

                if (syntax_mode.Checked)
                {
                    if (lex.invalid == 0 && lex.token.Count != 0)
                    {
                        //SYNTAX ANALYZER
                        SyntaxInitializer    Syntax_Analyzer = new SyntaxInitializer();
                        SemanticsInitializer semantics       = new SemanticsInitializer();
                        Output.Text += "\n[2] Starting Syntax Analyzer\n";

                        if (semantics_mode.Checked)
                        {
                            Output.Text += "[3] Starting Static Semantics Analyzer\n";
                        }
                        string syntax_result = Syntax_Analyzer.Start(tokenDump(lex.token)) + "\n";
                        int    c             = 0;
                        Grid_Syntax.Rows.Clear();
                        foreach (var item in Syntax_Analyzer.SET)
                        {
                            Grid_Syntax.Rows.Add((c + 1).ToString(), Syntax_Analyzer.PRODUCTION[c].ToString(), "->", Syntax_Analyzer.SET[c].ToString());
                            c++;
                        }


                        //MessageBox.Show(Syntax_Analyzer.production);
                        //MessageBox.Show(Syntax_Analyzer.recursiveprod);
                        if (syntax_result != "Syntax Analyzer Succeeded...\n")
                        {
                            int errornum = 1;
                            DataSyntaxError.Rows.Clear();
                            //if(Syntax_Analyzer.PRODUCTION[c-1] == "Prod_logop1")
                            //{
                            //    DataSyntaxError.Rows.Add(errornum, Syntax_Analyzer.errors.getLines(), Syntax_Analyzer.errors.getColumn(), "Expected: \"!\", \"id\", \"boollit\", \"intlit\", \"doublelit\", \"charlit\", \"stringlit\".");
                            //}
                            //else if (Syntax_Analyzer.PRODUCTION[c - 1] == "Prod_option" && Syntax_Analyzer.errors.getErrorMessage() == "Expected: Var, Clear, id, Int, Char, Boolean, Double, String, ++, --, Object, Until, Do, For, Array, If, Read, Say, Option, ., Stop, End.")
                            //{
                            //    DataSyntaxError.Rows.Add(errornum, Syntax_Analyzer.errors.getLines(), Syntax_Analyzer.errors.getColumn(), "Expected: \"intlit\", \"charlit\", \"stringlit\".");
                            //}
                            //else
                            if (Syntax_Analyzer.errors.getColumn() == 1)
                            {
                                Syntax_Analyzer.errors.setLines(Syntax_Analyzer.errors.getLines() - 1);
                            }
                            DataSyntaxError.Rows.Add(errornum, Syntax_Analyzer.errors.getLines(), Syntax_Analyzer.errors.getColumn(), Syntax_Analyzer.errors.getErrorMessage());
                            errornum++;
                        }
                        else
                        {
                            Output.Text += syntax_result;
                        }
                        if (semantics_mode.Checked)
                        {
                            semantics = SemanticsStart(tokenDumps(lex.token));
                            string semantics_result = semantics.Start();
                            foreach (var item in semantics.Identifiers)
                            {
                                Grid_Dec.Rows.Add(item.getLines(), item.getId(), item.getDtype(), item.getValue(), item.getScope());
                            }
                            foreach (var item in semantics.Indexes)
                            {
                                Grid_Array.Rows.Add(item.getId(), item.getDatatype(), item.getValue(), item.getIndex_1(), item.getIndex_2());
                            }
                            if (semantics.error != "" || syntax_result != "Syntax Analyzer Succeeded...\n")
                            {
                                Output.Text += semantics.error + semantics_result;
                            }
                            else
                            {
                                CodeTranslator generate = new CodeTranslator();
                                generate = CodeGenStart(TokenDump(lex.token), semantics);
                                generate.Start();
                                string code = generate.code;
                                code  = code.Remove(code.Length - 2, 2);
                                code += "}\n    public static Int64[] Random(Int64 start, Int64 end)\n{\nList<Int64> listNumbers = new List<Int64>();\nInt64 length = (end - start) + 1;\nif (length < 2)\n{\n    Console.WriteLine(\"Insufficient Length of Random Numbers...\");\n    return listNumbers.ToArray();\n}\n\nRandom rand = new Random();\nInt64 number;\nfor (Int64 i = 0; i < length; i++)\n{\n    do\n    {\n        number = Int64.Parse(rand.Next(Int32.Parse(start.ToString()), Int32.Parse(end.ToString()) + 1).ToString());\n    } while (listNumbers.Contains(number));\n    listNumbers.Add(number);\n}\nreturn listNumbers.ToArray();\n}";
                                code += "\nprivate static string Substr(string str, Int64 index)\n{\nreturn str.Substring(Int32.Parse(index.ToString()));\n}\nprivate static string Substr(string str, Int64 index, Int64 length)\n{\nreturn str.Substring(Int32.Parse(index.ToString()), Int32.Parse(length.ToString()));\n}\nprivate static Int64 StrLen(string str)\n{\nreturn Int64.Parse(str.Length.ToString());\n}\npublic static Int64 PInt(string s)\n{\n    Int64 num = -1;\n bool p = false;\n  p = Int64.TryParse(s,out num);\n    return num;\n}";
                                code += "\npublic static char[] ToCharArray (string str)\n{\nreturn str.ToCharArray();\n}\n";
                                code += "\npublic static string ToUpper(string str)\n{\n    return str.ToUpper();\n}\n\npublic static string ToLower(string str)\n{\n    return str.ToLower();\n}\n";
                                code += "\n   private static double Sqrt(Int64 intNum)\n   {\n           return Double.Parse(Math.Sqrt(Int32.Parse(intNum.ToString())).ToString(\"0.00##\"));\n   }\n";
                                code += " \npublic static Int64 CompareTo(string a, string b)\n{\n    return Int64.Parse(a.CompareTo(b).ToString());\n}";
                                code += "\npublic static double PDouble(string s)\n{\n    double num = -1;\n bool p = false;\n  p = Double.TryParse(s,out num);\n    return num;\n}\n}\n";
                                if (generatedCode.Checked)
                                {
                                    MessageBox.Show(code);
                                }
                                if (consoleOutput.Checked)
                                {
                                    //OUTPUT
                                    OutputInitializer output = new OutputInitializer();
                                    output.Start(code);
                                    if (output.error != "")
                                    {
                                        Output.Text += "Semantic Error/s: \n" + output.error;
                                        MessageBox.Show("Code compiled with error.", "FAILED!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    else
                                    {
                                        Output.Text += "Static Semantics Analyzer Succeeded...";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }