Esempio n. 1
0
        public void TestOperatorPrecedence()
        {
            var grammar = new OperatorGrammar();
            var parser  = new Parser(grammar);

            TestHelper.CheckGrammarErrors(parser);

            var parseTree = parser.Parse("x + y * z");

            TestHelper.CheckParseErrors(parseTree);
            Assert.IsTrue(parseTree.Root != null, "Root not found.");
            Assert.IsTrue(parseTree.Root.Term.Name == "binExpr", "Expected binExpr.");
            Assert.IsTrue(parseTree.Root.ChildNodes[1].Term.Name == "+", "Expected + operator."); //check that top operator is "+", not "*"

            parseTree = parser.Parse("x * y + z");
            TestHelper.CheckParseErrors(parseTree);
            Assert.IsTrue(parseTree.Root != null, "Root not found.");
            Assert.IsTrue(parseTree.Root.Term.Name == "binExpr", "Expected binExpr.");
            Assert.IsTrue(parseTree.Root.ChildNodes[1].Term.Name == "+", "Expected + operator."); //check that top operator is "+", not "*"

            parseTree = parser.Parse("-x * y");                                                   //should be interpreted as -(x*y), so top operator should be -
            TestHelper.CheckParseErrors(parseTree);
            Assert.IsTrue(parseTree.Root != null, "Root not found.");
            Assert.IsTrue(parseTree.Root.Term.Name == "unExpr", "Expected unExpr.");
            Assert.IsTrue(parseTree.Root.ChildNodes[0].Term.Name == "-", "Expected - operator."); //check that top operator is "+", not "*"
        }
Esempio n. 2
0
    public void TestOperatorPrecedence() {

      var grammar = new OperatorGrammar();
      var parser = new Parser(grammar);
      TestHelper.CheckGrammarErrors(parser);

      var parseTree = parser.Parse("x + y * z");
      TestHelper.CheckParseErrors(parseTree);
      Assert.IsTrue(parseTree.Root != null, "Root not found.");
      Assert.IsTrue(parseTree.Root.Term.Name == "binExpr", "Expected binExpr.");
      Assert.IsTrue(parseTree.Root.ChildNodes[1].Term.Name == "+", "Expected + operator."); //check that top operator is "+", not "*"

      parseTree = parser.Parse("x * y + z");
      TestHelper.CheckParseErrors(parseTree);
      Assert.IsTrue(parseTree.Root != null, "Root not found.");
      Assert.IsTrue(parseTree.Root.Term.Name == "binExpr", "Expected binExpr.");
      Assert.IsTrue(parseTree.Root.ChildNodes[1].Term.Name == "+", "Expected + operator."); //check that top operator is "+", not "*"

      parseTree = parser.Parse("-x * y"); //should be interpreted as -(x*y), so top operator should be -
      TestHelper.CheckParseErrors(parseTree);
      Assert.IsTrue(parseTree.Root != null, "Root not found.");
      Assert.IsTrue(parseTree.Root.Term.Name == "unExpr", "Expected unExpr.");
      Assert.IsTrue(parseTree.Root.ChildNodes[0].Term.Name == "-", "Expected - operator."); //check that top operator is "+", not "*"

    }