Inheritance: Symbol
Exemplo n.º 1
0
        private void GenerateInitializeMethod(
            List <AutomatonState> states,
            List <Production> productions,
            Dictionary <string, NonTerminal> nonTerminals)
        {
            // warning 649 : this field never assigned to.
            Console.WriteLine("#pragma warning disable 649");
            Console.WriteLine("  private static Dictionary<int, string> aliases;");
            Console.WriteLine("#pragma warning restore 649");
            Console.WriteLine("  private static Rule[] rules = new Rule[{0}];", productions.Count + 1);
            Console.WriteLine("  private static State[] states = new State[{0}];", states.Count);
            Console.WriteLine("  private static string[] nonTerms = new string[] {");

            int length = 0;

            Console.Write("      ");
            foreach (NonTerminal nonTerminal in nonTerminals.Values)
            {
                string ss = String.Format(CultureInfo.InvariantCulture, "\"{0}\", ", nonTerminal.ToString());
                length += ss.Length;
                Console.Write(ss);
                if (length > 70)
                {
                    Console.WriteLine();
                    Console.Write("      ");
                    length = 0;
                }
            }
            Console.WriteLine("};");
            Console.WriteLine();

            Console.WriteLine("  static {0}() {{", grammar.ParserName);
            int state_nr = 0;

            foreach (AutomatonState state in states)
            {
                GenerateShiftReduceMachineState(state_nr++, state);
            }
            Console.WriteLine();

            Console.WriteLine("    for (int sNo = 0; sNo < states.Length; sNo++) states[sNo].number = sNo;");

            Console.WriteLine();
            foreach (Production production in productions)
            {
                GenerateShiftReduceMachineRule(production);
            }

            List <Terminal> aliasList = new List <Terminal>();

            foreach (KeyValuePair <string, Terminal> pair in grammar.terminals)
            {
                Terminal term = pair.Value;
                if (term.Alias != null)
                {
                    aliasList.Add(term);
                }
            }
            if (aliasList.Count > 0)
            {
                Console.WriteLine();
                Console.WriteLine("    aliases = new Dictionary<int, string>();");
                foreach (Terminal termWithAlias in aliasList)
                {
                    Console.WriteLine("    aliases.Add({0}, {1});",
                                      termWithAlias.num,
                                      CharacterUtilities.QuoteAndCanonicalize(termWithAlias.Alias));
                }
            }
            Console.WriteLine("  }");
            Console.WriteLine();

            Console.WriteLine("  protected override void Initialize() {");
            Console.WriteLine("    this.InitSpecialTokens((int){0}.error, (int){0}.EOF);", grammar.TokenName);
            Console.WriteLine("    this.InitStates(states);");
            Console.WriteLine("    this.InitRules(rules);");
            Console.WriteLine("    this.InitNonTerminals(nonTerms);");
            Console.WriteLine("  }");
            Console.WriteLine();
        }
Exemplo n.º 2
0
 internal ShiftReduceConflict(Terminal sy, string s1, string s2, AutomatonState from, AutomatonState to)
     : base(sy, s1, s2)
 {
     fromState = from; toState = to;
     fromState.Link(this);
 }
Exemplo n.º 3
0
        internal void Generate(List <AutomatonState> states)
        {
            StreamWriter tWrtr   = null;
            StreamWriter cstWrtr = null;
            StreamWriter sWrtr   = null;
            StreamWriter cWrtr   = null;
            TextWriter   save    = Console.Out;
            //
            // Did we specify output filename in command line or
            // the file (if both, the command line takes precedence)?
            //
            string outFile = GPCG.OutFileName;

            if (outFile == null)
            {
                outFile = grammar.OutFileName;
            }

            if (outFile != null)
            {
                try
                {
                    FileStream fStrm = new FileStream(outFile, FileMode.Create);
                    sWrtr = new StreamWriter(fStrm);
                    Console.WriteLine("GPPG: sending output to {0}", outFile);
                    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 grammar file \"{0}\"", grammar.InputFileIdent);
                }
                catch (IOException x) {
                    Console.Error.WriteLine("GPPG: Error. Failed to create token namelist file");
                    Console.Error.WriteLine(x.Message);
                    tWrtr = null;
                }
            }

            if (grammar.CsTokFileName != null) // generate C# token declaration file
            {
                try {
                    FileStream fStrm = new FileStream(grammar.CsTokFileName, FileMode.Create);
                    cstWrtr = new StreamWriter(fStrm);
                    cstWrtr.WriteLine("// Token declarations  for grammar file \"{0}\"", grammar.InputFileIdent);
                }
                catch (IOException x) {
                    Console.Error.WriteLine("GPPG: Error. Failed to create C# token declaration file");
                    Console.Error.WriteLine(x.Message);
                    cstWrtr = null;
                }
            }

            if (GPCG.ShareTokens && grammar.DatFileName != null) // serialize Terminals dictionary.
            {
                FileStream fStrm = null;
                try {
                    // Insert marker to carry Terminal.max into the serialized structure.
                    Terminal.InsertMaxDummyTerminalInDictionary(grammar.terminals);

                    fStrm = new FileStream(grammar.DatFileName, FileMode.Create);
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(fStrm, grammar.terminals);
                }
                catch (IOException x) {
                    Console.Error.WriteLine("GPPG: Error. Failed to create token serialization file");
                    Console.Error.WriteLine(x.Message);
                }
                finally {
                    if (fStrm != null)
                    {
                        fStrm.Close();
                    }
                    Terminal.RemoveMaxDummyTerminalFromDictionary(grammar.terminals);
                }
            }

            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.InputFileIdent);
                    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('{');
            }
            //
            // Emit token enumeration
            //
            if (!GPCG.ImportedTokens)
            {
                if (GPCG.CsTokenFile)
                {
                    GenerateCsTokenFile(cstWrtr, tWrtr);
                }
                else
                {
                    GenerateInlineTokens(grammar.terminals, tWrtr);
                }
            }
            //
            // Report any conflicts
            //
            grammar.ReportConflicts(cWrtr);
            //
            // Now emit the parser class.
            //
            GenerateClassHeader(grammar.ParserName);
            if (grammar.prologCode.Count > 0)
            {
                Console.WriteLine("  // Verbatim content from {0}", grammar.InputFileIdent);
                foreach (LexSpan span in grammar.prologCode)
                {
                    InsertCodeSpan(span);
                }
                Console.WriteLine("  // End verbatim content from {0}", grammar.InputFileIdent);
                Console.WriteLine();
            }
            GenerateInitializeMethod(states, grammar.productions, grammar.nonTerminals);
            GenerateShiftReduceMachineActions(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 (cstWrtr != null)
            {
                cstWrtr.Close(); // Close the optional token declaration 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();
            }
        }
Exemplo n.º 4
0
 internal Conflict(Terminal sy, string s1, string s2)
 {
     symbol = sy; str1 = s1; str2 = s2;
 }