示例#1
0
        static void Main(string[] args)
        {
            Console.Title = "MyCompiler - Parser";

            var argManager = new ArgManager(args);

            bool debug = argManager.Contains("-d");
            //debug = true; // cancella questa riga dopo le fasi di test

            string sourceFilename = argManager.First();
            if(string.IsNullOrEmpty(sourceFilename))
                sourceFilename = argManager.Find("-s");

            // if DEBUG
            if (debug && string.IsNullOrEmpty(sourceFilename))
                sourceFilename = "lexoutput.txt";

            string outputFilename = argManager.Find("-o");
            if (string.IsNullOrEmpty(outputFilename))
                outputFilename = "parseoutput.txt";
            string bnfFilename = argManager.Find("-b");
            if (string.IsNullOrEmpty(bnfFilename))
                bnfFilename = "bnf.txt";
            string tableFilename = argManager.Find("-t");

            // Error
            if (!File.Exists(sourceFilename))
            {
                Console.WriteLine(String.Format("Error, source file: \"{0}\" not found!", sourceFilename));
                return;
            }
            if (!File.Exists(bnfFilename))
            {
                Console.WriteLine(String.Format("Error, BNF file: \"{0}\" not found!", bnfFilename));
                return;
            }

            // Load BNF
            Console.WriteLine("Reading BNF...");
            Console.WriteLine(string.Empty);

            var grammar = (new BNFParser(bnfFilename)).Parse();
            if (grammar == null)
            {
                Console.WriteLine("Exception: Error parsing BNF file...");
                return;
            }

            File.WriteAllText("grammar.txt", grammar.ToString());

            // input's tokens
            List<Token> tokens = new List<Token>();

            StreamReader rd = new StreamReader(sourceFilename);
            while(!rd.EndOfStream)
            {
                string line = rd.ReadLine();
                if (string.IsNullOrEmpty(line))
                    continue;

                try
                {
                    tokens.Add(Token.Parse(line));
                }
                catch(Exception e)
                {
                    Console.WriteLine("Exception, Error parsing tokens!");
                    Console.WriteLine(e.ToString());
                }
            }
            rd.Close();
            rd.Dispose();

            // Check the parsing Table

            var parsingTable = new ParsingTable(grammar);
            if (!string.IsNullOrEmpty(tableFilename))
            {
                File.WriteAllText(tableFilename, parsingTable.ToString());
            }

            Console.WriteLine(string.Empty);

            if (parsingTable.LeftFactored == false)
            {
                Console.WriteLine("Warning: The current grammar is not left-factored!");
                Console.WriteLine(string.Empty);
            }
            // Parsing

            var parser = new FrontEnd.Parser(grammar);
            var parseTree = parser.Parse(tokens);

            if (parseTree == null)
            {
                Console.WriteLine(parser.Error);
                File.WriteAllText(outputFilename, parser.Error);
            }
            else
            {
                Console.WriteLine("ParseTree:");
                Console.WriteLine(string.Empty);
                Console.WriteLine(parseTree.ToString());

                File.WriteAllText(outputFilename, parseTree.ToXML());

                Console.WriteLine(string.Empty);
                Console.WriteLine("Result: parsing done!");
            }

            if (debug)
                Console.ReadKey();
        }
 protected void SetUp()
 {
     _scanner = new Scanner();
     _parser = new Parser();
 }