Exemplo n.º 1
0
 public static SyntacticElement Parse(List <string> lineElements)
 {
     if (string.Compare("therefore", lineElements[0], StringComparison.OrdinalIgnoreCase) == 0)
     {
         ThereforeSyntacticElement therefore = new ThereforeSyntacticElement();
         // get lhs prop p and q imp z
         List <string>    expressionText = lineElements.Skip(1).ToList();
         SyntacticElement expression     = ExpressionParser.Parse(expressionText);
         // check lhs
         if (expression == null)
         {
             return(new InvalidSyntacticElement("No expression for therefore", "Nothing to be proved? why are you here?"));
         }
         else if (expression is InvalidSyntacticElement)
         {
             return(new InvalidSyntacticElement("Invalid expression for therefore", ((InvalidSyntacticElement)expression).Message));
         }
         else if (expression is ExpressionSyntacticElement)
         {
             therefore.expression = (ExpressionSyntacticElement)expression;
         }
         else
         {
             throw new InvalidOperationException();
         }
         return(therefore);
     }
     else
     {   // this is here if someone changes the design.
         return(new InvalidSyntacticElement("No therefore header", "cannot parse what does not exist"));
     }
 }
Exemplo n.º 2
0
        private static SyntacticElement parseStandardProposition(List <string> lineElements)
        {
            PropositionExpression proposition;
            int index = -1;

            index = IndexOfImplies(lineElements);
            if (index < 0)
            {
                index = IndexOfIfAndOnlyIf(lineElements);
                if (index < 0)
                {
                    throw new FormatException("no proposition element");
                }
                proposition = new IfAndOnlyIfSyntacticExpression();
            }
            else
            {
                proposition = new ImplicationSyntacticExpression();
            }

            // get lhs prop p and q imp z
            List <string>    lhsText = lineElements.Skip(1).Take(index - 1).ToList();
            SyntacticElement lhs     = ExpressionParser.Parse(lhsText);

            // check lhs
            if (lhs is ExpressionSyntacticElement)
            {
                proposition.lhs = (ExpressionSyntacticElement)lhs;
            }
            else
            {
                return(ProcessLhsError(lhs));
            }

            //           0  1  2  3  4  5  6  7     8 - X = 3
            // get rhs prop p and q imp z and k     index = 4
            //           0  1  2  3  4  5     6 - X = 3
            // get rhs prop p imp z and k     index = 2
            List <string>    rhsText = lineElements.Skip(index + 1).Take(lineElements.Count() - (index + 1)).ToList();
            SyntacticElement rhs     = ExpressionParser.Parse(rhsText);

            if (rhs is ExpressionSyntacticElement)
            {
                proposition.rhs = (ExpressionSyntacticElement)rhs;
            }
            else
            {
                return(ProcessRhsError(rhs));
            }
            return(proposition);
        }
