Example #1
0
        static void Main(string[] args)
        {
            var grammar = GrammarLoader.Load("mangle-rule.txt");

            grammar.AddTag(new Number());
            grammar.AddTag(new SourceName());
            var parser = new Parser(grammar);

            while (true)
            {
                Console.Write("> ");
                string input = Console.ReadLine();
                var    tree  = parser.Parse(input);
                if (tree.Status == ParseTree.ParseStatus.Success)
                {
                    print(tree.Tree, 0);
                }
                else
                {
                    Console.WriteLine("Parse fail...");
                }
            }
        }
Example #2
0
        public Form1()
        {
            grammarLoader.Load(@"D:\TranslationSystems\parsegrammar2.txt");
            grammarLoader.CreateRecognizeTable();
            parser = new SyntaxParser.SyntaxParser(grammarLoader);
            lexer  = new Lex.Lex();
            InitializeComponent();
            ReadCode.MouseClick += (o, e) =>
            {
                using (StreamReader sr = new StreamReader(@"D:\code.txt"))
                {
                    CodeBox.Text = sr.ReadToEnd();
                }
            };
            GrammarLoad.Click += (o, e) =>
            {
                //try
                //{

                //    if (parser.IsParsed(lexs_bag.Lexems.ToList()))
                //    {
                //        ErrorsBox.Text += "Complete";
                //    }
                //    else
                //    {
                //        ErrorsBox.Text += "Failed";
                //    }
                //}
                //catch (Exception ex)
                //{
                //    ErrorsBox.Text = ex.Message;
                //}
            };
            AnalysButton.MouseClick += (o, e) =>
            {
                try
                {
                    IList <SyntaxParser.Operation> opers;

                    var lexs_bag = lexer.Analys(CodeBox.Lines);
                    if (parser.IsParsed(lexs_bag.Lexems.ToList(), out opers))
                    {
                        var codegen = new CodeGenerator();
                        ErrorsBox.Lines = codegen.generate(opers).Split('\n');

                        //ErrorsBox.Text = "Complete";
                        //ErrorsBox.Text = tree.ToString();
                        //CodeBox.Lines = tree.toStringArr().ToArray();
                    }
                    else
                    {
                        ErrorsBox.Text = "Failed";
                    }
                }
                catch (Exception ex)
                {
                    ErrorsBox.Text = ex.Message;
                }
                //var generator = new CodeGenerator();
                //ErrorsBox.Text = generator.Execute(generator.CreateOutputArray(CodeBox.Lines[0])).ToString();
            };
            CodeBox.TextChanged += (o, e) =>
            {
                string[] nums = new string[CodeBox.Lines.Length];
                for (int i = 1; i <= CodeBox.Lines.Length; i++)
                {
                    nums[i - 1] = i.ToString();
                }
                numericCode.Lines = nums;
            };
            CodeBox.VScroll += (o, e) =>
            {
                numericCode.Select(numericCode.Text.Length - 1, 0);
                numericCode.ScrollToCaret();
            };
        }