示例#1
0
文件: MainForm.cs 项目: Rolf321/p4
        private void BuildParseTree(NonTerminalNode nonTerminal)
        {
            tvParseTree.Nodes.Clear();
            TreeNode node = new TreeNode(nonTerminal.Rule.ToString(),
                                         Convert.ToInt32(IconType.Reduction),
                                         Convert.ToInt32(IconType.Reduction));

            tvParseTree.Nodes.Add(node);
            for (int i = 0; i < nonTerminal.Count; i++)
            {
                SyntaxNode childNode = nonTerminal[i];
                BuildParseTree(childNode, node);
            }
            node.Expand();
        }
示例#2
0
文件: MainForm.cs 项目: Rolf321/p4
        private void ParseText()
        {
            ClearData();
            VBScriptParser parser = new VBScriptParser();

            parser.ParseAction = new ParseActionDelegate(AddParseAction);
            StringReader    reader     = new StringReader(tbSource.Text);
            NonTerminalNode syntaxNode = parser.Parse(reader) as NonTerminalNode;

            if (syntaxNode != null)
            {
                BuildParseTree(syntaxNode);
                tcPages.SelectedIndex = 2;
            }
            else
            {
                tcPages.SelectedIndex = 1;
            }
        }
示例#3
0
文件: MainForm.cs 项目: Rolf321/p4
        private void BuildParseTree(SyntaxNode syntaxNode, TreeNode parentNode)
        {
            NonTerminalNode nonTerminal = syntaxNode as NonTerminalNode;

            if (nonTerminal != null)
            {
                TreeNode node = new TreeNode(nonTerminal.Rule.ToString(),
                                             Convert.ToInt32(IconType.Reduction),
                                             Convert.ToInt32(IconType.Reduction));
                parentNode.Nodes.Add(node);
                for (int i = 0; i < nonTerminal.Count; i++)
                {
                    SyntaxNode childNode = nonTerminal[i];
                    BuildParseTree(childNode, node);
                }
            }
            else
            {
                TreeNode node = new TreeNode(syntaxNode.ToString(),
                                             Convert.ToInt32(IconType.Token),
                                             Convert.ToInt32(IconType.Token));
                parentNode.Nodes.Add(node);
            }
        }
示例#4
0
        /// <summary>
        /// Parsers the given string.
        /// </summary>
        /// <param name="value">String to parse.</param>
        /// <returns>
        /// Topmost reduction if the parsing was successful.
        /// Retuns null if the value was not parsed.
        /// </returns>
        public SyntaxNode Parse(TextReader reader)
        {
            int    reductionNumber = 0;
            Parser parser          = new Parser(reader, m_grammar);

            parser.TrimReductions = true;

            while (true)
            {
                ParseMessage response = parser.Parse();
                switch (response)
                {
                case ParseMessage.LexicalError:
                    AddParseAction(parser, response, "Cannot Recognize Token",
                                   "", parser.TokenText, "");
                    return(null);

                case ParseMessage.SyntaxError:
                    StringBuilder expectedTokens = new StringBuilder();
                    foreach (Symbol token in parser.GetExpectedTokens())
                    {
                        expectedTokens.Append(token.Name);
                        expectedTokens.Append(' ');
                    }
                    AddParseAction(parser, response, "Expecting the following tokens",
                                   "", expectedTokens.ToString(), "");
                    return(null);

                case ParseMessage.Reduction:
                    NonTerminalNode nonTerminal = new NonTerminalNode(parser.ReductionRule);
                    nonTerminal.ReductionNumber = ++reductionNumber;
                    parser.TokenSyntaxNode      = nonTerminal;
                    StringBuilder childReductionList = new StringBuilder();
                    for (int i = 0; i < parser.ReductionCount; i++)
                    {
                        SyntaxNode node = parser.GetReductionSyntaxNode(i) as SyntaxNode;
                        nonTerminal.Add(node);
                        NonTerminalNode childNode = node as NonTerminalNode;
                        if (childNode != null)
                        {
                            childReductionList.Append('#');
                            childReductionList.Append(childNode.ReductionNumber);
                            childReductionList.Append(' ');
                        }
                    }
                    AddParseAction(parser, response, nonTerminal.Rule.ToString(),
                                   reductionNumber.ToString(),
                                   childReductionList.ToString(),
                                   nonTerminal.Rule.Index.ToString());
                    break;

                case ParseMessage.Accept:                               //=== Success!
                    AddParseAction(parser, response, parser.ReductionRule.ToString(),
                                   "", "", "");
                    return((SyntaxNode)parser.TokenSyntaxNode);

                case ParseMessage.TokenRead:
                    TerminalNode terminal = new TerminalNode(
                        parser.TokenSymbol,
                        parser.TokenText,
                        parser.TokenString,
                        parser.TokenLineNumber,
                        parser.TokenLinePosition);
                    parser.TokenSyntaxNode = terminal;
                    AddParseAction(parser, response,
                                   terminal.Symbol.Name, "",
                                   terminal.ToString(),
                                   terminal.Symbol.Index.ToString());
                    break;

                case ParseMessage.InternalError:
                    AddParseAction(parser, response, "Error in LR state engine",
                                   "", "", "");
                    return(null);

                case ParseMessage.NotLoadedError:
                    //=== Due to the if-statement above, this case statement should never be true
                    AddParseAction(parser, response, "Compiled Grammar Table not loaded",
                                   "", "", "");
                    return(null);

                case ParseMessage.CommentError:
                    AddParseAction(parser, response, "Unexpected end of file",
                                   "", "", "");
                    return(null);
                }
            }
        }