예제 #1
0
        /// <summary>
        /// Evaluates a derivation of the form:
        /// Expression= Expression add Expression | Expression sub Expression | integer
        /// </summary>
        /// <remarks>
        /// The production rules are:
        /// Expression -> Expression add Expression
        /// Expression -> Expression sub Expression
        /// Expression -> integer
        /// </remarks>
        /// <param name="node">The <see cref="ParseNode"/> to evaluate</param>
        /// <param name="state">A user supplied state object. What it should be depends on the production's associated code block</param>
        /// <returns>The result of the evaluation</returns>
        internal static int EvaluateExpression(ParseNode node, object state)
        {
            if ((GloryDemo.Test2Parser.integer == node.SymbolId))
            {
                return((int)(Test2Parser._ChangeType(node.Value, typeof(int))));
            }
            if ((false == node.IsNonTerminal))
            {
                return((int)(Test2Parser._ChangeType(0, typeof(int))));
            }
            int result = Test2Parser.EvaluateExpression(node.Children[0], state);
            int i      = 2;

            for (
                ; (i < node.Children.Length);
                )
            {
                if ((node.Children[(i - 1)].SymbolId == GloryDemo.Test2Parser.add))
                {
                    result = (result + Test2Parser.EvaluateExpression(node.Children[i], state));
                }
                else
                {
                    result = (result - Test2Parser.EvaluateExpression(node.Children[i], state));
                }
                i = (i + 2);
            }
            return((int)(Test2Parser._ChangeType(result, typeof(int))));
        }
예제 #2
0
        static void Main(string[] args)
        {
            string input;

            //using (var sr = new StreamReader(@"..\..\data2.json"))
            //	input = sr.ReadToEnd();
            input = "d+d+d+d";
            var test1Tokenizer = new Test1Tokenizer(input);
            var test1Parser    = new Test1Parser(test1Tokenizer);

            foreach (var pt in test1Parser.ParseReductions(false, true, false))
            {
                Console.WriteLine(pt.ToString("t"));
                Console.WriteLine();
            }
            input = "1+3*-5";
            var expressionTokenizer = new ExpressionTokenizer(input);
            var expressionParser    = new ExpressionParser(expressionTokenizer);

            foreach (var pt in expressionParser.ParseReductions())
            {
                Console.WriteLine(pt.ToString("t"));
                Console.WriteLine();
                Console.WriteLine(ExpressionParser.Evaluate(pt));
            }
            Console.WriteLine();
            input = "1+5-3+2";
            var test2Tokenizer = new Test2Tokenizer(input);
            var test2Parser    = new Test2Parser(test2Tokenizer);

            foreach (var pt in test2Parser.ParseReductions())
            {
                if (!pt.HasErrors)
                {
                    Console.WriteLine(pt.ToString("t"));
                    Console.WriteLine();
                    Console.WriteLine(Test2Parser.Evaluate(pt));
                }
            }
            input = "(int)foo.bar * baz";
            var seTokenizer = new SlangExpressionTokenizer(input);
            var seParser    = new SlangExpressionParser(seTokenizer);

            foreach (var pt in seParser.ParseReductions())
            {
                if (!pt.HasErrors)
                {
                    Console.WriteLine(pt.ToString("t"));
                    Console.WriteLine();
                }
            }
        }
예제 #3
0
 /// <summary>
 /// Evaluates a derivation of the form:
 /// Expression= Expression add Expression | Expression sub Expression | integer
 /// </summary>
 /// <remarks>
 /// The production rules are:
 /// Expression -> Expression add Expression
 /// Expression -> Expression sub Expression
 /// Expression -> integer
 /// </remarks>
 /// <param name="node">The <see cref="ParseNode"/> to evaluate</param>
 /// <param name="state">A user supplied state object. What it should be depends on the production's associated code block</param>
 /// <returns>The result of the evaluation</returns>
 public static int Evaluate(ParseNode node, object state)
 {
     return(Test2Parser.EvaluateExpression(node, state));
 }
예제 #4
0
 /// <summary>
 /// Evaluates a derivation of the form:
 /// Expression= Expression add Expression | Expression sub Expression | integer
 /// </summary>
 /// <remarks>
 /// The production rules are:
 /// Expression -> Expression add Expression
 /// Expression -> Expression sub Expression
 /// Expression -> integer
 /// </remarks>
 /// <param name="node">The <see cref="ParseNode"/> to evaluate</param>
 /// <returns>The result of the evaluation</returns>
 public static int Evaluate(ParseNode node)
 {
     return(Test2Parser.EvaluateExpression(node, null));
 }