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 8270337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:32:45+00:00 2026-06-08T06:32:45+00:00

I wish to parse a string representing a boolean expression. The following code is

  • 0

I wish to parse a string representing a boolean expression. The following code is based on many examples and uses pyparsing’s operatorPrecedence helper.

The problem is that it I would like the code to raise an exception when the string cannot be sensibly parsed. As it stands, many invalid expressions just pass quietly and the wrong expression is returned.

import pyparsing

_A = pyparsing.Literal('A')
_B = pyparsing.Literal('B')

bool_operand = pyparsing.Or((_A, _B))

precedence_list = [("NOT", 1, pyparsing.opAssoc.RIGHT),
        ("AND", 2, pyparsing.opAssoc.LEFT),
        ("OR", 2, pyparsing.opAssoc.LEFT),]

bool_parser = pyparsing.operatorPrecedence(bool_operand, precedence_list)

print bool_parser.parseString('A OR B OR NOT A') # A valid string
print bool_parser.parseString('A NOT AND B') # an invalid string

This outputs:

[['A', 'OR', 'B', 'OR', ['NOT', 'A']]]
['A']

So the first string works, but the second string does not raise an exception as desired.

Any ideas how I can do this? I’m not particularly familiar with pyparsing, so I could have missed something there.

  • 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-08T06:32:47+00:00Added an answer on June 8, 2026 at 6:32 am

    Change your definitions of A_ and B_ to use the Keyword class instead of the Literal class.

    Also, when you call parseString, add parseAll=True so that you force the entire input to be parsed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to parse a string and ensure that it is lower case. It
I have the following language i wish to parse using antlr 1.2.2. TEST <name>
I have the following collection ( see image ) . I wish to parse
I am writing some code to parse an xml file of the following format
I wish to parse java source code files, and extract the methods source code.
i wish to parse following XML <?xml version=1.0 encoding=UTF-8?> <product xmlns=http://products.org> <make xmlns=http://camera.org> <model>Camry</model>
Assume final String fname = /dir1/dir2/fname.ext . I do not wish to parse the
I wish to create a call object from a string. alist() , which does
I have a string from which I wish to extract a single word, but
I wish to define a type String such that declaring a variable of type

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.