Documentation

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

Example

Example of using AST tree for evaluation

BooleanInt

        private class BooleanInt
        {
            private bool _value;

            public BooleanInt(string text)
            {
                _value = (int.Parse(text) == 1);
            }

            public bool Value { get { return _value; } }
        }

Grammar & definitons

            Nonterminal<char> boolTarget = "bool[t]";
            boolTarget.Definition = new Rules<char>()
            {
                () => '0',
                () => '1',
            };

            Expression<char, BooleanInt> boolExpression = "bool[t]";
            boolExpression.Eval = (a, b) =>
            {
                if (b[0].Context.Token.Equals('0') || b[0].Context.Token.Equals('1'))
                {
                    return new BooleanInt(b[0].Context.ToString());
                }
                throw new NotSupportedException("Input token is not supported");
            };

            Parser<char> boolParser = "0".ToCharArray();
            boolTarget.Initialize();
            boolTarget.Validate();

            bool expected = true;
            bool actual = boolTarget.Parse(boolParser);
            Assert.AreEqual(expected, actual);

            bool result = boolExpression[boolParser.Tree].Value;
            Assert.AreEqual(false, result);

Last edited Dec 1, 2010 at 1:05 AM by hack2root, version 5

Comments

No comments yet.