示例#1
0
        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;
        }
示例#2
0
        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;
        }
示例#3
0
文件: App.xaml.cs 项目: Slesa/Lingua
        public void CreateParser()
        {
            var assembly = Assembly.GetAssembly(typeof(App));

            var grammar = new Grammar();
            grammar.Load(assembly, "Prolog");
            grammar.LoadRules(assembly, "Prolog");
            grammar.Resolve();

            Trace.WriteLine("TERMINALS");
            foreach (var terminal in grammar.GetTerminals())
            {
                Trace.WriteLine(terminal.Name);
                StringBuilder 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);
                }

                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();
            var terminalReaderGeneratorResult = terminalReaderGenerator.GenerateTerminalReader(grammar);
            TerminalReader = terminalReaderGeneratorResult.TerminalReader;

            IParserGenerator parserGenerator = new ParserGenerator();
            var parserGeneratorResult = parserGenerator.GenerateParser(grammar);
            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();
        }