QUT.GPGen.CodeGenerator.Generate C# (CSharp) Method

Generate() private method

private Generate ( List states, Grammar grammar ) : void
states List
grammar Grammar
return void
        internal void Generate(List<AutomatonState> states, Grammar grammar)
        {
            StreamWriter tWrtr = null;
            StreamWriter sWrtr = null;
            StreamWriter cWrtr = null;
            TextWriter   save = Console.Out;

            this.grammar = grammar;
            if (grammar.OutFileName != null)
            {
                try
                {
                    FileStream fStrm = new FileStream(grammar.OutFileName, FileMode.Create);
                    sWrtr = new StreamWriter(fStrm);
                    Console.WriteLine("GPPG: sending output to {0}", grammar.OutFileName);
                    Console.SetOut(sWrtr);
                }
                catch (IOException x)
                {
                    Console.Error.WriteLine("GPPG: Error. File redirect failed");
                    Console.Error.WriteLine(x.Message);
                    Console.Error.WriteLine("GPPG: Terminating ...");
                    Environment.Exit(1);
                }
            }

            if (grammar.TokFileName != null) // generate token list file
            {
                try
                {
                    FileStream fStrm = new FileStream(grammar.TokFileName, FileMode.Create);
                    tWrtr = new StreamWriter(fStrm);
                    tWrtr.WriteLine("// Symbolic tokens for parser for grammar file \"{0}\"", grammar.InputFileName);
                }
                catch (IOException x)
                {
                    Console.Error.WriteLine("GPPG: Error. Failed to create token namelist file");
                    Console.Error.WriteLine(x.Message);
                    tWrtr = null;
                }
            }

            if (grammar.DiagFileName != null) // generate conflict list file
            {
                try
                {
                    FileStream cStrm = new FileStream(grammar.DiagFileName, FileMode.Create);
                    cWrtr = new StreamWriter(cStrm);
                    cWrtr.WriteLine("// Parser Conflict Information for grammar file \"{0}\"", grammar.InputFileName);
                    cWrtr.WriteLine();
                }
                catch (IOException x)
                {
                    Console.Error.WriteLine("GPPG: Error. Failed to create conflict information file");
                    Console.Error.WriteLine(x.Message);
                    cWrtr = null;
                }
            }

            GenerateCopyright();

            GenerateUsingHeader();

            if (grammar.Namespace != null)
            {
                Console.WriteLine("namespace {0}", grammar.Namespace);
                Console.WriteLine('{');
            }

            GenerateTokens(grammar.terminals, tWrtr);
            grammar.ReportConflicts(cWrtr);

            GenerateClassHeader(grammar.ParserName);
            foreach (LexSpan span in grammar.prologCode)
            {
                InsertCodeSpan(span);
            }
            GenerateInitializeMethod(states, grammar.productions, grammar.nonTerminals);
            GenerateActionMethod(grammar.productions);
            GenerateToStringMethod();
            InsertCodeSpan(grammar.epilogCode);
            GenerateClassFooter();

            if (grammar.Namespace != null)
                Console.WriteLine('}');

            if (tWrtr != null)
            {
                tWrtr.WriteLine("// End symbolic tokens for parser");
                tWrtr.Close(); // Close the optional token name stream
            }

            if (cWrtr != null)
            {
                cWrtr.WriteLine("// End conflict information for parser");
                cWrtr.Close(); // Close the optional token name stream
            }

            if (sWrtr != null)
            {
                Console.SetOut(save);
                sWrtr.Close();
            }
        }

Usage Example

Esempio n. 1
0
File: Main.cs Progetto: kzyg/spark
        private static void Main(string[] args)
        {
            Stream       inputFile = null;
            Grammar      grammar   = null;
            ErrorHandler handler   = new ErrorHandler();

            Lexers.Scanner scanner = null;
            Parser.Parser  parser  = null;

            Assembly assm = Assembly.GetExecutingAssembly();
            object   info = Attribute.GetCustomAttribute(assm, typeof(AssemblyFileVersionAttribute));

            versionInfo = ((AssemblyFileVersionAttribute)info).Version;

            try
            {
                string filename = ProcessOptions(args);

                if (filename == null)
                {
                    return;
                }

                try
                {
                    inputFile = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
                }
                catch (IOException)
                {
                    inputFile = null;
                    string message = String.Format(CultureInfo.InvariantCulture, "Source file <{0}> not found{1}", filename, Environment.NewLine);
                    handler.AddError(message, null); // aast.AtStart;
                    throw;
                }

                scanner = new Lexers.Scanner(inputFile);
                scanner.SetHandler(handler);

                parser = new Parser.Parser(filename, scanner, handler);
                //
                // If the parse is successful, then process the grammar.
                // Otherwise just report the errors that have been listed.
                //
                if (parser.Parse())
                {
                    grammar = parser.Grammar;

                    if (Terminal.Max > 255)
                    {
                        handler.ListError(null, 103, CharacterUtilities.Map(Terminal.Max), '\'');
                    }

                    LALRGenerator         generator = new LALRGenerator(grammar);
                    List <AutomatonState> states    = generator.BuildStates();
                    generator.ComputeLookAhead();
                    generator.BuildParseTable();
                    if (!grammar.CheckGrammar(handler))
                    {
                        throw new ArgumentException("Non-terminating grammar");
                    }
                    //
                    // If the grammar has non-terminating non-terms we cannot
                    // create a diagnostic report as the grammar is incomplete.
                    //
                    bool DoDiagnose = Diagnose && !grammar.HasNonTerminatingNonTerms;

                    if (Report || DoDiagnose)
                    {
                        string htmlName = System.IO.Path.ChangeExtension(filename, ".report.html");
                        try
                        {
                            System.IO.FileStream   htmlFile   = new System.IO.FileStream(htmlName, System.IO.FileMode.Create);
                            System.IO.StreamWriter htmlWriter = new System.IO.StreamWriter(htmlFile);
                            Grammar.HtmlHeader(htmlWriter, filename);

                            if (Report && DoDiagnose)
                            {
                                grammar.GenerateCompoundReport(htmlWriter, filename, states);
                            }
                            else if (Report)
                            {
                                grammar.GenerateReport(htmlWriter, filename, states);
                            }

                            Grammar.HtmlTrailer(htmlWriter);

                            if (htmlFile != null)
                            {
                                htmlWriter.Flush();
                                htmlFile.Close();
                            }
                        }
                        catch (System.IO.IOException)
                        {
                            Console.Error.WriteLine("Cannot create html output file {0}", htmlName);
                        }
                    }
                    else if (!handler.Errors)
                    {
                        CodeGenerator code = new CodeGenerator();
                        code.Generate(states, grammar);
                    }
                }
            }
            catch (System.Exception e)
            {
                Console.Error.WriteLine("Unexpected Error {0}", e.Message);
                throw; // Now rethrow the caught exception.
            }
            finally
            {
                if ((handler.Errors || handler.Warnings) && scanner != null)
                {
                    handler.DumpAll(scanner.Buffer, Console.Error);
                }
                if (Listing || handler.Errors || handler.Warnings)
                {
                    string       listName   = parser.ListfileName;
                    StreamWriter listStream = ListingFile(listName);
                    if (listStream != null)
                    {
                        handler.MakeListing(scanner.Buffer, listStream, parser.SourceFileName, versionInfo);
                    }
                }
            }
        }
All Usage Examples Of QUT.GPGen.CodeGenerator::Generate