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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:34:52+00:00 2026-05-12T14:34:52+00:00

Suppose I have a string such as this in a text file: (((var1 AND

  • 0

Suppose I have a string such as this in a text file:

(((var1 AND var2 AND var3) OR var4) AND ((var5 OR var6) AND var7))

After parsing this into the C program and the vars are handled and set correctly it will end up looking something like this:

(((1 AND 0 AND 0) OR 1) AND ((0 OR 1) AND 1))

Are there any useful libraries out there for evaluating expressions that are represented as one string like this? I was thinking I could just call a Perl program with the string as an argument that would be able to return the result easily, but I wasn’t sure if there was a library in C that did this, or if there are any known algorithms for solving such expressions?

What I’m actually looking for is something that would spit out an answer to this expression (maybe parse was a bad word) i.e. 1 or 0.

In a nutshell, it’s a file containing a bunch of random expressions (already known to be in the right format) that need to be evaluated to either 0 or 1. (The example above evaluates to 1 because it results in (1 AND 1)).

  • 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-12T14:34:53+00:00Added an answer on May 12, 2026 at 2:34 pm

    I tried to write the most compact C code for this bool expression evaluation problem. Here is my final code:

    EDIT: deleted

    Here is the added negation handling:

    EDIT: test code added

    char *eval( char *expr, int *res ){
      enum { LEFT, OP1, MID, OP2, RIGHT } state = LEFT;
      enum { AND, OR } op;
      int mid=0, tmp=0, NEG=0;
    
      for( ; ; expr++, state++, NEG=0 ){
        for( ;; expr++ )
             if( *expr == '!'     ) NEG = !NEG;
        else if( *expr != ' '     ) break;
    
             if( *expr == '0'     ){ tmp  =  NEG; }
        else if( *expr == '1'     ){ tmp  = !NEG; }
        else if( *expr == 'A'     ){ op   = AND; expr+=2; }
        else if( *expr == '&'     ){ op   = AND; expr+=1; }
        else if( *expr == 'O'     ){ op   = OR;  expr+=1; }
        else if( *expr == '|'     ){ op   = OR;  expr+=1; }
        else if( *expr == '('     ){ expr = eval( expr+1, &tmp ); if(NEG) tmp=!tmp; }
        else if( *expr == '\0' ||
                 *expr == ')'     ){ if(state == OP2) *res |= mid; return expr; }
    
             if( state == LEFT               ){ *res  = tmp;               }
        else if( state == MID   && op == OR  ){  mid  = tmp;               }
        else if( state == MID   && op == AND ){ *res &= tmp; state = LEFT; }
        else if( state == OP2   && op == OR  ){ *res |= mid; state = OP1;  }
        else if( state == RIGHT              ){  mid &= tmp; state = MID;  }
      }
    }
    

    Testing:

    #include <stdio.h> 
    
    void test( char *expr, int exprval ){
      int result;
      eval( expr, &result );
      printf("expr: '%s' result: %i  %s\n",expr,result,result==exprval?"OK":"FAILED");
    }
    #define TEST(x)   test( #x, x ) 
    
    #define AND       && 
    #define OR        || 
    
    int main(void){
      TEST( ((( 1 AND 0 AND 0) OR 1) AND ((0 OR 1) AND 1)) );
      TEST( !(0 OR (1 AND 0)) OR !1 AND 0 );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have JS code inside a jsp files, such as this: <% String
Suppose I have a string such as this: ABC.DEF.GHI.JKL (This string could have any
Suppose I have a string which contains an HTML file. How do I get
Suppose I have following string: String asd = this is test ass this is
Suppose I have a string like this: one two three four five six seven
Suppose I have a string of text, of all characters Latin-based. With punctuation. How
Suppose you have a string with text in two or more scripts. When you
Suppose I have a large list (around 10,000 entries) of string triples as such:
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {

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.