//The program will return true or false depending on
//whether input is a valid LISP command, a command-line interpreter
#include <iostream>
#include <iomanip>
#include <cstdlib>
usingnamespace std;
//The Lispinterpreter class will take input and return a boolean value
//depending on whether the command is valid or not
class Lispinterpreter{
private:
char ch;//variable to keep track of the last input from the user
public:
bool read();
bool S();
bool R();
bool atom();
bool atom1();
bool text();
};
const string digits = "0123456789";
const string letters = "ABCDEDFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int main()
{
Lispinterpreter interpreter;
while(cin){
cout << "Enter LISP expression: (or ^Z to stop) >> ";
bool result = interpreter.read();
if(result == true) cout << "True\n"; else cout << "FALSE\n";
}
system("pause");
return 0;
}
//All functions belong to the class Lispinterpreter
//Pre: The user has inputted something
bool Lispinterpreter::read()
{
cin >> ch;
return S();
}
//Post: read() is called
//Pre: read() function is satisfied
bool Lispinterpreter::S()
{
if(ch == '(')
{
cin >> ch;
return R();
}
if(ch == '"')
{
cin >> ch;
return text();
}
return atom();
}
//Post: R(), text(), or atom() is called
//Pre: A right paren was the user's input
bool Lispinterpreter::R()
{
if(ch == ')')
{
cin >> ch;
returntrue;
}
if(ch == '"')
{
cin >> ch;
return text() && R();
}
if(ch == '(')
{
cin >> ch;
return R() && R();
}
return atom() && R();
}
//Post: true, text(), or R() is returned/called
//Pre: User's input was not a ')', ""', or '('
bool Lispinterpreter::atom()
{
if(digits.find(ch) < digits.length())
{
cin.get(ch);
return atom1();
}
if(letters.find(ch) < letters.length())
{
cin.get(ch);
return atom1();
}
}
//Post:If user's input included a letter or whole non-negative number, atom() is called.
//Pre: User's input was a letter or whole non-negative number
bool Lispinterpreter::atom1()
{
if(digits.find(ch) < digits.length())
{
cin.get(ch);
return atom1();
}
if(letters.find(ch) < letters.length())
{
cin.get(ch);
return atom1();
}
returntrue;
}
//Post: recursive loop untill the end of the line of text is reached
//Pre: a '"' was the user's input
bool Lispinterpreter::text()
{
if(ch == '"')
{
cin >> ch;
returntrue;
}
if(ch != '"')
{
cin >> ch;
return text();
}
}
//Post: Untill '"' is found the function will call upon itself, if '"' is not
//found the function will return false.
It seems when I input more than three characters I get my message, but other inputs don't work. Examples:
(goodbye cruel world)
"goodbye cruel world"
)goodbye(