This is sad... I'm finishing up my CS minor and I still can't do this crap (this is why I dropped it from my major... wasn't learning a damn thing).
Anyway...
Trying to write a scanner/parser for a calculator language in C++. Here's the scanner that I'm going to use (not the one I wrote, but it's more correct to the calculator language)
Right up at the top there, it says the problem. cin stops at blanks. What should I use to bypass this? Or, conversly, should I just require that the input be without blanks? Might be easier, but I would like to have more functionality.
At any rate, thanks in advance for any help/suggestions.
Anyway...
Trying to write a scanner/parser for a calculator language in C++. Here's the scanner that I'm going to use (not the one I wrote, but it's more correct to the calculator language)
code:<HR>Code:// COSC 325 Project 1 // C++ version by S. Dean 9/19/2001 // apparently the cin stops at blanks, so this is untested for bypassing them 9/19/2001<P>#include "iostream.h" #include "string.h"<P>void main() { char s[100]; int i; cout << "Enter a string"<< endl; cin >> s; cout << "The string was " << s << endl; i = 0; while (s[i] != 0) { // bypass any blanks while (s[i] == ' ') i++; <P> // recognize operator tokens if ((s[i] == '+') || (s[i] == '-') || (s[i] == '*') || (s[i] == '/')) { cout << s[i] << endl; i++; } else // recognize the beginning of an identifier if (((s[i] >= 'a') && (s[i]<= 'z')) || ((s[i] >= 'A') && (s[i] <= 'Z'))) { cout << s[i]; i++; // and "accumulate" the rest of it while (((s[i] >= 'a') && (s[i]<= 'z')) || ((s[i] >= 'A') && (s[i] <= 'Z')) || ((s[i] >= '0') && (s[i] <= '9'))) { cout << s[i]; i++;} cout << endl; // and then do the linefeed } else // recognize the beginning of a literal (numeric constant) if ((s[i] >= '0') && (s[i]<= '9')) { cout << s[i]; i++; // and "accumulate" the rest of it while ((s[i] >= '0') && (s[i]<= '9')) { cout << s[i]; i++;} cout << endl; // and then do the linefeed } else // recognize the assignment operator if ((s[i] == ':') && (s[i+1] == '=')) { cout << ":="<< endl ; i++; i++;} else // recognize the $$ if ((s[i] == '$') && (s[i+1] == '$')) { cout << "$$" << endl; i++; i++;} else // recognize left parenthesis if (s[i] == '(') { cout << '(' << endl; i++;} else // recognize right parenthesis (no checking to be sure they match) if (s[i] == ')') { cout << ')' << endl; i++;} else { // invalid character cout << "Illegal character:" << s[i] << endl; i++; } }<P>}
Right up at the top there, it says the problem. cin stops at blanks. What should I use to bypass this? Or, conversly, should I just require that the input be without blanks? Might be easier, but I would like to have more functionality.
At any rate, thanks in advance for any help/suggestions.