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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:02:49+00:00 2026-05-29T16:02:49+00:00

I want to create a kind of parser of the form: #include <iostream> #include

  • 0

I want to create a kind of parser of the form:

#include <iostream>
#include <string>
#include <sstream>
#include <cctype>

using namespace std;

bool isValid(istringstream& is)
{
  char ch;
  is.get(ch); //I know get(ch) is a good start but this is as for as I got :)
  .......
  ....
}

int main()
{
  string s;
  while(getline(cin,s))
  {
    istringstream is(s);
    cout<<(isValid(is)? "Expression OK" : "Not OK")<<endl;
  }
}

A boolean function that returns TRUE if the sequence of char is of the form “5” or “(5+3)” or “((5+3)+6)” or “(((4+2)+1)+6)” …etc and FALSE for any other case

Basically, an expression will be considered as valid if it is either a single digit or of the form “open parenthesis-single digit-plus sign-single digit-close parenthesis”

  • Valid Expression = single digit

    and

  • Valid Expression = (Valid Expression + Valid Expression)

Given that there is no limit to the size of the above form (number of opening and closing parenthesis..etc.) I’d like to do that using recursion

Being the newbie that I am.. Thank you for any helpful input!

  • 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-29T16:02:50+00:00Added an answer on May 29, 2026 at 4:02 pm

    To do a recursive solution you’re gonna want to read the string into a buffer first, then do something like this:

    int expression(char* str) {
        if (*str == '(') {
            int e1 = expression(str + 1);
            if (e1 == -1 || *(str + 1 + e) != '+') {
                return -1;
            }
            int e2 = expression(str + 1 + e + 1);
            if (e2 == -1 || *(str + 1 + e + 1 + e2) != ')') {
                return -1;
            }
            return 1 + e1 + 1 + e2 + 1;
        }
    
        if (*str >= '0' || *str <= '9') {
            return 1;
        }
    
        return -1;
    }
    
    bool isvalid(char* str) {
        int e1 = expression(str);
        if (e1 < 0) {
            return false;
        }
        if (e1 == strlen(str)) {
            return true;
        }
        if (*(str + e1) != '+') {
            return false;
        }
        int e2 = expression(str + e1 + 1);
        if (e2 < 0) {
            return false;
        }
        return (e1 + 1 + e2 == strlen(str));
    }
    

    Basically, the expression function returns the length of the valid expression at it’s argument. If it’s argument begins with a parenthesis, it gets the length of the expression after that, verifies the plus after that, then verifies the closing parenthesis after the next expression. If the argument begins with a number, return 1. If something is messed up, return -1. Then using that function we can figure out whether or not the string is valid by some sums and the length of the string.

    I haven’t tested the function at all, but the only case this might fail in that I can think of would be excessive parenthesis: ((5)) for example.

    An alternative to recursion could be some sort of lexical parsing such as this:

    enum {
        ExpectingLeftExpression,
        ExpectingRightExpression,
        ExpectingPlus,
        ExpectingEnd,
    } ParseState;
    
    // returns true if str is valid
    bool check(char* str) {
        ParseState state = ExpectingLeftExpression;
    
        do {
            switch (state) {
                case ExpectingLeftExpression:
                    if (*str == '(') {
                    } else if (*str >= '0' && *str <= '9') {
                        state = ExpectingPlus;
                    } else {
                        printf("Error: Expected left hand expression.");
                        return false;
                    }
                break;
                case ExpectingPlus:
                    if (*str == '+') {
                        state = ExpectingRightExpression;
                    } else {
                        printf("Error: Expected plus.");
                        return false;
                    }
                break;
                case ExpectingRightExpression:
                    if (*str == '(') {
                        state = ExpectingLeftExpression;
                    } else if (*str >= '0' && *str <= '9') {
                        state = ExpectingEnd;
                    } else {
                        printf("Error: Expected right hand expression.");
                        return false;
                    }
                break;
            }
        } while (*(++str));
    
        return true;
    }
    

    That function’s not complete at all, but you should be able to see where it’s going. I think the recursion works better in this case anyways.

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

Sidebar

Related Questions

Basically i want to create XMLDesigner kind of thing in Flex, using which user
Im new to android and I want to create kind of CRUD with SQLite.
I want to create some kind of AJAX script or call that continuously will
I want to create my own kind of a SeekBar which means I want
I want to create a dialog that contains some kind of text element (JLabel/JTextArea
I want to make this kind of query: create procedure something @name varchar(13) as
I want to create one simple application for sending request from iphone(client) by using
I want to create a registration form, with an option to either the register
So I want to create a questionnaire kind of thing. But I want my
I am trying to create a parser for this kind of texts: {{texta1|texta2|texta3} {texta11|texta22}|{textb1|textb2}

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.