// FUNCTION: receiveInput // Recieves input as a string and returns it as a double (if valid) double Converter::receiveInput() { std::string inputString; std::cout << "\nINPUT:\n"; std::getline(std::cin, inputString); bool invalidInput = true; double inputValue; if (stringIsNumber(inputString)) { inputValue = std::stod(inputString); if (inputValue >= -pow(10, 13) && inputValue <= pow(10, 13)) invalidInput = false; } while (invalidInput) { std::cout << "Invalid input. Please try again: "; std::getline(std::cin, inputString); if (stringIsNumber(inputString)) { inputValue = std::stod(inputString); if (inputValue >= -pow(10, 13) && inputValue <= pow(10, 13)) invalidInput = false; } } return inputValue; }
/* * "int isValidVariable(char *str)" * check if string of valid length, does not start with number or symbol * does not contain symbols in the middle, special symbols have been check by now */ int isValidVariableAndNotReserved(char *str){ // base case check for null or is a reserved word or is numerical if (str == NULL || isReserverdWord(str) || stringIsNumber(str)) { return 0; } // get the string length int len = strlen(str); // is token a punctuation or symbol if (len == 1) { if (charType(str[0]) == 3){ return 0; } } // does variable start with a letter, or is of legal size if ((charType(str[0]) != 2) || len > MAX_VAR_LEN ) { printError(err31, str); //printf("Error, variable %s is invalid\n", str); exit(EXIT_FAILURE); } // at this point, isnumber, is symbol, is reserved word, is special char have all been checked return 1; }
/* * IdentifyInputToken(char *caCleanInputTokens[]) * take each token and check if its a reserved word, numerical or symbol pail, or variable * if token is an illegal lexeme, print out error and exit program * */ void IdentifyInputToken(char *caCleanInputTokens[], namerecord_t *record_table){ int ssNext = 0; int ss = 0; int rw = 0; int tknlen = 0; int tknLenNext = 0; int i = 0; int vs = 0; int LexRecordIndex = 0; // keep track of the lexeme table records for (i = 0; i < m_nCleanInputTokens ; i++) { char *str = caCleanInputTokens[i]; tknlen = strlen(str); // typedef enum {lexConstant = 1, lexVar, lexProc} eLexemeKind; // [integer = constant = 1], [variable = var = 2], [reserved word = proc = 3] // check if it is a reserved word if ( ( rw = isReserverdWord(str) ) && tknlen > 1 ){ // insert token record in record_table insertNamerecord_table(LexRecordIndex++, record_table, lexProc, 0, ' ', ' ', str, m_naWsym[(rw - 1)] ); continue; } // check if token is number if (stringIsNumber(str)){ // if is invalid length or illegal number, it will fail at check // insert token record in record_table insertNamerecord_table(LexRecordIndex++, record_table, lexConstant, 0, ' ', ' ', str, 3 ); continue; } // check if token is variable if (isValidVariableAndNotReserved(str)){ // if is invalid length or illegal variable, it will fail at check // insert token record in record_table insertNamerecord_table(LexRecordIndex++, record_table, lexVar, 0, ' ', ' ', str, 2 ); continue; } // check if token special symbol if ( ( ss = isSpecialChar(str[0]) ) && tknlen == 1 ){ // current token is a symbol, check if next token is symbol // do not increase index counter, just look ahead if (i < m_nCleanInputTokens -1){ char *strNext = caCleanInputTokens[i + 1]; tknLenNext = strlen(str); // isSpecialChar(strNext[0]) == 7 if ( strlen(strNext) == 1 && (isSpecialChar(strNext[0]) == 12 || isSpecialChar(strNext[0]) == 13 ) ) { // next token is of length 1, is a symbol // check if they are a legal symbol pair : >=, <=, < >, := if ((vs = validSymbolPair(str[0], strNext[0])) ) { i++; // insert token record in record_table insertNamerecord_table(LexRecordIndex++, record_table, lexProc, 1, str[0], strNext[0], " ", vs ); continue; } } } // insert token record in record_table insertNamerecord_table(LexRecordIndex++, record_table, lexProc, 0, ' ', ' ', str, m_naSpecialSymbols[(ss - 1)] ); continue; } } }