def addTextFunction(self, expression): temp = expression.replace("(", " ") temp = temp.replace(")", " ") temp = cleaning.stripWhiteSpace(temp) temp = temp.replace("`", "") args = temp.split(",") if args[0].lower() == "typedef": return self.addTextSort(expression) elif len(args) == 2: return self.addTextAtomic(expression) returnType = "" funcName = "" funcArgs = [] #Find the return type if args[0] in self.sorts.keys(): returnType = args[0] args.remove(args[0]) #Find the function name for arg in args: if not arg in self.sorts.keys(): funcName = arg args.remove(arg) break #Find the function args for arg in args: if arg in self.sorts.keys(): funcArgs.append(arg) #Error Checking if returnType == "" or funcName == "" or funcArgs == []: print "ERROR: The function prototype was not formatted correctly." return False #Add the function return self.addCodeFunction(funcName, returnType, funcArgs)
def addTextFunction(self,expression): temp = expression.replace("("," ") temp = temp.replace(")"," ") temp = cleaning.stripWhiteSpace(temp) temp = temp.replace("`","") args = temp.split(",") if args[0].lower() == "typedef": return self.addTextSort(expression) elif len(args) == 2: return self.addTextAtomic(expression) returnType = "" funcName = "" funcArgs = [] #Find the return type if args[0] in self.sorts.keys(): returnType = args[0] args.remove(args[0]) #Find the function name for arg in args: if not arg in self.sorts.keys(): funcName = arg args.remove(arg) break #Find the function args for arg in args: if arg in self.sorts.keys(): funcArgs.append(arg) #Error Checking if returnType == "" or funcName == "" or funcArgs == []: print "ERROR: The function prototype was not formatted correctly." return False #Add the function return self.addCodeFunction(funcName,returnType,funcArgs)
def tokenizeRandomDCEC(expression, namespace=""): ''' This function creates a token representation of a random DCEC statement. It returns the token as well as sorts of new atomics and functions. ''' #Default DCEC Functions if namespace == "": namespace = prototypes.NAMESPACE() namespace.addBasicDCEC() else: namespace = namespace #Remove Comments temp = removeComments(expression) #Check for an empty string if temp == "()": return ("", {}, {}, {}) #Check for a parentheses mismatch error if not cleaning.checkParens(expression): print "ERROR: parentheses mismatch error." return (False, False, False, False) #Make symbols into functions temp = functorizeSymbols(temp) #Strip comments temp = cleaning.stripComments(temp) #Strip whitespace so you can do the rest of the parsing temp = cleaning.stripWhiteSpace(temp) #Tuck the functions inside thier parentheses temp = cleaning.tuckFunctions(temp) #Strip whitespace again temp = cleaning.stripWhiteSpace(temp) #Consolidate Parentheses temp = cleaning.consolidateParens(temp) quantifiers = [] #These are the tokens that should be added to the namespace addAtomics = {} addFunctions = {} addQuants = {} returnToken = TokenTree(temp, namespace, quantifiers, addQuants, addAtomics, addFunctions) #check for errors that occur in the lower level if isinstance(returnToken, bool) and returnToken == False: return (False, False, False, False) #Add quantifiers to the TokenTree returnToken = tokenizeQuantifiers(returnToken, quantifiers) return (returnToken, addQuants, addAtomics, addFunctions)
def tokenizeRandomDCEC(expression,namespace = ""): ''' This function creates a token representation of a random DCEC statement. It returns the token as well as sorts of new atomics and functions. ''' #Default DCEC Functions if namespace == "": namespace = prototypes.NAMESPACE() namespace.addBasicDCEC() else: namespace = namespace #Remove Comments temp = removeComments(expression) #Check for an empty string if temp == "()": return ("",{},{},{}) #Check for a parentheses mismatch error if not cleaning.checkParens(expression): print "ERROR: parentheses mismatch error." return (False,False,False,False) #Make symbols into functions temp = functorizeSymbols(temp) #Strip comments temp = cleaning.stripComments(temp) #Strip whitespace so you can do the rest of the parsing temp = cleaning.stripWhiteSpace(temp) #Tuck the functions inside thier parentheses temp = cleaning.tuckFunctions(temp) #Strip whitespace again temp = cleaning.stripWhiteSpace(temp) #Consolidate Parentheses temp = cleaning.consolidateParens(temp) quantifiers = [] #These are the tokens that should be added to the namespace addAtomics = {} addFunctions = {} addQuants = {} returnToken = TokenTree(temp,namespace,quantifiers,addQuants,addAtomics,addFunctions) #check for errors that occur in the lower level if isinstance(returnToken,bool) and returnToken == False: return (False,False,False,False) #Add quantifiers to the TokenTree returnToken = tokenizeQuantifiers(returnToken,quantifiers) return (returnToken,addQuants,addAtomics,addFunctions)
def addTextSort(self, expression): temp = expression.replace("(", " ") temp = temp.replace(")", " ") temp = cleaning.stripWhiteSpace(temp) temp = temp.replace("`", "") args = temp.split(",") if len(args) == 2: self.addCodeSort(args[1]) elif len(args) > 2: self.addCodeSort(args[1], args[2:]) else: print "ERROR: Cannot define the sort" return False
def addTextSort(self,expression): temp = expression.replace("("," ") temp = temp.replace(")"," ") temp = cleaning.stripWhiteSpace(temp) temp = temp.replace("`","") args = temp.split(",") if len(args) == 2: self.addCodeSort(args[1]) elif len(args) > 2: self.addCodeSort(args[1],args[2:]) else: print "ERROR: Cannot define the sort" return False
def addTextAtomic(self, expression): temp = expression.replace("(", " ") temp = temp.replace(")", " ") temp = cleaning.stripWhiteSpace(temp) temp = temp.replace("`", "") args = temp.split(",") returnType = "" funcName = "" #Find the return type for arg in args: if arg in self.sorts.keys(): returnType = arg args.remove(arg) break #Find the function name for arg in args: if not arg in self.sorts.keys(): funcName = arg args.remove(arg) break return self.addCodeAtomic(funcName, returnType)
def addTextAtomic(self,expression): temp = expression.replace("("," ") temp = temp.replace(")"," ") temp = cleaning.stripWhiteSpace(temp) temp = temp.replace("`","") args = temp.split(",") returnType = "" funcName = "" #Find the return type for arg in args: if arg in self.sorts.keys(): returnType = arg args.remove(arg) break #Find the function name for arg in args: if not arg in self.sorts.keys(): funcName = arg args.remove(arg) break return self.addCodeAtomic(funcName,returnType)
if __name__ == "__main__": ''' Testing suite. ''' inputin = raw_input("Enter an expression: ") #Make symbols into functions temp = removeComments(inputin) print temp temp = functorizeSymbols(temp) print temp #Strip comments temp = cleaning.stripComments(temp) print temp #Strip whitespace so you can do the rest of the parsing temp = cleaning.stripWhiteSpace(temp) print temp #Tuck the functions inside thier parentheses temp = cleaning.tuckFunctions(temp) print temp #Consolidate Parentheses temp = cleaning.consolidateParens(temp) print temp testNAMESPACE = prototypes.NAMESPACE() testNAMESPACE.addBasicDCEC() testNAMESPACE.addBasicNumerics() testNAMESPACE.addBasicLogic() testNAMESPACE.addTextFunction("ActionType heal Agent") #testNAMESPACE.addTextFunction("Boolean B Agent Moment Boolean Certainty") addQuants = {} addAtomics = {}