private Parser() { Lingua.Grammar grammar = new Lingua.Grammar(); grammar.Load(Assembly.GetCallingAssembly(), "Prolog"); grammar.LoadRules(Assembly.GetCallingAssembly(), "Prolog"); grammar.Resolve(); m_grammar = grammar; TerminalReaderGenerator terminalReaderGenerator = new TerminalReaderGenerator(); TerminalReaderGeneratorResult terminalReaderGeneratorResult = terminalReaderGenerator.GenerateTerminalReader(m_grammar); m_terminalReader = terminalReaderGeneratorResult.TerminalReader; ParserGenerator parserGenerator = new ParserGenerator(); ParserGeneratorResult parserGeneratorResult = parserGenerator.GenerateParser(m_grammar); m_parser = parserGeneratorResult.Parser; }
Parser() { var grammar = new Lingua.Grammar(); grammar.Load(Assembly.GetCallingAssembly(), "Prolog"); grammar.LoadRules(Assembly.GetCallingAssembly(), "Prolog"); grammar.Resolve(); Grammar = grammar; var terminalReaderGenerator = new TerminalReaderGenerator(); var terminalReaderGeneratorResult = terminalReaderGenerator.GenerateTerminalReader(Grammar); _terminalReader = terminalReaderGeneratorResult.TerminalReader; var parserGenerator = new ParserGenerator(); var parserGeneratorResult = parserGenerator.GenerateParser(Grammar); _parser = parserGeneratorResult.Parser; }
public void CreateParser() { Assembly assembly = Assembly.GetAssembly(typeof(Form1)); var grammar = new Grammar(); grammar.Load(assembly, "Calculator"); grammar.LoadRules(assembly, "Calculator"); grammar.Resolve(); Trace.WriteLine("TERMINALS"); foreach (var terminal in grammar.GetTerminals()) { Trace.WriteLine(terminal.Name); var sb = new StringBuilder(); sb.Append(" First:"); foreach (var first in terminal.First) { sb.Append(" "); sb.Append(first == null ? "e" : first.Name); } Trace.WriteLine(sb.ToString()); } Trace.WriteLine("NONTERMINALS"); foreach (var nonterminal in grammar.GetNonterminals()) { Trace.WriteLine(nonterminal.Name); foreach (var rule in nonterminal.Rules) { Trace.WriteLine(" " + rule.ToString()); } var sb = new StringBuilder(); sb.Append(" First:"); foreach (var first in nonterminal.First) { sb.Append(" "); sb.Append(first == null ? "e" : first.Name); } Trace.WriteLine(sb.ToString()); sb = new StringBuilder(); sb.Append(" Follow:"); foreach (var first in nonterminal.Follow) { sb.Append(" "); sb.Append(first == null ? "e" : first.Name); } Trace.WriteLine(sb.ToString()); } ITerminalReaderGenerator terminalReaderGenerator = new TerminalReaderGenerator(); TerminalReaderGeneratorResult terminalReaderGeneratorResult = terminalReaderGenerator.GenerateTerminalReader(grammar); this.m_terminalReader = (TerminalReader)terminalReaderGeneratorResult.TerminalReader; IParserGenerator parserGenerator = new ParserGenerator(); ParserGeneratorResult parserGeneratorResult = parserGenerator.GenerateParser(grammar); this.m_parser = (Parser)parserGeneratorResult.Parser; ITerminalReader terminalReader = terminalReaderGeneratorResult.TerminalReader; terminalReader.Open("(123 + 34) / 52"); IParser parser = parserGeneratorResult.Parser; parser.Parse(terminalReader); var token = terminalReader.ReadTerminal(); while (token != null) { if (!token.ElementType.Ignore) { Console.WriteLine("{0}: {1}", token.ElementType.Name, token.Text); } token = terminalReader.ReadTerminal(); } //Console.WriteLine("Press Enter to exit."); //Console.ReadLine(); }
public void CreateParser() { Assembly assembly = Assembly.GetAssembly(typeof(App)); Grammar grammar = new Grammar(); grammar.Load(assembly, "Prolog"); grammar.LoadRules(assembly, "Prolog"); grammar.Resolve(); Trace.WriteLine("TERMINALS"); foreach (TerminalType terminal in grammar.GetTerminals()) { Trace.WriteLine(terminal.Name); StringBuilder sb = new StringBuilder(); sb.Append(" First:"); foreach (TerminalType first in terminal.First) { sb.Append(" "); if (first == null) { sb.Append("e"); } else { sb.Append(first.Name); } } Trace.WriteLine(sb.ToString()); } Trace.WriteLine("NONTERMINALS"); foreach (NonterminalType nonterminal in grammar.GetNonterminals()) { StringBuilder sb; Trace.WriteLine(nonterminal.Name); foreach (RuleType rule in nonterminal.Rules) { Trace.WriteLine(" " + rule.ToString()); } sb = new StringBuilder(); sb.Append(" First:"); foreach (TerminalType first in nonterminal.First) { sb.Append(" "); if (first == null) { sb.Append("e"); } else { sb.Append(first.Name); } } Trace.WriteLine(sb.ToString()); sb = new StringBuilder(); sb.Append(" Follow:"); foreach (TerminalType first in nonterminal.Follow) { sb.Append(" "); if (first == null) { sb.Append("e"); } else { sb.Append(first.Name); } } Trace.WriteLine(sb.ToString()); } ITerminalReaderGenerator terminalReaderGenerator = new TerminalReaderGenerator(); TerminalReaderGeneratorResult terminalReaderGeneratorResult = terminalReaderGenerator.GenerateTerminalReader(grammar); m_terminalReader = terminalReaderGeneratorResult.TerminalReader; IParserGenerator parserGenerator = new ParserGenerator(); ParserGeneratorResult parserGeneratorResult = parserGenerator.GenerateParser(grammar); m_parser = parserGeneratorResult.Parser; //ITerminalReader terminalReader = terminalReaderGeneratorResult.TerminalReader; //terminalReader.Open("(123 + 34) / 52"); //IParser parser = parserGeneratorResult.Parser; //parser.Parse(terminalReader); //Terminal token = terminalReader.ReadTerminal(); //while (token != null) //{ // if (!token.ElementType.Ignore) // { // Console.WriteLine("{0}: {1}", token.ElementType.Name, token.Text); // } // token = terminalReader.ReadTerminal(); //} //Console.WriteLine("Press Enter to exit."); //Console.ReadLine(); }