undercurrent
undercurrent

Reputation: 395

parsing class instance methods with custom grammar in python

So I have a problem I'm trying to solve in which I have two fundamental classes CMIX and C, whereby CMIX contains a list of instances of C: [C1, C2, .... Cn] which will be manipulated by a custom grammar (string values operators - "+", "-", "", "(", ")", but in the sense that there is the ability to embed extra logic into operations. For example, a [C1 + C2 + ... + Cn] composition would call C.foo() for each C1,C2,...Cn, and then add those results. Similarly, a composition of grammar terms with the object set, e.g.: [C1 "" "(" C1 "+" C2 "*" "("C2"+"C2")"")"] would call C1.foo(), C2.foo(), and then combine those results according to normal mathematical evaluation. Initially, I just want to deal with basic math operations like * + ( ) but would like to be able to extend it later for custom (non math) terms .

A basic representative sketch of the class functionality is as follows (this is mainly conceptual; especially the CMIX, as I'm unsure of the best way to go about the parsing syntax logic)

class CMIX():
   self.C_list = []  # List of instances of type C
   self.grammar = []
   def my_val(self, val):
      ## call every .foo(val=val) in self.C_list, and apply the grammar rules accordingly.

class C():
   def __init__(self, location):
      self.location= location
   def my_val(self, val):
      return val * 2 
   def check_location(self):
      if self.location == "antarctica":
         return "cold"
      elif self.location == "moon":
         return "bouncy"
      else:
         return "lost"

for example, say we have defined the global grammar operator

+ means if C.check_location() == "lost" then return -100*C.my_val(val) otherwise just return C.m

the grammar said C1 + C2, and C1.location="lost", C2.location="antarctica", then CMIX.my_val(20) would evaluate like: -100*C1.my_val(20) + C2.my_val(20)

Is there an existing easy way to do this in python? Or perhaps a lightweight library that allows one to create such evaluation rules. I have found for instance https://github.com/pydata/numexpr however this applies directly onto datatypes, whereas it should apply onto the object layer, apply custom logic, and then execute the standard math grammar order of operations.

Any direction for trying to do this is greatly appreciated! regards

Upvotes: 0

Views: 614

Answers (1)

PaulMcG
PaulMcG

Reputation: 63749

Pyparsing makes it pretty easy to define infix notation parsers, and can wrap operators and operands in classes to do standard or customized behavior. See this question and its pyparsing-based solution: pyparsing nestedExpr and nested parentheses

Upvotes: 1

Related Questions