def andand (a, b):
if a!=0 and b!=0:
return 1
else:
return 0
def oror(a, b):
if a!=0 or b!=0:
return 1
else:
return 0
def BNF():
"""
expop :: '^'
multop :: '*' | '/'
addop :: '+' | '-'
integer :: ['+' | '-'] '0'..'9'+
atom :: PI | E | real | fn '(' expr ')' | '(' expr ')'
factor :: atom [ expop factor ]*
term :: factor [ multop factor ]*
expr :: term [ addop term ]*
equation:: expr [equality expr]*
logic :: equation [logicalop equation]*
"""
global bnf
if not bnf:
point = Literal( "." )
e = CaselessLiteral( "E" )
fnumber = Combine( Word( "+-"+nums, nums ) +
Optional( point + Optional( Word( nums ) ) ) +
Optional( e + Word( "+-"+nums, nums ) ) )
#columnName = Word(alphanums)
ident = Word(alphas+ChineseCharacters.unicodeList, ChineseCharacters.unicodeList+alphas+nums+"_$[]")
plus = Literal( "+" )
minus = Literal( "-" )
mult = Literal( "*" )
div = Literal( "/" )
andand = Literal("&&")
oror = Literal("||")
is_a = Literal("==")
less_than = Literal("<")
bigger_than = Literal(">")
is_not_a = Literal("!=")
lpar = Literal( "(" ).suppress()
rpar = Literal( ")" ).suppress()
addop = plus | minus
multop = mult | div
compop = is_a | is_not_a | less_than | bigger_than
logical = andand | oror
expop = Literal( "^" )
pi = CaselessLiteral( "PI" )
logic = Forward()
atom = (Optional("-") + ( ident | e | fnumber | pi + lpar + logic + rpar).setParseAction( pushFirst ) | ( lpar + logic.suppress() + rpar )).setParseAction(pushUMinus)
# by defining exponentiation as "atom [ ^ factor ]..." instead of "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-righ
# that is, 2^3^2 = 2^(3^2), not (2^3)^2.
factor = Forward()
factor << atom + ZeroOrMore( ( expop + factor ).setParseAction( pushFirst ) )
term = factor + ZeroOrMore( ( multop + factor ).setParseAction( pushFirst ) )
expr = term + ZeroOrMore( ( addop + term ).setParseAction( pushFirst ) )
equation = expr + ZeroOrMore( ( compop + expr ).setParseAction(pushFirst) )
logic << equation + ZeroOrMore( ( logical + equation ).setParseAction(pushFirst) )
bnf = logic
return bnf
elif op in logicalopnList:
op2 = evaluateStack( s )
op1 = evaluateStack( s )
return logicalopn[op]( op1, op2 )