本文整理汇总了C++中TokenScanner::setInput方法的典型用法代码示例。如果您正苦于以下问题:C++ TokenScanner::setInput方法的具体用法?C++ TokenScanner::setInput怎么用?C++ TokenScanner::setInput使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TokenScanner
的用法示例。
在下文中一共展示了TokenScanner::setInput方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: isHaiku
bool isHaiku(string filename) {
fstream infile;
infile.open(filename.c_str());
string temp;
int count = 0;
TokenScanner scanner;
while(getline(infile, temp)){
count++;
int syllables = 0;
scanner.setInput(temp);
while (scanner.hasMoreTokens()){
syllables += syllablesIn(scanner.nextToken());
}
if((count == 1 || count ==3)&&syllables!=5){
infile.close();
return false;
}
if (count == 2 && syllables !=7){
infile.close();
return false;
}
if (count >3){
infile.close();
return false;
}
}
return true;
}
示例2: processLine
void processLine(string line, Program & program, EvalState & state) {
TokenScanner scanner;
scanner.ignoreWhitespace();
scanner.scanNumbers();
scanner.setInput(line);
string str=scanner.nextToken();
if (scanner.getTokenType(str)==NUMBER) {
int lineNumber=stringToInteger(str);
string token=scanner.nextToken();
scanner.saveToken(token);
if (token!="") {
program.addSourceLine(lineNumber, line);
program.setParsedStatement(lineNumber, parseStatement(scanner));
}
else {
program.removeSourceLine(lineNumber);
}
}
else if (str=="LIST") {
for (int i=program.getFirstLineNumber(); i!=-1; i=program.getNextLineNumber(i)) {
cout << program.getSourceLine(i)<<endl;
}
}
else if (str=="CLEAR") {
program.clear();
}
else if(str=="QUIT") exit(0);
else if (str=="HELP") cout<<"This is a minimal BASIC interpreter."<<endl;
else if (str=="RUN") {
int currentLine=program.getFirstLineNumber();
state.setCurrentLine(currentLine);
while (currentLine!=-1) {
program.getParsedStatement(currentLine)->execute(state);
if(currentLine!=state.getCurrentLine()) {
currentLine=state.getCurrentLine();
}
else {
currentLine=program.getNextLineNumber(currentLine);
state.setCurrentLine(currentLine);
}
}
}
}
示例3: toAlpha
void toAlpha(std::string word, Map<std::string, std::string>& symbolTable) {
TokenScanner scanner;
scanner.ignoreWhitespace();
for(char i = 'A'; i < 'Z'; i++)
scanner.addWordCharacters(symbolTable.get(charToString(i)));
scanner.setInput(word);
while(scanner.hasMoreTokens()) {
std::string symbol = scanner.nextToken();
cout << symbolTable.get(symbol);
}
cout << endl;
}
示例4: processLine
void processLine(string line, Program & program, EvalState & state) {
TokenScanner scanner;
scanner.ignoreWhitespace();
scanner.scanNumbers();
scanner.setInput(line);
// Expression *exp = parseExp(scanner);
// int value = exp->eval(state);
// cout << value << endl;
// delete exp;
if (!scanner.hasMoreTokens()) {
return;
}
string firstToken = toUpperCase(scanner.nextToken());
if (firstToken == "RUN") {
runProgram(program, state);
}
else if (firstToken == "QUIT") {
exit(0);
}
else if (firstToken == "HELP") {
dspHelp();
}
else if (firstToken == "CLEAR") {
program.clear();
}
else if (firstToken == "LIST") {
listCmd(program);
}
else if (firstToken == "INPUT" || firstToken == "PRINT" || firstToken == "LET") {
scanner.saveToken(firstToken);
Statement * stmt = parseStatement(scanner);
stmt->execute(state);
delete stmt;
}
else {
int lineNumber = stringToInteger(firstToken);
if (!scanner.hasMoreTokens()) {
program.removeSourceLine(lineNumber);
}
else {
int length = firstToken.length();
string source = line.substr(length);
program.addSourceLine(lineNumber, source);
Statement * stmt = parseStatement(scanner);
program.setParsedStatement(lineNumber, stmt);
}
}
}
示例5: interpreter
void interpreter(void) {
std::string line;
TokenScanner scanner;
scanner.ignoreWhitespace();
scanner.scanNumbers();
Map<std::string, std::string> symbolTable;
cout << "Interpreter is running: " << endl;
// Main loop:
std::string key, value;
while(true) {
cout << "> ";
line = getLine();
if(line == "quit")
break;
else if(line == "list") {
for(std::string key : symbolTable) {
cout << key << " = " << symbolTable.get(key) << endl;
}
}
else if(symbolTable.containsKey(line))
cout << symbolTable.get(line) << endl;
// Parse statements:
else {
scanner.setInput(line);
bool isValue = false;
while(scanner.hasMoreTokens()) {
std::string token = scanner.nextToken();
if(isValue)
value = token;
else if(token == "=")
isValue = true;
else
key = token;
}
symbolTable.add(key, value);
}
}
cout << "Interpreter is stopped." << endl;
}
示例6: processLine
void processLine(string line, Program & program, EvaluationContext & context) {
TokenScanner scanner;
scanner.ignoreWhitespace();
scanner.setInput(line);
string firstToken = toUpperCase(scanner.nextToken());
if (firstToken == "RUN") {
runProgram(program, context);
}
else if (firstToken == "LIST") {
listProgram(program, scanner);
}
else if (firstToken == "CLEAR") {
program.clear();
}
else if (firstToken == "HELP") {
if (!scanner.hasMoreTokens()) {
displayHelp();
}
else {
specificHelp(scanner);
}
}
else if (firstToken == "QUIT") {
exit(0);
}
else {
int lineNumber = stringToInteger(firstToken);
if (!scanner.hasMoreTokens()) {
program.removeSourceLine(lineNumber);
}
else {
Statement *stmt = parseStatement(scanner);
program.addSourceLine(lineNumber, line);
program.setParsedStatement(lineNumber, stmt);
}
}
}
示例7: processLine
/*
* Function: processLine
* Usage: processLine(line, program, state);
* -----------------------------------------
* Processes a single line entered by the user. In this version,
* the implementation does exactly what the interpreter program
* does in Chapter 19: read a line, parse it as an expression,
* and then print the result. In your implementation, you will
* need to replace this method with one that can respond correctly
* when the user enters a program line (which begins with a number)
* or one of the BASIC commands, such as LIST or RUN.
*/
void processLine(string line, Program & program, EvalState & state) {
TokenScanner scanner;
scanner.ignoreWhitespace();
scanner.scanNumbers();
scanner.setInput(line);
string token=scanner.nextToken();
TokenType type=scanner.getTokenType(token);
int lineNumb;
int priorLineNumb;
if(type==NUMBER){
if(scanner.hasMoreTokens()){
program.addSourceLine(stringToInteger(token),line);
Statement *stmt=parseStatement(scanner);
program.setParsedStatement(stringToInteger(token),stmt); //link statement to line number
}
else
program.removeSourceLine(stringToInteger(token));
}
if(type==WORD){
if(token=="LIST"){
lineNumb=program.getFirstLineNumber();
while(lineNumb!=-1){
cout<<program.getSourceLine(lineNumb)<<endl;
lineNumb=program.getNextLineNumber(lineNumb);
}
}
else if(token=="RUN"){
state.setCurrentLine(program.getFirstLineNumber());
lineNumb=state.getCurrentLine();
while(lineNumb!=-1){
if(program.hasLineNumber(lineNumb)==false)
error("Invalid line reference at program line "+integerToString(priorLineNumb));
Statement *stmt=program.getParsedStatement(lineNumb);
state.setCurrentLine(program.getNextLineNumber(lineNumb));
stmt->execute(state);
priorLineNumb=lineNumb;
lineNumb=state.getCurrentLine();
}
}
else if(token=="CLEAR"){
program.clear();
}
else if(token=="HELP"){
cout<<"Minimal Basic Statements"<<endl;
cout<<"REM This statement used for comments"<<endl;
cout<<"LET This statement is BASIC's assigment statement"<<endl;
cout<<"PRINT In minimal BASIC, the PRINT statement has the form: "<<endl;
cout<<" PRINT exp"<<endl;
cout<<" where exp is an expression."<<endl;
cout<<"INPUT In the minimal version of the BASIC interpreter, the INPUT statement has the form:"<<endl;
cout<<" INPUT var"<<endl;
cout<<" where var is a variable read in from the user"<<endl;
cout<<"GOTO This statement has the syntax"<<endl;
cout<<" GOTO n"<<endl;
cout<<" which forces the program to continue from line n instead of continuing with the next statement"<<endl;
cout<<"IF This statement provides conditional control. The syntax for this statement is: "<<endl;
cout<<" IF exp1 op exp2 THEN n"<<endl;
cout<<" where exp1 and exp2 are expressions and op is one of the conditional operators =, <, or >."<<endl;
cout<<"END This statment marks the end of the program."<<endl;
cout<<"Commands to control the BASIC interpreter"<<endl;
cout<<"RUN This command starts program execution beginning at the lowest-numbered line. "<<endl;
cout<<"LIST This command lists the steps in the program in numerical sequence."<<endl;
cout<<"CLEAR This command deletes the program so the user can start entering a new one."<<endl;
cout<<"HELP This command provides a simple help message describing the interpreter."<<endl;
cout<<"QUIT Typing QUIT exits from the BASIC interpreter by calling exit(0)."<<endl;
}
else if(token=="QUIT"){
exit(0);
}
else
cout<<"Invalid command"<<endl;
}
}
示例8: main
/* main() manages the user interface;
* instantiates priority queue object, then operates loop to read input
* from user and call the appropriate priority queue method
*/
int main() {
PriorityQueue pq;
TokenScanner scanner;
while (true) {
string line = getLine("> ");
scanner.setInput(line);
scanner.ignoreWhitespace();
string cmd=scanner.nextToken();
if (cmd == "help") {
helpCommand();
}
else if (cmd == "enqueue") {
if(scanner.hasMoreTokens()){
string value=scanner.nextToken();
if(scanner.hasMoreTokens()){
scanner.scanNumbers();
string priorityStr=scanner.nextToken();
double priority=stringToDouble(priorityStr);
pq.enqueue(value,priority);
}
else
pq.enqueue(value);
}
}
else if (cmd == "dequeue") {
if(pq.isEmpty())
cout<<"The queue is empty"<<endl;
else
cout<<pq.dequeue()<<endl;
}
else if (cmd == "peek") {
if(pq.isEmpty())
cout<<"The queue is empty"<<endl;
else
cout<<pq.peek()<<endl;
}
else if (cmd == "peekPriority"||cmd=="peekpriority") {
if(pq.isEmpty())
cout<<"The queue is empty"<<endl;
else
cout<<pq.peekPriority()<<endl;
}
else if (cmd == "clear") {
pq.clear();
}
else if (cmd == "size") {
cout<<pq.size()<<endl;
}
else if (cmd == "isEmpty"||cmd=="isempty") {
if(pq.isEmpty())
cout<<"true";
else
cout<<"false";
cout<<endl;
}
else if(cmd=="list")
list(pq);
else {
cout << "Undefined command: " << cmd << endl;
}
}
return 0;
}
示例9: processLine
void processLine(string line, Program & program, EvalState & state) {
/* TokenScanner scanner;
scanner.ignoreWhitespace();
scanner.scanNumbers();
scanner.setInput(line);
Expression *exp = parseExp(scanner);
int value = exp->eval(state);
cout << value << endl;
delete exp;*/
TokenScanner scanner;
scanner.ignoreWhitespace();
scanner.scanNumbers();
scanner.setInput(line);
string token = scanner.nextToken();
TokenType type = scanner.getTokenType(token);
if (type == WORD){
if (token == "RUN")
{
int current_line=program.getFirstLineNumber();
while (current_line>=0)
{
int next_line = program.getNextLineNumber(current_line);
Statement* stat=program.getParsedStatement(current_line);
StatementType type = stat->get_type();
if (type == END)
break;
if (type == GOTO)
{
((Gotostmt*)stat)->execute(state,current_line);
continue;
}
if (type == IF)
{
((Ifstmt*)stat)->execute(state, current_line);
if (current_line != -2)
continue;
}
stat->execute(state);
current_line = next_line;
}
}
if (token == "QUIT")
{
exit(0);
}
if (token == "CLEAR")
{
program.clear();
EvalState state1;
state = state1;
}
if (token == "HELP")
{
}
if (token == "LIST")
{
int current_line = program.getFirstLineNumber();
while (current_line != -1)
{
cout << program.getSourceLine(current_line) << endl;
current_line = program.getNextLineNumber(current_line);
}
}
if (token == "INPUT"){
string st = scanner.nextToken();
if (scanner.hasMoreTokens())
error("SYNTAX ERROR");
Inputstmt inputst(st);
inputst.execute(state);
}
if (token == "PRINT"){
Printstmt printst(scanner);
printst.execute(state);
}
if (token == "LET")
{
Letstmt lets(scanner);
lets.execute(state);
}
if (token != "RUN"&&token != "CLEAR"&&token != "HELP"&&token != "LIST"&&token != "QUIT"&&token != "PRINT"&&token != "LET"&&token != "INPUT")
error("SYNTAX ERROR");
}
else if (type == NUMBER){
int line_num = atoi(token.c_str());
string token2 = scanner.nextToken();
if (token2 == "")
{
program.removeSourceLine(line_num);
return;
//.........这里部分代码省略.........