[Pyparsing] fourFn.py explanation and slight modification
Brought to you by:
ptmcg
From: Glenn P. <gle...@gm...> - 2014-09-04 09:23:39
|
Hi I am new to pyparsing and I am having trouble understanding the grammer of the example fourFn.py <http://pyparsing.wikispaces.com/file/view/fourFn.py/30154950/fourFn.py> I am trying to add comparison operators and logical operators to this example. The comparisons work ok but I not sure how to add the logical operators AND OR NOT So far I have this plus = Literal( "+" ) minus = Literal( "-" ) mult = Literal( "*" ) div = Literal( "/" ) lte = Literal( ">=" ) lt = Literal( "<" ) gt = Literal( ">" ) anding = CaselessLiteral( "and" ) lpar = Literal( "(" ).suppress() rpar = Literal( ")" ).suppress() addop = plus | minus | lte | lt | gt multop = mult | div expop = Literal( "^" ) pi = CaselessLiteral( "PI" ) sensor_values = CaselessLiteral( "sensor_values" ) expr = Forward() atom = ((Optional(oneOf("- +")) + (sensor_values|pi|e|fnumber|ident+lpar+expr+rpar).setParseAction(self.pushFirst)) | Optional(oneOf("- +")) + Group(lpar+expr+rpar) ).setParseAction(self.pushUMinus) # by defining exponentiation as "atom [ ^ factor ]..." instead of # "atom [ ^ atom ]...", we get right-to-left exponents, instead of left-to-right # that is, 2^3^2 = 2^(3^2), not (2^3)^2. factor = Forward() factor <<= atom + ZeroOrMore( ( expop + factor ).setParseAction( self.pushFirst ) ) term = factor + ZeroOrMore( ( multop + factor ).setParseAction( self.pushFirst ) ) expr <<= term + ZeroOrMore( ( addop + term ).setParseAction( self.pushFirst ) ) Could someone perhaps explain the three lines below and how I would add my CaselessLiteral( "and" ) boolean so I could do something like nsp.eval('10 >= 5 AND 10 < 15') factor <<= atom + ZeroOrMore( ( expop + factor ).setParseAction( self.pushFirst ) ) term = factor + ZeroOrMore( ( multop + factor ).setParseAction( self.pushFirst ) ) expr <<= term + ZeroOrMore( ( addop + term ).setParseAction( self.pushFirst ) ) PS what is the <<= operator ? I can't find the docs for it Thanks for any help. |