Exemple #1
0
 // Rule
 // Constant | Function | Variable
 public Rule ParseRule()
 {
     Rule r;
     if (currentToken.Type == TokenType.Identifier)
     {
         if (lexer.Peek().Type == TokenType.LeftParen) //Function
         {
             r = new Rule(Type.Function);
             r.Label = currentToken.Lexeme;
             Advance();
             Advance(); //'('
             r.ArgList = ParseArgList();
             Advance(); // ')'
         }
         else                                        // Constant
         {
             r = new Rule(Type.Constant);
             r.Label = currentToken.Lexeme;
             Advance();
         }
     }
     else                                            // variable
     {
         r = new Rule(Type.Variable);
         r.Label = currentToken.Lexeme;
         Advance();
     }
     return r;
 }
        /// <summary>
        /// The rules for unifying Rules in predicates.
        /// </summary>
        /// <param name="Rulei"></param>
        /// <param name="Rulej"></param>
        /// <returns> true if the two rules can unify, false if they can't. </returns>
        private bool CanUnify(Rule Rulei, Rule Rulej)
        {
            // Variables can resolve with other variables.
            if ((Rulei.Type == Type.Variable && Rulej.Type == Type.Variable) || (Rulej.Type == Type.Variable && Rulei.Type == Type.Variable))
                return true;

            // Constants can resolve with variables.
            if ((Rulei.Type == Type.Variable && Rulej.Type == Type.Constant) || (Rulei.Type == Type.Constant && Rulej.Type == Type.Variable))
                return true;

            // A constant can resolve with the variable of a function.
            if ((Rulei.Type == Type.Constant && Rulej.Type == Type.Function) || (Rulei.Type == Type.Function && Rulej.Type == Type.Constant))
                return true;

            // A variable can resolve with a function
            if ((Rulei.Type == Type.Variable && Rulej.Type == Type.Function) || (Rulei.Type == Type.Function && Rulej.Type == Type.Variable))
                return true;

            // A function can resolve with another function of the same type.
            if (((Rulei.Type == Type.Function && Rulej.Type == Type.Function) || (Rulej.Type == Type.Function && Rulei.Type == Type.Function)) && Rulei.Label == Rulej.Label)
                return true;

            return false;
        }