0

I'm in a method, named total_parsing, and I create a local variable istringstream stream(to_analize) (where to_analize is the string that I have to analyze).

When I reach the special char ( or [ I want to claim a method, reading_between_bracket, that read the string from the open bracket to the closing bracket. After that I want that the method reading_between_bracket give me the possibility to continue the reading in total parsing. I need a method for the reading of string in bracket because I want to claim it recursively every time I reach a bracket. The string is in the format of the example.

Example -( B + D -( ( G*F) -H )))

void total_parsing(string to_analize) {
  istrinstream stream(to_analize);
  do {
    // DO OTHER OPERATION WITHOUT BRACKET
    if (token == "(" || !token.find('(')) {
      stream = reading_between_bracket(stream);
    }
  } while (stream >> token);
}

istringstream reading_between_bracket(istringstream stream) {
  // DO THE OPERATION BETWEEN BRACKET
  // RECURSIVE CALL OF THIS METHOD IF I REACH A BRACKET,RETURN THE STREAM
  return stream(or a pointer that make possible to continue reading);
}

PS I have to use C++11 and I can utilize STL and sstream.

The code that is in description didn't work

David G
  • 94,763
  • 41
  • 167
  • 253
Rikissssss
  • 17
  • 5

1 Answers1

0

The code did not work because passing by value requires making a copy, and stringstream objects may not be copied. See also.

So instead, pass the stream by reference:

void parse(string input){
    istringstream stream(input);
    string token;
    while (stream >> token) {
        if (is_open_bracket(token)) {
            parse_inside_brackets(stream);
        }
    }
}

void parse_inside_brackets(istringstream& stream) {
    // notice the '&'
    // do some processing here; the stream's internal position is updated
    // there is no need to return anything; the change to `stream` will be
    // reflected in the caller
}

To accomplish the recursion you describe, you probably want something more like:

void parse(string input) { // helper function to set up recursion
    parse_rec(istringstream(input), 0);
}

void parse_rec(istringstream& stream, int level) {
    // 'level' param indicates how many nested brackets we've seen
    string token;
    while (stream >> token) {
        if (is_close_bracket(token) { return; }
        else if (is_open_bracket(token)) {
            parse_rec(stream, level + 1);
        }
        else {
            // handle token at this level
        }
    }
    // If we get here, we ran out of tokens. So if we're not at the base level...
    if (level) {
        // ... then we had an unmatched bracket, so do error handling
    }
}

(But it may be the case that you should read up on more sophisticated parsing techniques :) And on the other hand, if you can handle the task with this approach, then maybe you don't even need the recursion, just tracking of the indentation level.)

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • My problem is to read a operation like this -((((A + B)+ C) + D)*E) where bracket isn't separated by space – Rikissssss Jul 13 '19 at 15:20
  • You will need a more sophisticated approach to *tokenizing* the stream, than simply using `>>`. Hopefully that term will help with your searches ;) – Karl Knechtel Jul 14 '19 at 06:00