From 5d04064f6d571672253f94abb55136481678e927 Mon Sep 17 00:00:00 2001 From: Zander Thannhauser Date: Thu, 5 Jun 2025 20:18:09 -0500 Subject: [PATCH] even when switched over to infix-notation, parenthses still are too slow --- main.py | 48 +++++++----------------------------------------- 1 file changed, 7 insertions(+), 41 deletions(-) diff --git a/main.py b/main.py index f6ca5ba..befc9aa 100755 --- a/main.py +++ b/main.py @@ -362,53 +362,19 @@ def get_simplifications(args, available_operators): return cache[available_operators]; def create_parser(): - import pyparsing as pp + from pyparsing import infix_notation, opAssoc, Word, oneOf - root = pp.infix_notation(pp.Word("01wxyz"), [ + # this can't handle parenthesis very well... + # do I really have to write my own parser? + root = infix_notation(Word("01wxyz"), [ ('!', 1, opAssoc.RIGHT), + (oneOf('< <= > >='), 2, opAssoc.LEFT), + (oneOf('== !='), 2, opAssoc.LEFT), (oneOf('&& !& &!'), 2, opAssoc.LEFT), - (oneOf('|| !| |!'), 2, opAssoc.LEFT)]) - - # from pyparsing import Forward, Suppress, Keyword, Group, ZeroOrMore, Optional, Literal - - # root = Forward() - - # literal = pp.Word('01'); - - # variable = pp.Word('wxyz') - - # highest = literal | variable | (Suppress('(') + root + Suppress(')')); - - # prefix_expression = Group(Literal("!") + highest) | highest - - # relational_expression = \ - # Group(prefix_expression + Literal('<=') + prefix_expression) \ - # | Group(prefix_expression + Literal('>=') + prefix_expression) \ - # | Group(prefix_expression + Literal('>') + prefix_expression) \ - # | Group(prefix_expression + Literal('<') + prefix_expression) \ - # | Group(prefix_expression + Literal('==') + prefix_expression) \ - # | Group(prefix_expression + Literal('!=') + prefix_expression) \ - # | prefix_expression; - - # logical_and_expression = Forward(); - # logical_and_expression <<= \ - # Group(relational_expression + Literal('&&') + logical_and_expression) \ - # | Group(relational_expression + Literal('!&') + logical_and_expression) \ - # | Group(relational_expression + Literal('&!') + logical_and_expression) \ - # | relational_expression; - - # logical_or_expression = Forward() - # logical_or_expression <<= \ - # Group(logical_and_expression + Literal('||') + logical_or_expression) \ - # | Group(logical_and_expression + Literal('!|') + logical_or_expression) \ - # | Group(logical_and_expression + Literal('|!') + logical_or_expression) \ - # | logical_and_expression; - - # root <<= logical_or_expression; + (oneOf('|| !| |!'), 2, opAssoc.LEFT)]); return root; - def parse(parser, text): return parser.parseString(text, parseAll = True).asList()[0]