From c451b1ebb9e4a76ab760822ba985ed456796fa4e Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 9 Dec 2020 01:30:21 +0000 Subject: [PATCH] parser v 0.0.4-alpha --- .gitignore | 6 +-- .../tra_analysis/equation/Expression.py | 38 ++++++++++++++ .../tra_analysis/equation/__init__.py | 2 +- .../equation/{parser.py => parser/BNF.py} | 22 -------- .../equation/parser/RegexInplaceParser.py | 51 +++++++++++++++++++ .../tra_analysis/equation/parser/__init__.py | 32 ++++++++++++ .../tra_analysis/equation/{ => parser}/py2.py | 0 7 files changed, 124 insertions(+), 27 deletions(-) create mode 100644 analysis-master/tra_analysis/equation/Expression.py rename analysis-master/tra_analysis/equation/{parser.py => parser/BNF.py} (82%) create mode 100644 analysis-master/tra_analysis/equation/parser/RegexInplaceParser.py create mode 100644 analysis-master/tra_analysis/equation/parser/__init__.py rename analysis-master/tra_analysis/equation/{ => parser}/py2.py (100%) diff --git a/.gitignore b/.gitignore index be2b2712..1fd768eb 100644 --- a/.gitignore +++ b/.gitignore @@ -39,7 +39,5 @@ analysis-master/tra_analysis/.ipynb_checkpoints analysis-master/tra_analysis/metrics/__pycache__ analysis-master/dist data-analysis/config/ -analysis-master/tra_analysis/equation/__pycache__/Expression.cpython-38.pyc -analysis-master/tra_analysis/equation/__pycache__/__init__.cpython-38.pyc -analysis-master/tra_analysis/equation/__pycache__/parser.cpython-38.pyc -analysis-master/tra_analysis/equation/__pycache__/py2.cpython-38.pyc +analysis-master/tra_analysis/equation/__pycache__/* +analysis-master/tra_analysis/equation/parser/__pycache__/* diff --git a/analysis-master/tra_analysis/equation/Expression.py b/analysis-master/tra_analysis/equation/Expression.py new file mode 100644 index 00000000..df659557 --- /dev/null +++ b/analysis-master/tra_analysis/equation/Expression.py @@ -0,0 +1,38 @@ +# Titan Robotics Team 2022: Expression submodule +# Written by Arthur Lu +# Notes: +# this should be imported as a python module using 'from tra_analysis.Equation import Expression' +# adapted from https://github.com/pyparsing/pyparsing/blob/master/examples/fourFn.py +# setup: + +__version__ = "0.0.1-alpha" + +__changelog__ = """changelog: + 0.0.1-alpha: +""" + +__author__ = ( + "Arthur Lu ", +) + +import re +from .parser import BNF + +class Expression(): + + expression = None + protected = list(BNF().fn.keys()) + + def __init__(self, s): + if(self.validate(s)): + self.expression = s + else: + pass + + def validate(self, s): + + return true + + def substitute(self, var, value): + + pass \ No newline at end of file diff --git a/analysis-master/tra_analysis/equation/__init__.py b/analysis-master/tra_analysis/equation/__init__.py index 447703d3..5948e0da 100644 --- a/analysis-master/tra_analysis/equation/__init__.py +++ b/analysis-master/tra_analysis/equation/__init__.py @@ -1 +1 @@ -from .parser import * \ No newline at end of file +from .Expression import Expression \ No newline at end of file diff --git a/analysis-master/tra_analysis/equation/parser.py b/analysis-master/tra_analysis/equation/parser/BNF.py similarity index 82% rename from analysis-master/tra_analysis/equation/parser.py rename to analysis-master/tra_analysis/equation/parser/BNF.py index 14e627fc..97b2bfb4 100644 --- a/analysis-master/tra_analysis/equation/parser.py +++ b/analysis-master/tra_analysis/equation/parser/BNF.py @@ -1,26 +1,4 @@ -# Titan Robotics Team 2022: Expression submodule -# Written by Arthur Lu -# Notes: -# this should be imported as a python module using 'from tra_analysis import equation' -# adapted from https://github.com/pyparsing/pyparsing/blob/master/examples/fourFn.py -# setup: - from __future__ import division - -__version__ = "0.0.2-alpha" - -__changelog__ = """changelog: - 0.0.2-alpha: - - wrote BNF using pyparsing and uses a BNF metasyntax - - renamed this submodule parser - 0.0.1-alpha: - - took items from equation.ipynb and ported here -""" - -__author__ = ( - "Arthur Lu ", -) - from pyparsing import (Literal, CaselessLiteral, Word, Combine, Group, Optional, ZeroOrMore, Forward, nums, alphas, oneOf) from . import py2 import math diff --git a/analysis-master/tra_analysis/equation/parser/RegexInplaceParser.py b/analysis-master/tra_analysis/equation/parser/RegexInplaceParser.py new file mode 100644 index 00000000..d8886878 --- /dev/null +++ b/analysis-master/tra_analysis/equation/parser/RegexInplaceParser.py @@ -0,0 +1,51 @@ +import re +from decimal import Decimal +from functools import reduce + +class RegexInplaceParser(object): + + def __init__(self, string): + + self.string = string + + def add(self, string): + while(len(re.findall("[+]{1}[-]?", string)) != 0): + string = re.sub("[-]?\d+[.]?\d*[+]{1}[-]?\d+[.]?\d*", str("%f" % reduce((lambda x, y: x + y), [Decimal(i) for i in re.split("[+]{1}", re.search("[-]?\d+[.]?\d*[+]{1}[-]?\d+[.]?\d*", string).group())])), string, 1) + return string + + def sub(self, string): + while(len(re.findall("\d+[.]?\d*[-]{1,2}\d+[.]?\d*", string)) != 0): + g = re.search("\d+[.]?\d*[-]{1,2}\d+[.]?\d*", string).group() + if(re.search("[-]{1,2}", g).group() == "-"): + r = re.sub("[-]{1}", "+-", g, 1) + string = re.sub(g, r, string, 1) + elif(re.search("[-]{1,2}", g).group() == "--"): + r = re.sub("[-]{2}", "+", g, 1) + string = re.sub(g, r, string, 1) + else: + pass + return string + + def mul(self, string): + while(len(re.findall("[*]{1}[-]?", string)) != 0): + string = re.sub("[-]?\d+[.]?\d*[*]{1}[-]?\d+[.]?\d*", str("%f" % reduce((lambda x, y: x * y), [Decimal(i) for i in re.split("[*]{1}", re.search("[-]?\d+[.]?\d*[*]{1}[-]?\d+[.]?\d*", string).group())])), string, 1) + return string + + def div(self, string): + while(len(re.findall("[/]{1}[-]?", string)) != 0): + string = re.sub("[-]?\d+[.]?\d*[/]{1}[-]?\d+[.]?\d*", str("%f" % reduce((lambda x, y: x / y), [Decimal(i) for i in re.split("[/]{1}", re.search("[-]?\d+[.]?\d*[/]{1}[-]?\d+[.]?\d*", string).group())])), string, 1) + return string + + def exp(self, string): + while(len(re.findall("[\^]{1}[-]?", string)) != 0): + string = re.sub("[-]?\d+[.]?\d*[\^]{1}[-]?\d+[.]?\d*", str("%f" % reduce((lambda x, y: x ** y), [Decimal(i) for i in re.split("[\^]{1}", re.search("[-]?\d+[.]?\d*[\^]{1}[-]?\d+[.]?\d*", string).group())])), string, 1) + return string + + def evaluate(self): + string = self.string + string = self.exp(string) + string = self.div(string) + string = self.mul(string) + string = self.sub(string) + string = self.add(string) + return string \ No newline at end of file diff --git a/analysis-master/tra_analysis/equation/parser/__init__.py b/analysis-master/tra_analysis/equation/parser/__init__.py new file mode 100644 index 00000000..17b284ed --- /dev/null +++ b/analysis-master/tra_analysis/equation/parser/__init__.py @@ -0,0 +1,32 @@ +# Titan Robotics Team 2022: Expression submodule +# Written by Arthur Lu +# Notes: +# this should be imported as a python module using 'from tra_analysis.Equation import parser' +# adapted from https://github.com/pyparsing/pyparsing/blob/master/examples/fourFn.py +# setup: + +__version__ = "0.0.4-alpha" + +__changelog__ = """changelog: + 0.0.4-alpha: + - moved individual parsers to their own filespar + 0.0.3-alpha: + - readded old regex based parser as RegexInplaceParser + 0.0.2-alpha: + - wrote BNF using pyparsing and uses a BNF metasyntax + - renamed this submodule parser + 0.0.1-alpha: + - took items from equation.ipynb and ported here +""" + +__author__ = ( + "Arthur Lu ", +) + +__all__ = { + "BNF", + "RegexInplaceParser" +} + +from .BNF import BNF as BNF +from .RegexInplaceParser import RegexInplaceParser as RegexInplaceParser \ No newline at end of file diff --git a/analysis-master/tra_analysis/equation/py2.py b/analysis-master/tra_analysis/equation/parser/py2.py similarity index 100% rename from analysis-master/tra_analysis/equation/py2.py rename to analysis-master/tra_analysis/equation/parser/py2.py