コード例 #1
0
        public void TestOperatorPrecedenceWithHints()
        {
            var grammar = new OperatorGrammarHintsOnTerms();
            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");                                                   //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 == "binExpr", "Expected binExpr.");
            Assert.IsTrue(parseTree.Root.ChildNodes[1].Term.Name == "*", "Expected * operator."); //check that top operator is "+", not "*"

            var grammar2 = new OperatorGrammarHintsOnNonTerms();

            parser = new Parser(grammar2);
            TestHelper.CheckGrammarErrors(parser);

            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 == "binExpr", "Expected binExpr.");
            Assert.IsTrue(parseTree.Root.ChildNodes[1].Term.Name == "*", "Expected * operator."); //check that top operator is "+", not "*"
        }
コード例 #2
0
    public void TestOperatorPrecedenceWithHints() {

      var grammar = new OperatorGrammarHintsOnTerms();
      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"); //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 == "binExpr", "Expected binExpr.");
      Assert.IsTrue(parseTree.Root.ChildNodes[1].Term.Name == "*", "Expected * operator."); //check that top operator is "+", not "*"

      var grammar2 = new OperatorGrammarHintsOnNonTerms();
      parser = new Parser(grammar2);
      TestHelper.CheckGrammarErrors(parser);

      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 == "binExpr", "Expected binExpr.");
      Assert.IsTrue(parseTree.Root.ChildNodes[1].Term.Name == "*", "Expected * operator."); //check that top operator is "+", not "*"
    
    }