Documentation

This is a fast and clean look of what the library is, helping you to understand the whole library:

Experimental

Information

This toolset follows PEG parser construction methodology, since it can be converted directly into recoverable recursive descent parser using preprcessor definition:


        /// <summary>
        /// Parses the specified text.
        /// </summary>
        /// <param name="parser">The input text</param>
        /// <returns>A boolean value</returns>
        public override bool Parse(Parser parser)
        {
            if (parser.Current.Char == Terminal.Null)
            {
                return false;
            }
            parser.Push();
            Stream start = parser.Current;
            foreach (Rule rule in _rules)
            {
#if RECURSION

                if (rule.Parse(parser))
                {
                    Stream end = parser.Current;
                    parser.Parse(new Context(this, Name, start, end));
                    return true;
                }
#else
                foreach (Terminal terminal in rule.First)
                {
                    if (terminal.Optional || parser.Read(terminal))
                    {
                        if (rule.Parse(parser))
                        {
                            Stream end = parser.Current;
                            parser.Parse(this, Name, start, end);
                            return true;
                        }
                    }
                }
#endif
            }
            parser.Pop();
            return false;
        }

Examples

WARNING! This is highly experimental and is not turned on by default in any of the project build configurations at the moment

This is uncommon use of nonterminals. Usually, nonterminals must be used using access modifier St, where S - nonterminal, t - flag indicating is that nonterminal can be optional in the rule stream or not

For example, lets build parser for the non-LL(1), non-LR(1) grammar:

S ← 'x' S 'x' | 'x'

The code for the test method might looks like this:


        [TestMethod]
        public void TestMethod1()
        {
            Terminal x = 'x';
            Nonterminal S = "S";

            S.Definition = new Rules()
            {
                () => x + S + x,
                () => x
            };

            S.Initialize();
            S.Validate();

            Parser input = "xxx";

            bool actual = S.Parse(input);
            bool expected = true;

            Assert.AreEqual(expected, actual);
        }

Simply look into output from the test run under the solution's Trace build for the selected unit test)

Remarks

If you are out of luck finding sources for the implicit Nonterminal & Terminal to Token conversion functions for the goal of compilation amibigous grammar, please look into the previous releases, i.e v 1.0.2.1.
By comparing with Nonterminal, Terminal & Tokens class definitions, you will find such a code which currently is out of scope of the actual source tree distribution.

Last edited Nov 30, 2010 at 2:55 PM by hack2root, version 9

Comments

No comments yet.