pyParsing计算表达式

问题描述

浏览了本网站的pyparsing和帖子的几个示例之后,我设法编写了将完全按照我想要的方式解析表达式的代码。但是,现在我一直沉迷于如何评估它,因此在这里寻求您的帮助。

所以当我给这个字符串时:“((Number(3)> Number(5))AND(Time(IST) [[[[['Number',[3]],'>',['Number',[5]]],'AND',[['Time',['IST']],'

Number是一个自定义函数,它接受int输入并返回int。 时间是一个自定义函数,它接受字符串输入并以int形式返回当前系统时间。 这样,我将拥有许多自定义函数,这些函数将接受一些输入并返回值。

所以有人可以帮助我评估已分析的结果,所以最终我应该得到True或False作为最终结果吗?

这是我的python脚本的副本:

from pyparsing import (
    CaselessKeyword,Suppress,Word,alphas,alphanums,nums,Optional,Group,oneOf,Forward,infixNotation,opAssoc,dblQuotedString,delimitedList,Combine,Literal,QuotedString,ParserElement,Keyword,OneOrMore,pyparsing_common as ppc,)

ParserElement.enablePackrat()

LPAR,RPAR = map(Suppress,"()")

expr = Forward()

alps = Word(alphas,alphanums) 

def stat_function(name):
    return ( 
        Group(CaselessKeyword(name) + Group(LPAR + delimitedList(expr) + RPAR)) |
        Group(CaselessKeyword(name) + Group(LPAR + delimitedList(alps) + RPAR))
    )

timeFunc = stat_function("Time")
dateFunc = stat_function("Date")
numFunc  = stat_function("Number")
funcCall = timeFunc | dateFunc | numFunc

Compop = oneOf("< = > >= <= != <>")
 
multOp = oneOf("* /")
addOp = oneOf("+ -")
logicalOp = oneOf("and or AND OR")
numericLiteral = ppc.number

operand = numericLiteral | funcCall | alps  
arithExpr = infixNotation(
    operand,[(multOp,2,opAssoc.LEFT),(addOp,(Compop,(logicalOp,]  
)

expr <<= arithExpr  

s1 = "(Number(3) > Number(5)) AND (Time(IST) < Number(1030))"
result = expr.parseString(s1)
print(result)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)