size_t Capture::build_grammar(string* rule, size_t location) { this->location = location; int depth_cnt = 0; int cnt = 1; for (string::iterator it = rule->begin(); it != rule->end(); ++it, cnt++) { char c = *it; switch (c) { case '(': if (depth_cnt++ != 0) { this->text.push_back(c); } break; case ')': depth_cnt--; if (depth_cnt > 0) { this->text.push_back(c); } else if (depth_cnt < 0) { cerr << "Found ')' at char " << location << " but was not matched!" << endl; exit(-4); } else { goto end_of_loop; } break; default: if (depth_cnt != 0) { this->text.push_back(c); } break; } } cerr << "Expected ')' but was not found!" << endl; cerr << "( at " << location << " was not matched!" << endl; exit(-2); end_of_loop: this->str_length = cnt; if (verbose) { /* Dump the parsed input */ cout << "Captured: " << this->text << endl; } Lexer* nodes = new Lexer(location); nodes->build_grammar(&this->text); Token* symbols = nodes->get_tokens()->omit_starter(); if (symbols != NULL) { this->setRight(symbols); symbols->setParent(this); } else { cerr << "Error at " << location << endl; } /* Make sure there are no undefined pointers after deleting nodes */ symbols->set_parser(this->parser); delete nodes; return this->length(); }