int Parser::_factor() { int value; TokenType t = _token(); // Factor can be a ( expr ) if (t == TT_LPAREN) { _scanner.advance(); value = _expr(); t = _token(); if (t == TT_RPAREN) { _scanner.advance(); return value; } else { notifyListeners("Syntax error: expecting a ')'"); _success = false; return 0; } } std::stringstream ss; // Factor can be begin w/ a '+' or '-' to indicate sign. if (t == TT_PLUS || t == TT_MINUS) { ss << _scanner.next(); _scanner.advance(); t = _token(); } // A number is expected. if (t == TT_NUMBER) { _parseNumber(); assert(!_buffer.empty()); ss << _buffer; ss >> value; assert(ss.eof()); return value; }
CAbstractEq* CEqParserV2::_parseEquation(std::string &equation, int &it) { CChainEq* rVal = new CChainEq(); CAbstractEq* number = NULL; bool hasNum = false; bool hasOp = false; bool inverseNum = false; char chr = equation.at(it); while ((unsigned)it < equation.length()) { //// Fast exit if statement ends //if(cc::isParClose(equation.at(it))) //{ // DBOUT("Called break"); // it+=2; // break; //} chr = equation.at(it); DBOUT("it = " << it << ", char = " << chr); eOpType op = eOpType::NULL_EQ; if (cc::isDecimal(chr)) { DBOUT("Found decimal: " << chr); number = _parseNumber(equation, it); } else if (cc::isParOpen(chr)) { std::string sub = _getEqSubstr(equation, ++it); DBOUT("Found subEquation "<<sub); int subIt = 0; // dummy number = _parseEquation(sub, subIt); } else if (cc::isAlpha(chr)) { DBOUT("Found alpha " << chr); std::string fcnName = _parseIdentifier(equation, it); number = _parseFunction(fcnName,equation, it); } else if (cc::isOperator(chr)) { op = _parseOperator(equation, it); } else { it++; } hasNum = (number != NULL); hasOp = (op != eOpType::NULL_EQ); DBOUT("hasOp = " << hasOp << " hasNum = " << hasNum); if (hasNum) { bool doAdd = false; // Has Number and Operator if (hasOp) { doAdd = true; } // Default Operator "CONST" if finished else if ((unsigned)it >= equation.length()) { op = eOpType::CONST_EQ; doAdd = true; } // Default Operator "MPL" if undefined else { chr = equation.at(it); DBOUT("Try autoadd MPL (203) char is: " << chr); // Subequation e.g. 4(2+3) // ^ ^ doAdd |= cc::isParOpen(chr); // Function e.g. 4x // ^ doAdd |= cc::isAlpha(chr); // Decimal e.g. (2+3)4 // ^ doAdd |= cc::isDecimal(chr); if (doAdd) { op = eOpType::MPL_EQ; } } if (doAdd) { if (inverseNum) number = new CInvEq(number); DBOUT("Adding Operation "<<op); rVal->addOperation(number, op); op = eOpType::NULL_EQ; number = NULL; // hasNum = false; hasOp = false; inverseNum = false; } } // Inverse negative else { if (op == eOpType::SUB_EQ) { DBOUT("Setting inversenum = true"); inverseNum = true; } } } if (hasNum) { if (inverseNum) number = new CInvEq(number); DBOUT("Adding Operation 101"); rVal->addOperation(number, eOpType::CONST_EQ); } DBOUT("Returning equation, it = "<<it); return rVal; }
int Range::_parseMatlabRange(int *pstart, int *pstep, int *pend) { // Parse a matlab-style [start][:step][:[end]] string, // which includes 1 1:5 1:2:10 :4 and : // Missing numbers take defaults from min_val and max_val int start = 0 , end = 0, step = 0, rc; DBGFPRINTF((stderr, "_parseRange: at '%s'\n", pos)); // Either the first character is a colon, or it's a number // .. but skip leading WS pos += strspn(pos, space); char c = *pos; if (c == '\0') { errmsg = "expecting Matlab range, hit EOS"; DBGFPRINTF((stderr, "_parseRange:%s @ '%s'\n", errmsg, pos)); return 0; } // Try to get first number, OK to fail if ( (rc=_parseNumber(&start)) < 0) { // fatal error in number return rc; } else if (rc == 0) { // Next chr *must* be colon c = *pos; if (c == '\0' || strchr(colons, c) == NULL) { errmsg = "Matlab range does not start with colon or number"; DBGFPRINTF((stderr, "_parseRange:%s @ '%s'\n", errmsg, pos)); return 0; } // It was a colon, but don't move over it, so we see it again blw //++pos; // default the first val if (min_val == RNG_VAL_BAD) { errmsg = "min_val not specified for default range"; DBGFPRINTF((stderr, "_parseRange:%s @ '%s'\n", errmsg, pos)); return -1; } start = min_val; } // Parse up to two subseqent ":number" terms int i; int read_end = 0; end = start; for (i = 0; i < 2; ++i) { // Skip ws pos += strspn(pos, space); // Do we have a colon? c = *pos; if (c != '\0' && strchr(colons, c)) { // We have a colon. If this is the second pass, push the // first pass to the step if (i > 0) { if (read_end == 0) { // I think this means "::" found - just ignore fprintf(stderr, "defaulted step (:: found?)\n"); } else { step = end; } } // Step over colon ++pos; // Either grab a number, or it defaults if ( (rc=_parseNumber(&end)) < 0) { return rc; } else if (rc == 0) { // Didn't find a number, so take default end if (max_val == RNG_VAL_BAD) { errmsg = "max_val not specified for default range"; DBGFPRINTF((stderr, "_parseRange:%s @ '%s'\n", errmsg, pos)); return -1; } end = max_val - 1; } else { read_end = 1; } } } // Maybe choose appropriate default step if (step == 0) { if (end < start) step = -1; else step = 1; } // Should now have it all *pstart = start; *pend = end; *pstep = step; DBGFPRINTF((stderr, "got mat range %d:%d:%d @ '%s'\n", start, step, end, pos)); return 1; }