示例#1
0
文件: Parser.cs 项目: Slesa/Lingua
        /// <summary>
        /// Performs syntax analysis against a sequence of terminals according to the <see cref="Grammar"/> used to create the <see cref="Parser"/>.
        /// </summary>
        /// <param name="terminalReader">Retrieves a sequence of <see cref="Terminal"/> objects.</param>
        /// <returns>If syntax analysis succeeds, returns the <see cref="Nonterminal"/> associated with <see cref="Grammar.StartNonterminal"/>.  Otherwise, <value>null</value> is returned.</returns>
        public Nonterminal Parse(ITerminalReader terminalReader)
        {
            var stack = new ParserStack();
            stack.Push(null, InitialState);

            var terminal = terminalReader.ReadTerminal();
            while (terminal != null)
            {
                if (terminal.ElementType.Ignore)
                {
                    terminal = terminalReader.ReadTerminal();
                }
                else
                {
                    var action = stack.Peek().State.GetAction(terminal.ElementType);

                    LinguaTrace.TraceEvent(TraceEventType.Information, LinguaTraceId.ID_PARSE_ACTION, "{0}", action);

                    if (action == null)
                    {
                        return null;
                    }

                    switch (action.ActionType)
                    {
                        case ParserActionTypes.Accept:
                            {
                                var reduce = (ParserActionAccept)action;
                                var rule = reduce.Rule;
                                var lhs = Reduce(stack, rule);
                                return lhs;
                            }

                        case ParserActionTypes.Shift:
                            {
                                var shift = (ParserActionShift)action;
                                stack.Push(terminal, shift.State);
                                terminal = terminalReader.ReadTerminal();
                            }
                            break;

                        case ParserActionTypes.Reduce:
                            {
                                var reduce = (ParserActionReduce)action;
                                var rule = reduce.Rule;
                                var lhs = Reduce(stack, rule);

                                // Push the LHS nonterminal on the stack.
                                //
                                stack.Push(lhs, stack.Peek().State.GetGoto(lhs.ElementType));
                            }
                            break;

                        default:
                            throw new InvalidOperationException(string.Format("Unrecognized action type {0}.", action.ActionType));
                    }
                }
            }
            return null;
        }
示例#2
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;
        }
示例#3
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;
        }
示例#4
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;
        }
示例#5
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;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TerminalReaderGeneratorResult"/> class.
 /// </summary>
 /// <param name="terminalReader">The <see cref="ITerminalReader"/> created by the <see cref="ITerminalReaderGenerator"/>.</param>
 public TerminalReaderGeneratorResult(ITerminalReader terminalReader)
 {
     TerminalReader = terminalReader;
 }
示例#7
0
        /// <summary>
        /// Performs syntax analysis against a sequence of terminals according to the <see cref="Grammar"/> used to create the <see cref="Parser"/>.
        /// </summary>
        /// <param name="terminalReader">Retrieves a sequence of <see cref="Terminal"/> objects.</param>
        /// <returns>If syntax analysis succeeds, returns the <see cref="Nonterminal"/> associated with <see cref="Grammar.StartNonterminal"/>.  Otherwise, <value>null</value> is returned.</returns>
        public Nonterminal Parse(ITerminalReader terminalReader)
        {
            ParserStack stack = new ParserStack();

            stack.Push(null, InitialState);

            Terminal terminal = terminalReader.ReadTerminal();

            while (terminal != null)
            {
                if (terminal.ElementType.Ignore)
                {
                    terminal = terminalReader.ReadTerminal();
                }
                else
                {
                    ParserAction action = stack.Peek().State.GetAction(terminal.ElementType);

                    LinguaTrace.TraceEvent(TraceEventType.Information, LinguaTraceId.ID_PARSE_ACTION, "{0}", action);

                    if (action == null)
                    {
                        return(null);
                    }

                    switch (action.ActionType)
                    {
                    case ParserActionTypes.Accept:
                    {
                        ParserActionAccept reduce = (ParserActionAccept)action;
                        RuleType           rule   = reduce.Rule;

                        Nonterminal lhs = Reduce(stack, rule);

                        return(lhs);
                    }

                    case ParserActionTypes.Shift:
                    {
                        ParserActionShift shift = (ParserActionShift)action;

                        stack.Push(terminal, shift.State);

                        terminal = terminalReader.ReadTerminal();
                    }
                    break;

                    case ParserActionTypes.Reduce:
                    {
                        ParserActionReduce reduce = (ParserActionReduce)action;
                        RuleType           rule   = reduce.Rule;

                        Nonterminal lhs = Reduce(stack, rule);

                        // Push the LHS nonterminal on the stack.
                        //
                        stack.Push(lhs, stack.Peek().State.GetGoto(lhs.ElementType));
                    }
                    break;

                    default:
                        throw new InvalidOperationException(string.Format("Unrecognized action type {0}.", action.ActionType));
                    }
                }
            }
            ;

            return(null);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TerminalReaderGeneratorResult"/> class.
 /// </summary>
 /// <param name="terminalReader">The <see cref="ITerminalReader"/> created by the <see cref="ITerminalReaderGenerator"/>.</param>
 public TerminalReaderGeneratorResult(ITerminalReader terminalReader)
 {
     m_terminalReader = terminalReader;
 }
示例#9
0
        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();
        }
示例#10
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();
        }
示例#11
0
文件: App.xaml.cs 项目: Slesa/Lingua
        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();
        }