Exemplo n.º 3
0
 private static SyntacticElement ProcessRhsError(SyntacticElement rhs)
 {
     if (rhs == null)
     {
         return(new InvalidSyntacticElement("No rhs on predicate", "Nothing cannot imply something"));
     }
     else if (rhs is InvalidSyntacticElement)
     {
         return(new InvalidSyntacticElement("Invalid right hand side of proposition", ((InvalidSyntacticElement)rhs).Message));
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
Exemplo n.º 4
0
        // assumes a valid syntactic element
        public ProgramFile Add(string line)
        {
            SyntacticElement se = LineParser.Parse(line);

            if (se is InvalidSyntacticElement)
            {
                throw new ArgumentException("Unable to add invalid syntactic element! " + se.Text);
            }
            if (se is PredicateSyntacticElement)
            {
                PredicateSyntacticElement p = (PredicateSyntacticElement)se;
                if (!programData.Add(p))
                {
                    throw new ArgumentException("Predicate {0} already exists", p.Name);
                }
            }
            if (se is PropositionExpression)
            {
                PropositionExpression p = (PropositionExpression)se;
                programData.Add(p);
            }
            if (se is PredicateSetterSyntacticElement)
            {
                programData.Add((PredicateSetterSyntacticElement)se);
            }
            if (se is ThereforeSyntacticElement)
            {
                ThereforeSyntacticElement t = (ThereforeSyntacticElement)se;
                programData.Add(t);
            }
            if (!(se is NullSyntacticElement))
            {
                Listing.Add(line);
            }
            return(this);
        }
Exemplo n.º 5
0
        // returns a PredicateExpression : ExpressionSyntacticElement
        //    // a predicate expression is a recursive data structure
        //    // a predicate expression is a [predicate|predicate expression] operation [predicate|predicate expression]
        //    public PredicateExpression lhs;
        //    public Operation operation;
        //    public PredicateExpression rhs;
        //    public override bool? Value { get { return value; } set { this.value = value; } }
        //  expressions may be held in brackets, operations not
        // ad adicio: ( must be followed by a predicate and preceded by an operation
        //            ) must be followed by a operation and preceded by an predicate
        // bracket must also balance
        //   z and not( p and q ) or ( ( not ( a and b ) ) or ( c and d ) )
        public static SyntacticElement Parse(List <string> lineElements)
        {
            if (lineElements.Count() == 0)
            {
                return(new InvalidSyntacticElement("Invalid expression", "Expression has ended in an operation"));
            }
            if (lineElements.Count() == 1)
            {
                if (lineElements[0].IsaReservedWord())
                {
                    return(new InvalidSyntacticElement("Invalid expression", "Expression has ended in an operation"));
                }
                else
                {
                    return(new PredicateExpression(lineElements[0]));
                }
            }
            // find the index of the first reserved word
            // return the left hand side as a string and the right hand side as a string
            // we will need to be thinking about recursion here.
            int index = IndexOfFirstKeyWord(lineElements);

            if (index == -1)
            {
                return(new InvalidSyntacticElement("Invalid expression syntax", "No expression found"));
            }
            else if (index == -2)   // expression surrounded by
            {
                // strip the covering brackets
                return(Parse(lineElements.Skip(1).Take(lineElements.Count() - 2).ToList()));
            }
            else
            {
                ExpressionSyntacticElement ese = GetExpressionFrom(lineElements[index]);
                if (ese is NotExpression)
                {
                    if (index == 0)
                    {
                        SyntacticElement exp = Parse(lineElements.Skip(index + 1).ToList <string>());
                        if (exp is ExpressionSyntacticElement)
                        {
                            ((NotExpression)(ese)).expression = (ExpressionSyntacticElement)exp;
                        }
                        else if (exp is InvalidSyntacticElement)
                        {
                            return(exp);
                        }
                        else
                        {
                            return(new InvalidSyntacticElement("Invalid expression syntax", "Unknown expression found not"));
                        }
                        return(ese);
                    }
                    else // the not expression is not the first token unless first token is a bracket?
                    {
                        return(new InvalidSyntacticElement("Invalid expression syntax", "not must be the first token in the string unless it is ("));
                    }
                }
                else
                {
                    SyntacticElement lhs = Parse(lineElements.Take(index).ToList <string>());
                    SyntacticElement rhs = Parse(lineElements.Skip(index + 1).ToList <string>());
                    if ((lhs is ExpressionSyntacticElement) &&
                        (rhs is ExpressionSyntacticElement))
                    {
                        if (ese is AndExpression)
                        {   // 0 1 3 4 (idex)
                            ((AndExpression)(ese)).lhs = (ExpressionSyntacticElement)lhs;
                            ((AndExpression)(ese)).rhs = (ExpressionSyntacticElement)rhs;
                        }
                        else if (ese is OrExpression)
                        {
                            ((OrExpression)(ese)).lhs = (ExpressionSyntacticElement)lhs;
                            ((OrExpression)(ese)).rhs = (ExpressionSyntacticElement)rhs;
                        }
                        else if (ese is XorExpression)
                        {
                            ((XorExpression)(ese)).lhs = (ExpressionSyntacticElement)lhs;
                            ((XorExpression)(ese)).rhs = (ExpressionSyntacticElement)rhs;
                        }
                        else
                        {
                            return(new InvalidSyntacticElement("Invalid expression syntax", "Unknown expression found"));
                        }
                        return(ese);
                    }
                    else
                    {
                        return(new InvalidSyntacticElement("Invalid expression syntax", "Unknown expression found"));
                    }
                }
            }
        }