Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8322181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:17:40+00:00 2026-06-08T23:17:40+00:00

I have a list of Boolean expressions that represent physical objects that can be

  • 0

I have a list of Boolean expressions that represent physical objects that can be combined to represent larger objects. They look something like this: ((A and B) or C). This object can be represented by a combination of A and B or by C alone. I would like to generate a list of lists of strings that can be used to create the object. In this case I want [[A,B], [C]].

Pyparsing looks pretty intriguing so I’ve decided to give it a shot for this problem. After a few failed attempts I’ve settled on adapting the fourFn.py example from the website. This is what I have so far:

from pyparsing import Literal, CaselessLiteral, Word, Combine, \
    Group, Optional, ZeroOrMore, Forward, alphanums

exprStack = []

def myAnd(op1, op2):
    if isinstance(op1, str):
        return([op1, op2])
    else:
    return op1.append(op2)

def myOr(op1, op2):
    if isinstance(op1, str):
        return([[op1], [op2]])
    else:
        return op1.append([op2])

def pushFirst(strg, loc, toks):
    exprStack.append(toks[0])

bnf = None
def BNF():
    """
    boolop  :: 'and' | 'or'
    gene    :: alphanum
    atom    :: gene | '(' expr ')'
    """
    global bnf
    if not bnf:
        element = Word(alphanums)
        andop  = Literal( "and" )
        orop = Literal( "or" )
        lpar  = Literal( "(" ).suppress()
        rpar  = Literal( ")" ).suppress()
        boolop  = andop | orop

        expr = Forward()
        atom = ((element | lpar + expr + rpar).setParseAction(pushFirst) | (lpar + expr.suppress() + rpar))
        expr << atom + ZeroOrMore((boolop + expr).setParseAction(pushFirst))

        bnf = expr
    return bnf

# map operator symbols to corresponding arithmetic operations
fn  = {"or": myOr,
       "and": myAnd}

def evaluateStack( s ):
    op = s.pop()
    if op in fn:
        op2 = evaluateStack(s)
        op1 = evaluateStack(s)
        return fn[op](op1, op2)
    else:
        return op

if __name__ == "__main__":

    def test(s, expVal):
        global exprStack
        exprStack = []
        results = BNF().parseString(s)
        val = evaluateStack(exprStack[:])
        if val == expVal:
            print s, "=", val, results, "=>", exprStack
        else:
            print "!!! "+s, val, "!=", expVal, results, "=>", exprStack

    test("((A and B) or C)", [['A','B'], ['C']])
    test("(A and B) or C", [['A','B'], ['C']])
    test("(A or B) and C", [['A', 'C'], ['B', 'C']])
    test("A and B", ['A', 'B'])
    test("A or B", [['A'], ['B']])

The first three tests fail here and only return the first element of each expression in parentheses. A will get pushed to the stack multiple times. It seems that the way I modified fourFn.py has broken my script’s ability to handle these groups. Is there a better way to approach this problem?

edit
After a cup of coffee I realized the problems I was having were pretty easy to solve. My new and and or functions are as follows:

def myAnd(op1, op2):
    if isinstance(op1, str) and isinstance(op2, str):
        newlist = [op1, op2]

    elif isinstance(op1, str):
        newlist = [op1]
        newlist.append(op2)

    elif isinstance(op2, str):
        newlist = op1
        newlist.append(op2)

    else:
        newlist = [op1.append(item) for item in op2]
    return newlist

def myOr(op1, op2):
    if isinstance(op1, str) and isinstance(op2, str):
        newlist = [[op1], [op2]]
        r
    elif isinstance(op1, str):
        newlist = [op1]
        newlist.append([op2])

    elif isinstance(op2, str):
        newlist = [op1]
        newlist.append([op2])

    else:
        newlist = [op1, [op2]]
    return newlist1

And the parser is constructed as follows:

expr = Forward()
atom = element.setParseAction(pushFirst) | (lpar + expr + rpar)
expr << atom + ZeroOrMore((boolop + expr).setParseAction(pushFirst))

A new and more interesting question involves how to deal with a case like this (A or B) and C. The result should be [[A, C], [B, C]]. Is there a typical pyparsing way of dealing with this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:17:41+00:00Added an answer on June 8, 2026 at 11:17 pm

    For future reference, here’s an approach that works for my test cases but that deviates from AST approach suggested above:

    from pyparsing import Literal, Word, Optional, \
        Group, ZeroOrMore, Forward, alphanums
    import ffparser, sys
    
    exprStack = []
    
    def myAnd(op1, op2):
        if isinstance(op1, str) and isinstance(op2, str):
            newlist = [[op1, op2]]
    
        elif isinstance(op1, str):
            newlist = op2
            [item.insert(0, op1) for item in newlist]
    
        elif isinstance(op2, str):
            newlist = op1
            [item.append(op2) for item in op1]
    
        else:
            newlist = [op1.append(item) for item in op2]
    
        return newlist
    
    def myOr(op1, op2):
       if isinstance(op1, str) and isinstance(op2, str):
            newlist = [[op1], [op2]]
    
        elif isinstance(op1, str):
            newlist = op2
            newlist.insert(0, [op1])
    
        elif isinstance(op2, str):
            newlist = op1
            newlist.append([op2])
    
        else:
            newlist = []
            [newlist.append(item) for item in op1]
            [newlist.append(item) for item in op2]
    
        return newlist
    
    def pushFirst(strg, loc, toks):
        exprStack.append(toks[0])
    
    bnf = None
    def BNF():
        """
        boolop  :: 'and' | 'or'
        gene    :: alphanum
        atom    :: gene | '(' expr ')'
        """
        global bnf
        if not bnf:
            element = Word(alphanums)
            andop  = Literal( "and" )
            orop = Literal( "or" )
            lpar  = Literal( "(" ).suppress()
        rpar  = Literal( ")" ).suppress()
        boolop  = andop | orop
    
        expr = Forward()
        atom = element.setParseAction(pushFirst) | (Optional(lpar) + expr + Optional(rpar))
        expr << atom + ZeroOrMore((boolop + expr).setParseAction(pushFirst))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a boolean property IsActive. In the view is a list of objects
I have a list of Parent objects that contain Children. Both Parents and Children
I'd like to have an object that implements both the Map and the List
I have a list of custom objects that I use as the commandName object
I have a list of custom objects that I am loading into an ActivityList
I have a list of objects, where each object has a boolean property named
Let's say you have a List<List<Boolean>> and you want to encode that into binary
I have the following code which takes List of boolean as a parameter then
I have some filters expressed as a list of function List(MyClass => Boolean) .
I have list of input area in the form with id like contact1_title, contact2_title,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.