Modify Tokenstreamget to return Tokenprint when it sees a ne
Modify Token_stream::get() to return Token(print) when it sees a newline. This implies looking for whitespace characters and treating newline (\'\ \') specially. You might find the standard library function isspace(ch), which returns true if ch is a whitespace character, useful.
This is what I have so far, but I can\'t seem to figure out the errors
Token Token_stream::get(){
if(full){
full = false;
return buffer; // buffer never gets deleted!
}
char ch;
cin >> ch;
switch(ch){
case print :
case \'=\' :
case \'(\' :
case \')\' :
case \'{\' :
case \'}\' :
case \'+\' :
case \'-\' :
case \'*\' :
case \'/\' :
case \'%\' :
return Token{ch};
case \'.\' : case \'0\' : case \'1\' : case \'2\' : case \'3\' : case \'4\' :
case \'5\' : case \'6\' : case \'7\' : case \'8\' : case \'9\' :
{
cin.putback(ch);
double val;
cin >> val;
return Token{number, val};
}
default :
if(isalpha(ch) || ch == \'_\'){
string s;
s+= ch;
while(cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch == \'_\'))
s+=ch;
cin.putback(ch);
//start keywords
if(s == declkey) return Token (let);
if(s == conskey) return Token (cons);
if(s == helpkey) return Token (help);
if(s == quitkey) return Token (quit);
//end keywords
return Token{name, s}; // returns a name with type name
}
error(\"Invalid Token\");
}
}
Solution
Please try below code. Let me know if your\'re facing further issue.
#include <iostream>
using namespace std;
int main()
{
cout << \"Hello World\" << endl;
return 0;
}
Token Token_stream::get(){
if(full){
full = false;
return buffer; // buffer never gets deleted!
}
char ch;
cin >> ch;
switch(ch){
case print :
case \'=\' :
case \'(\' :
case \')\' :
case \'{\' :
case \'}\' :
case \'+\' :
case \'-\' :
case \'*\' :
case \'/\' :
case \'%\' :
return Token{ch};
case \'.\' : case \'0\' : case \'1\' : case \'2\' : case \'3\' : case \'4\' :
case \'5\' : case \'6\' : case \'7\' : case \'8\' : case \'9\' :
{
cin.putback(ch);
double val;
cin >> val;
return Token{number, val};
}
default :
if(isalpha(ch) == 0 || ch == \'_\'){
string s;
s+= ch;
while(cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch == \'_\'))
s+=ch;
cin.putback(ch);
//start keywords
if(s == declkey) return Token (let);
if(s == conskey) return Token (cons);
if(s == helpkey) return Token (help);
if(s == quitkey) return Token (quit);
//end keywords
return Token{name, s}; // returns a name with type name
}
error(\"Invalid Token\");
}
}

