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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:11:15+00:00 2026-05-29T10:11:15+00:00

I am trying to parse a Boolean equation that is currently in the form

  • 0

I am trying to parse a Boolean equation that is currently in the form of an NSString.

I want to parse it into a tree so that I can manipulate and simplify the expression into its simplest form.

Like Wolfram Alpha is able to do.

http://www.wolframalpha.com/input/?i=%28A+and+%28A+or+B%29+or+%28B+and+A%29+or+not%28A%29%29+and+%28B+or+not+%28A%29%29

Simplifies the input:

(A and (A or B) or (B and A) or not(A)) and (B or not (A))

to

Not(a) or B

My problem is parsing the equation into a tree object where each tree node has 3 properties:

1.TreeNode *parent

2.NSMutableArray *children

3.NSString *data

thanks

  • 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-05-29T10:11:16+00:00Added an answer on May 29, 2026 at 10:11 am

    Ok so thanks for the help this is the final Objective- C code i wrote to parse a Boolean expression into a tree:

    it takes an expression such as

    A AND B OR C AND NOT(B)
    

    in the form:

    A.B + C./b
    

    It works with brackets parsing with precedence:

    -(TreeNode *)ParseStringIntoTree:(NSString *)InputString{ //input string to parse          
    //returns root-node of tree
    TreeNode *first=[[TreeNode alloc] init];
    TreeNode *current=first;
    NSString *workingString = [NSString stringWithString:InputString];
    
    if (([workingString characterAtIndex:0]=='(') && ([workingString characterAtIndex:workingString.length-1]==')')) {
        NSRange boop={1,workingString.length-2};
        workingString=[workingString substringWithRange:boop];
    }
    int brackCount=0; 
    bool plussesLeft=FALSE;
    for (int pos=0; pos<workingString.length; pos++) {
        char currentC=[workingString characterAtIndex:pos];
        //1
        if (currentC=='(') {
            brackCount++;
        }
        //2
        if (currentC==')') {
            brackCount--;
        } 
        if (currentC=='+' && brackCount==0){
            plussesLeft=TRUE;
        }
    
    }
    //############ PARSE plus signs with  BRACKETS
    brackCount=0;
    int prevPlusPos=-1;
    if (plussesLeft) {
        for (int pos=0; pos<workingString.length; pos++) {
            char currentC=[workingString characterAtIndex:pos];
    
            //1
            if (currentC=='(') {
                brackCount++;
            }
            //2
            if (currentC==')') {
                brackCount--;
            }    
    
            //3
            if (currentC=='+'&&brackCount==0) {
                NSRange boop={prevPlusPos+1, pos-prevPlusPos-1};
                NSString *toParse=[workingString substringWithRange:boop];
                TreeNode *child;
    
                if(toParse.length>1){child=[self ParseStringIntoTree:toParse];}
    
                else{child=[TreeNode newTreeNodeWithValue:toParse];}
                [current addChild:child];
                [current setValue:@"+"];
                prevPlusPos=pos;
            }
    
            //4
            if (pos==workingString.length-1 &&brackCount==0 && prevPlusPos!=-1) {
                NSRange boop={prevPlusPos+1, pos-prevPlusPos};
    
                NSString *toParse=[workingString substringWithRange:boop];
    
                TreeNode *child;
                if(toParse.length>1){child=[self ParseStringIntoTree:toParse];}
                else{child=[TreeNode newTreeNodeWithValue:toParse];};
    
                [current addChild:child];
                [current setValue:@"+"];
            }
        }
    }
    //############ finish PARSE plus signs with  BRACKETS
    
    BOOL dotsLeft=FALSE;
    for (int pos=0; pos<workingString.length; pos++) {
        char currentC=[workingString characterAtIndex:pos];
        //1
        if (currentC=='(') {
            brackCount++;
        }
        //2
        if (currentC==')') {
            brackCount--;
        } 
        if (currentC=='.' && brackCount==0){
            dotsLeft=TRUE;
        }
    
    }
    int prevDotPos=-1;
    if (!plussesLeft && dotsLeft) {
        for (int pos=0; pos<workingString.length; pos++) {
            char currentC=[workingString characterAtIndex:pos];
    
            //1
            if (currentC=='(') {
                brackCount++;
            }
            //2
            if (currentC==')') {
                brackCount--;
            }    
    
            //3
            if (currentC=='.' && brackCount==0 && prevPlusPos==-1) {
                NSRange boop={prevDotPos+1, pos-prevDotPos-1};
                NSString *toParse=[workingString substringWithRange:boop];
    
                TreeNode *child;
    
                if(toParse.length>1){child=[self ParseStringIntoTree:toParse];}
    
                else{child=[TreeNode newTreeNodeWithValue:toParse];}            
                [current addChild:child];
                [current setValue:@"."];
                prevDotPos=pos;
            }
    
            //4
            if (pos==workingString.length-1 &&brackCount==0 && prevDotPos!=-1) {
                NSRange boop={prevDotPos+1, pos-prevDotPos};
    
                NSString *toParse=[workingString substringWithRange:boop];
    
                TreeNode *child;
                if(toParse.length>1){child=[self ParseStringIntoTree:toParse];}
                else{child=[TreeNode newTreeNodeWithValue:toParse];};
    
                [current addChild:child];
                [current setValue:@"."];        
            }
        }
    
    
    
        //left with current being the 
    
    }
    
    if (!plussesLeft && !dotsLeft) {
        if ([workingString characterAtIndex:0]=='/') {
            TreeNode *child=[self ParseStringIntoTree:[workingString substringFromIndex:1]];
            [current addChild:child];
            [current setValue:@"/"];
        }
        if (workingString.length==1) {
            [current setValue:workingString];
        }
    }
    
    return first;
    
    }
    

    Where the treeNode object has properties and methods:

    @interface TreeNode : NSObject{
        NSMutableArray *children;
        TreeNode *parent;
        NSString *value;
    }
    
    +(TreeNode *)newTreeNodeWithValue:(NSString *)Value;
    -(void)addChild:(TreeNode *)child;
    

    The methods do as implied by there names.
    Hope this can help anyone else in future looking for a parser either specifically for Boolean algebra or maybe as a basis for a different parser.

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

Sidebar

Related Questions

I am trying to parse an expression tree for a linq provider and running
Trying to parse Maplines for an airport. Each airport can have X number of
I have been trying to parse Java exceptions that appear in a log for
I have been trying to create a simple app that parse an entire channel
I am trying to parse a webpage, the values i want to parse are
I'm trying to construct an antlr grammar to parse a templating language. that language
I am trying to build an expression tree programmatically. I have in my input,
Trying to parse an HTML document and extract some elements (any links to text
Trying to parse some XML but apparently this is too much for a lazy
Trying to parse an SQL string and pull out the parameters. Ex: select *

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.