OXIESEC PANEL
- Current Dir:
/
/
opt
/
gsutil
/
third_party
/
pyparsing
/
examples
Server IP: 2a02:4780:11:1594:0:ef5:22d7:a
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
12/11/2024 09:39:44 AM
rwxr-xr-x
📄
0README.html
10.75 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
AcManForm.dfm
38.22 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
LAparser.py
20.62 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
Setup.ini
1.41 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
SimpleCalc.py
3.58 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
SingleForm.dfm
42.7 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
TAP.py
7.42 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
__init__.py
0 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
adventureEngine.py
21.59 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
antlr_grammar.py
10.88 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
antlr_grammar_tests.py
2.89 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
apicheck.py
2.2 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
bf.py
4.15 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
bigquery_view_parser.py
62.51 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
booleansearchparser.py
15.26 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
btpyparse.py
4.16 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
builtin_parse_action_demo.py
817 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
cLibHeader.py
853 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
chemical_formulas.py
3.17 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
commasep.py
707 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
configParse.py
2 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
cpp_enum_parser.py
1.35 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
cuneiform_python.py
2.57 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
datetime_parse_actions.py
1.9 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
decaf_parser.py
6.87 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
delta_time.py
21.49 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
dfmparse.py
7.05 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
dhcpd_leases_parser.py
2.98 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
dictExample.py
1.73 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
dictExample2.py
2.1 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
directx_x_file_parser.py
5.54 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
ebnf.py
4.15 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
ebnftest.py
2.35 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
email_address_parser.py
1.18 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
eval_arith.py
6.15 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
excel_expr.py
2.2 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
fourFn.py
10.27 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
gen_ctypes.py
5.28 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
getNTPserversNew.py
1.11 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
greeting.py
518 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
greetingInGreek.py
425 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
greetingInKorean.py
495 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
groupUsingListAllMatches.py
509 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
hola_mundo.py
2.02 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
html_stripper.py
1.68 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
html_table_parser.py
2.16 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
httpServerLogParser.py
3.48 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
idlParse.py
7.53 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
include_preprocessor.py
2.63 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
indentedGrammarExample.py
1022 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
indented_block_example.py
712 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
inv_regex.py
8.08 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
javascript_grammar.g
18.1 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
jsonParser.py
4.7 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
left_recursion.py
1.41 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
lineno_example.py
1.49 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
listAllMatches.py
1.57 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
lua_parser.py
7.96 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
lucene_grammar.py
8.46 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
macro_expander.py
1.77 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
make_diagram.py
1.62 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
matchPreviousDemo.py
586 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
mongodb_query_expression.py
8.46 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
mozilla.ics
752 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
mozillaCalendarParser.py
2.71 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
nested.py
579 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
nested_markup.py
1.85 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
number_words.py
3.77 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
numerics.py
1.34 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
oc.py
4.34 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
one_to_ninety_nine.py
1.88 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
parseTabularData.py
1.68 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
parse_python_value.py
2.5 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
parse_results_sum_example.py
853 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
partial_gene_match.py
2.33 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
pgn.py
3.34 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
position.py
2.17 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
protobuf_parser.py
3.89 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
pythonGrammarParser.py
8.08 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
railroad_diagram_demo.py
1.55 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
range_check.py
2.84 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
readJson.py
120.73 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
removeLineBreaks.py
1.8 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
roman_numerals.py
3.57 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
rosettacode.py
7.43 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
scanExamples.py
2.36 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
searchParserAppDemo.py
962 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
searchparser.py
10.4 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
select_parser.py
8.32 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
sexpParser.py
5.23 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
shapes.py
1.69 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
simpleArith.py
2.42 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
simpleBool.py
3.19 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
simpleSQL.py
3.16 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
simpleWiki.py
1.08 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
snmp_api.h
28.69 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
sql2dot.py
3.07 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
stackish.py
2.71 KB
08/25/2024 02:43:42 PM
rw-r--r--
📁
statemachine
-
08/25/2024 02:43:42 PM
rwxr-xr-x
📄
tag_emitter.py
1.17 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
tag_metadata.py
805 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
test_bibparse.py
8.72 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
unicode_denormalizer.py
5.02 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
urlExtractor.py
1.03 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
urlExtractorNew.py
2.4 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
verilog_parse.py
31.15 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
withAttribute.py
912 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
wordsToNum.py
2.92 KB
08/25/2024 02:43:42 PM
rw-r--r--
Editing: fourFn.py
Close
# fourFn.py # # Demonstration of the pyparsing module, implementing a simple 4-function expression parser, # with support for scientific notation, and symbols for e and pi. # Extended to add exponentiation and simple built-in functions. # Extended test cases, simplified pushFirst method. # Removed unnecessary expr.suppress() call (thanks Nathaniel Peterson!), and added Group # Changed fnumber to use a Regex, which is now the preferred method # Reformatted to latest pyparsing features, support multiple and variable args to functions # # Copyright 2003-2019 by Paul McGuire # from pyparsing import ( Literal, Word, Group, Forward, alphas, alphanums, Regex, ParseException, CaselessKeyword, Suppress, delimitedList, ) import math import operator exprStack = [] def push_first(toks): exprStack.append(toks[0]) def push_unary_minus(toks): for t in toks: if t == "-": exprStack.append("unary -") else: break bnf = None def BNF(): """ expop :: '^' multop :: '*' | '/' addop :: '+' | '-' integer :: ['+' | '-'] '0'..'9'+ atom :: PI | E | real | fn '(' expr ')' | '(' expr ')' factor :: atom [ expop factor ]* term :: factor [ multop factor ]* expr :: term [ addop term ]* """ global bnf if not bnf: # use CaselessKeyword for e and pi, to avoid accidentally matching # functions that start with 'e' or 'pi' (such as 'exp'); Keyword # and CaselessKeyword only match whole words e = CaselessKeyword("E") pi = CaselessKeyword("PI") # fnumber = Combine(Word("+-"+nums, nums) + # Optional("." + Optional(Word(nums))) + # Optional(e + Word("+-"+nums, nums))) # or use provided pyparsing_common.number, but convert back to str: # fnumber = ppc.number().addParseAction(lambda t: str(t[0])) fnumber = Regex(r"[+-]?\d+(?:\.\d*)?(?:[eE][+-]?\d+)?") ident = Word(alphas, alphanums + "_$") plus, minus, mult, div = map(Literal, "+-*/") lpar, rpar = map(Suppress, "()") addop = plus | minus multop = mult | div expop = Literal("^") expr = Forward() expr_list = delimitedList(Group(expr)) # add parse action that replaces the function identifier with a (name, number of args) tuple def insert_fn_argcount_tuple(t): fn = t.pop(0) num_args = len(t[0]) t.insert(0, (fn, num_args)) fn_call = (ident + lpar - Group(expr_list) + rpar).setParseAction( insert_fn_argcount_tuple ) atom = ( addop[...] + ( (fn_call | pi | e | fnumber | ident).setParseAction(push_first) | Group(lpar + expr + rpar) ) ).setParseAction(push_unary_minus) # 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 + (expop + factor).setParseAction(push_first)[...] term = factor + (multop + factor).setParseAction(push_first)[...] expr <<= term + (addop + term).setParseAction(push_first)[...] bnf = expr return bnf # map operator symbols to corresponding arithmetic operations epsilon = 1e-12 opn = { "+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv, "^": operator.pow, } fn = { "sin": math.sin, "cos": math.cos, "tan": math.tan, "exp": math.exp, "abs": abs, "trunc": int, "round": round, "sgn": lambda a: -1 if a < -epsilon else 1 if a > epsilon else 0, # functionsl with multiple arguments "multiply": lambda a, b: a * b, "hypot": math.hypot, # functions with a variable number of arguments "all": lambda *a: all(a), } def evaluate_stack(s): op, num_args = s.pop(), 0 if isinstance(op, tuple): op, num_args = op if op == "unary -": return -evaluate_stack(s) if op in "+-*/^": # note: operands are pushed onto the stack in reverse order op2 = evaluate_stack(s) op1 = evaluate_stack(s) return opn[op](op1, op2) elif op == "PI": return math.pi # 3.1415926535 elif op == "E": return math.e # 2.718281828 elif op in fn: # note: args are pushed onto the stack in reverse order args = reversed([evaluate_stack(s) for _ in range(num_args)]) return fn[op](*args) elif op[0].isalpha(): raise Exception("invalid identifier '%s'" % op) else: # try to evaluate as int first, then as float if int fails try: return int(op) except ValueError: return float(op) if __name__ == "__main__": def test(s, expected): exprStack[:] = [] try: results = BNF().parseString(s, parseAll=True) val = evaluate_stack(exprStack[:]) except ParseException as pe: print(s, "failed parse:", str(pe)) except Exception as e: print(s, "failed eval:", str(e), exprStack) else: if val == expected: print(s, "=", val, results, "=>", exprStack) else: print(s + "!!!", val, "!=", expected, results, "=>", exprStack) test("9", 9) test("-9", -9) test("--9", 9) test("-E", -math.e) test("9 + 3 + 6", 9 + 3 + 6) test("9 + 3 / 11", 9 + 3.0 / 11) test("(9 + 3)", (9 + 3)) test("(9+3) / 11", (9 + 3.0) / 11) test("9 - 12 - 6", 9 - 12 - 6) test("9 - (12 - 6)", 9 - (12 - 6)) test("2*3.14159", 2 * 3.14159) test("3.1415926535*3.1415926535 / 10", 3.1415926535 * 3.1415926535 / 10) test("PI * PI / 10", math.pi * math.pi / 10) test("PI*PI/10", math.pi * math.pi / 10) test("PI^2", math.pi ** 2) test("round(PI^2)", round(math.pi ** 2)) test("6.02E23 * 8.048", 6.02e23 * 8.048) test("e / 3", math.e / 3) test("sin(PI/2)", math.sin(math.pi / 2)) test("10+sin(PI/4)^2", 10 + math.sin(math.pi / 4) ** 2) test("trunc(E)", int(math.e)) test("trunc(-E)", int(-math.e)) test("round(E)", round(math.e)) test("round(-E)", round(-math.e)) test("E^PI", math.e ** math.pi) test("exp(0)", 1) test("exp(1)", math.e) test("2^3^2", 2 ** 3 ** 2) test("(2^3)^2", (2 ** 3) ** 2) test("2^3+2", 2 ** 3 + 2) test("2^3+5", 2 ** 3 + 5) test("2^9", 2 ** 9) test("sgn(-2)", -1) test("sgn(0)", 0) test("sgn(0.1)", 1) test("foo(0.1)", None) test("round(E, 3)", round(math.e, 3)) test("round(PI^2, 3)", round(math.pi ** 2, 3)) test("sgn(cos(PI/4))", 1) test("sgn(cos(PI/2))", 0) test("sgn(cos(PI*3/4))", -1) test("+(sgn(cos(PI/4)))", 1) test("-(sgn(cos(PI/4)))", -1) test("hypot(3, 4)", 5) test("multiply(3, 7)", 21) test("all(1,1,1)", True) test("all(1,1,1,1,1,0)", False) """ Test output: >python fourFn.py 9 = 9 ['9'] => ['9'] -9 = -9 ['-', '9'] => ['9', 'unary -'] --9 = 9 ['-', '-', '9'] => ['9', 'unary -', 'unary -'] -E = -2.718281828459045 ['-', 'E'] => ['E', 'unary -'] 9 + 3 + 6 = 18 ['9', '+', '3', '+', '6'] => ['9', '3', '+', '6', '+'] 9 + 3 / 11 = 9.272727272727273 ['9', '+', '3', '/', '11'] => ['9', '3', '11', '/', '+'] (9 + 3) = 12 [['9', '+', '3']] => ['9', '3', '+'] (9+3) / 11 = 1.0909090909090908 [['9', '+', '3'], '/', '11'] => ['9', '3', '+', '11', '/'] 9 - 12 - 6 = -9 ['9', '-', '12', '-', '6'] => ['9', '12', '-', '6', '-'] 9 - (12 - 6) = 3 ['9', '-', ['12', '-', '6']] => ['9', '12', '6', '-', '-'] 2*3.14159 = 6.28318 ['2', '*', '3.14159'] => ['2', '3.14159', '*'] 3.1415926535*3.1415926535 / 10 = 0.9869604400525172 ['3.1415926535', '*', '3.1415926535', '/', '10'] => ['3.1415926535', '3.1415926535', '*', '10', '/'] PI * PI / 10 = 0.9869604401089358 ['PI', '*', 'PI', '/', '10'] => ['PI', 'PI', '*', '10', '/'] PI*PI/10 = 0.9869604401089358 ['PI', '*', 'PI', '/', '10'] => ['PI', 'PI', '*', '10', '/'] PI^2 = 9.869604401089358 ['PI', '^', '2'] => ['PI', '2', '^'] round(PI^2) = 10 [('round', 1), [['PI', '^', '2']]] => ['PI', '2', '^', ('round', 1)] 6.02E23 * 8.048 = 4.844896e+24 ['6.02E23', '*', '8.048'] => ['6.02E23', '8.048', '*'] e / 3 = 0.9060939428196817 ['E', '/', '3'] => ['E', '3', '/'] sin(PI/2) = 1.0 [('sin', 1), [['PI', '/', '2']]] => ['PI', '2', '/', ('sin', 1)] 10+sin(PI/4)^2 = 10.5 ['10', '+', ('sin', 1), [['PI', '/', '4']], '^', '2'] => ['10', 'PI', '4', '/', ('sin', 1), '2', '^', '+'] trunc(E) = 2 [('trunc', 1), [['E']]] => ['E', ('trunc', 1)] trunc(-E) = -2 [('trunc', 1), [['-', 'E']]] => ['E', 'unary -', ('trunc', 1)] round(E) = 3 [('round', 1), [['E']]] => ['E', ('round', 1)] round(-E) = -3 [('round', 1), [['-', 'E']]] => ['E', 'unary -', ('round', 1)] E^PI = 23.140692632779263 ['E', '^', 'PI'] => ['E', 'PI', '^'] exp(0) = 1.0 [('exp', 1), [['0']]] => ['0', ('exp', 1)] exp(1) = 2.718281828459045 [('exp', 1), [['1']]] => ['1', ('exp', 1)] 2^3^2 = 512 ['2', '^', '3', '^', '2'] => ['2', '3', '2', '^', '^'] (2^3)^2 = 64 [['2', '^', '3'], '^', '2'] => ['2', '3', '^', '2', '^'] 2^3+2 = 10 ['2', '^', '3', '+', '2'] => ['2', '3', '^', '2', '+'] 2^3+5 = 13 ['2', '^', '3', '+', '5'] => ['2', '3', '^', '5', '+'] 2^9 = 512 ['2', '^', '9'] => ['2', '9', '^'] sgn(-2) = -1 [('sgn', 1), [['-', '2']]] => ['2', 'unary -', ('sgn', 1)] sgn(0) = 0 [('sgn', 1), [['0']]] => ['0', ('sgn', 1)] sgn(0.1) = 1 [('sgn', 1), [['0.1']]] => ['0.1', ('sgn', 1)] foo(0.1) failed eval: invalid identifier 'foo' ['0.1', ('foo', 1)] round(E, 3) = 2.718 [('round', 2), [['E'], ['3']]] => ['E', '3', ('round', 2)] round(PI^2, 3) = 9.87 [('round', 2), [['PI', '^', '2'], ['3']]] => ['PI', '2', '^', '3', ('round', 2)] sgn(cos(PI/4)) = 1 [('sgn', 1), [[('cos', 1), [['PI', '/', '4']]]]] => ['PI', '4', '/', ('cos', 1), ('sgn', 1)] sgn(cos(PI/2)) = 0 [('sgn', 1), [[('cos', 1), [['PI', '/', '2']]]]] => ['PI', '2', '/', ('cos', 1), ('sgn', 1)] sgn(cos(PI*3/4)) = -1 [('sgn', 1), [[('cos', 1), [['PI', '*', '3', '/', '4']]]]] => ['PI', '3', '*', '4', '/', ('cos', 1), ('sgn', 1)] +(sgn(cos(PI/4))) = 1 ['+', [('sgn', 1), [[('cos', 1), [['PI', '/', '4']]]]]] => ['PI', '4', '/', ('cos', 1), ('sgn', 1)] -(sgn(cos(PI/4))) = -1 ['-', [('sgn', 1), [[('cos', 1), [['PI', '/', '4']]]]]] => ['PI', '4', '/', ('cos', 1), ('sgn', 1), 'unary -'] """