Implementing a logical condition in pyparsing

I am working on implementing parsing and state assessment using pyparsing. My goal is to successfully parse an expression like:

x <10 and y> 5 and z <3

The code I came up with looks something like this:

var = Word(alphas + '._', alphanums + '._') text = Suppress("'") + Word(alphas, alphanums) + Suppress("'") integer = Word(nums).setParseAction(lambda t: int(t[0])) operator = oneOf(">= <= != > < ==") comperison = (var + operator + (integer | text)).setParseAction(lambda t: self.operands_map[t[1]](t[0], t[2])) expr = operatorPrecedence(binary_op,[ ("NOT", 1, opAssoc.RIGHT, lambda t: do_not(t)), ("OR", 2, opAssoc.LEFT, do_or(t)), ("AND", 2, opAssoc.LEFT, lambda t: do_and(t))]) 

When I have no logical conditions or only one logical, it works fine, but when I have more than one, as in the example I gave above, it seems to fail and does not even perform logical operations.

Can anyone here give some ideas or recommendations? Any help would be appreciated.

+4
source share

Source: https://habr.com/ru/post/1500815/


All Articles