Re: [Pyparsing] fourFn.py explanation and slight modification
Brought to you by:
ptmcg
From: Glenn P. <gle...@gm...> - 2014-09-04 10:00:49
|
Hmm I got it going with this point = Literal( "." ) e = CaselessLiteral( "E" ) fnumber = Combine( Word( "+-"+nums, nums ) + Optional( point + Optional( Word( nums ) ) ) + Optional( e + Word( "+-"+nums, nums ) ) ) ident = Word(alphas, alphas+nums+"_$") plus = Literal( "+" ) minus = Literal( "-" ) mult = Literal( "*" ) div = Literal( "/" ) lte = Literal( ">=" ) lt = Literal( "<" ) gt = Literal( ">" ) anding = CaselessLiteral( "and" ) oring = CaselessLiteral( "or" ) lpar = Literal( "(" ).suppress() rpar = Literal( ")" ).suppress() addop = plus | minus | lte | lt | gt multop = mult | div logicalop = anding | oring expop = Literal( "^" ) pi = CaselessLiteral( "PI" ) expr = Forward() atom = ((Optional(oneOf("- +")) + (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 ) ) term2 = term + ZeroOrMore( ( addop + term ).setParseAction( self.pushFirst ) ) expr <<= term2 + ZeroOrMore( ( logicalop + term2 ).setParseAction( self.pushFirst ) ) Can't say I 100% understand the grammer though. Any short explanations would be great. On 4 September 2014 10:23, Glenn Pierce <gle...@gm...> wrote: > 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. > |