i am having issues correcting an error in my program
here is my code:
#include <iostream> // define libraries
#include <fstream>
using namespace std;
// Variable definitions:
ifstream infp; // file handler
enum Tokens {INT_LIT, IDENT, ASSIGN_OP, ADD_OP, SUB_OP, MUL_OP, DIV_OP, LEFT_PAREN, RIGHT_PAREN, LETTER, DIGIT, UNKNOWN,ENDFILE};
Tokens nextToken; // nextToken read from the file.
int charClass; // char class
char lexeme [100]; // number of characters per line
char nextChar; // next char read from the file
int lexLen; // number of characters in lexeme so far.
// function to get the next character from the file
void getChar()
{
if ((nextChar = infp.get()) != EOF)
{
if (isalpha(nextChar)) charClass = LETTER;
else if (isdigit(nextChar)) charClass = DIGIT;
else charClass = UNKNOWN;
}
else charClass = ENDFILE;
}
// function to skip all white spaces
void skipWhiteSpace()
{
while (isspace(nextChar))
getChar();
}
// function add the character read from the file into the arraylexeme
void addChar()
{
if (lexLen < 99)
{
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = 0;
}
else cout << "Error - lexeme is too long \n";
}
// function to determine the token
int lookup(char ch)
{
switch (ch)
{
case '(': addChar(); nextToken = LEFT_PAREN; break;
case ')': addChar(); nextToken = RIGHT_PAREN; break;
case '+': addChar(); nextToken = ADD_OP; break;
case '-': addChar(); nextToken = SUB_OP; break;
case '*': addChar(); nextToken = MUL_OP; break;
case '/': addChar(); nextToken = DIV_OP; break;
case '=': addChar(); nextToken = ASSIGN_OP; break;
default: addChar(); nextToken = ENDFILE; break;
}
return nextToken;
}
void print(Tokens t)
{ // print out the token category.
switch (t)
{
case INT_LIT: cout << "<INT_LIT>"; break;
case IDENT: cout << "<IDENT>"; break;
case ASSIGN_OP: cout << "<ASSIGN_OP>"; break;
case ADD_OP: cout << "<ADD_OP>"; break;
case SUB_OP: cout << "<SUB_OP>"; break;
case MUL_OP: cout << "<MUL_OP>"; break;
case DIV_OP: cout << "<DIV_OP>"; break;
case LEFT_PAREN: cout << "<LEFT_PAREN>"; break;
case RIGHT_PAREN: cout << "<RIGHT_PAREN>"; break;
case LETTER: cout << "<LETTER>"; break;
case DIGIT: cout << "<DIGIT>"; break;
case UNKNOWN: cout << "<UNKNOWN>"; break;
case ENDFILE: cout << "<ENDFILE>"; break;
} /* End of switch */
} /* End of function print */
int lex()
{ //function lex() returns a token for each lexeme read from the file
lexLen = 0; skipWhiteSpace();
switch (charClass)
{
case LETTER:
while (charClass == LETTER || charClass == DIGIT)
{
addChar();
getChar();
}
nextToken = IDENT;
break;
case DIGIT:
while (charClass == DIGIT)
{
addChar();
getChar();
}
nextToken = INT_LIT;
break;
case UNKNOWN: lookup(nextChar);
nextChar = ' ';
break;
case ENDFILE:
nextToken=ENDFILE;
lexeme[0]='E';
lexeme[1]='O';
lexeme[2] = 'F';
lexeme[3] = 0;
break;
} /* End of switch */
cout << "Next token is: "; print(nextToken);
cout <<" Next lexeme is " << lexeme << endl;
return nextToken;
} /* End of function lex */
int main() {
infp.open("front.in");
if (!infp) cout << "ERROR - cannot open front.in \n";
else {
nextChar = ' ';
lex();
do {
expr();
} while (nextToken != EOF);
}
return 0;
}
/* Function expr
Parses strings in the language generated by the rule:
<expr> → <term> {(+ | -) <term>}
equivalent to:
<expr> → <term> | <term + <term> | <term> - <term>
*/
void expr() {
printf("Enter <expr>\n");
term(); // Parse the first term
/* As long as the next token is + or -, call lex to get the next token
and parse the next term */
while (nextToken == ADD_OP || nextToken == SUB_OP){
lex();
term();
}
printf("Exit <expr>\n");
}
/* Function: term(): Parses strings in the language generated by the rule:
<term> -> <factor> {(* | /) <factor>} or equivalent to:
<term> -> <factor> | * <factor> | / <factor>
*/
void term() {
printf("Enter <term>\n");
factor(); // Parse the first factor
/* As long as the next token is * or /, next token and parse the next
factor */
while (nextToken == MUL_OP || nextToken == DIV_OP) {
lex();
factor();
}
printf("Exit <term>\n");
} /* End of function term */
/* Function factor: Parses strings generated by the rule:
<factor> -> id | int_constant | (<expr>) */
void factor() {
printf("Enter <factor>\n");
/* Determine which RHS */
if (nextToken) == IDENT || nextToken == INT_LIT)
lex(); /* For the RHS id, just call lex */
else if (nextToken == LEFT_PAREN) {
lex();
expr();
if (nextToken == RIGHT_PAREN)
lex();
else
cout << "Expected ), found " << nextChar << endl;
} /* End of else if (nextToken == ... */
else cout << "Error: Expected (, found " << nextChar << endl;
printf("Exit <factor>\n");
}
Its compiling perfectly now, wait for the compilation to end and make sure you have front.in file .
#include <iostream> // define libraries
#include <fstream>
using namespace std;
// Variable definitions:
void term();
void expr();
void factor();
ifstream infp; // file handler
enum Tokens {INT_LIT, IDENT, ASSIGN_OP, ADD_OP, SUB_OP, MUL_OP, DIV_OP, LEFT_PAREN, RIGHT_PAREN, LETTER, DIGIT, UNKNOWN,ENDFILE};
Tokens nextToken; // nextToken read from the file.
int charClass; // char class
char lexeme [100]; // number of characters per line
char nextChar; // next char read from the file
int lexLen; // number of characters in lexeme so far.
// function to get the next character from the file
void getChar()
{
if ((nextChar = infp.get()) != EOF)
{
if (isalpha(nextChar)) charClass = LETTER;
else if (isdigit(nextChar)) charClass = DIGIT;
else charClass = UNKNOWN;
}
else charClass = ENDFILE;
}
// function to skip all white spaces
void skipWhiteSpace()
{
while (isspace(nextChar))
getChar();
}
// function add the character read from the file into the arraylexeme
void addChar()
{
if (lexLen < 99)
{
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = 0;
}
else cout << "Error - lexeme is too long \n";
}
// function to determine the token
int lookup(char ch)
{
switch (ch)
{
case '(': addChar(); nextToken = LEFT_PAREN; break;
case ')': addChar(); nextToken = RIGHT_PAREN; break;
case '+': addChar(); nextToken = ADD_OP; break;
case '-': addChar(); nextToken = SUB_OP; break;
case '*': addChar(); nextToken = MUL_OP; break;
case '/': addChar(); nextToken = DIV_OP; break;
case '=': addChar(); nextToken = ASSIGN_OP; break;
default: addChar(); nextToken = ENDFILE; break;
}
return nextToken;
}
void print(Tokens t)
{ // print out the token category.
switch (t)
{
case INT_LIT: cout << "<INT_LIT>"; break;
case IDENT: cout << "<IDENT>"; break;
case ASSIGN_OP: cout << "<ASSIGN_OP>"; break;
case ADD_OP: cout << "<ADD_OP>"; break;
case SUB_OP: cout << "<SUB_OP>"; break;
case MUL_OP: cout << "<MUL_OP>"; break;
case DIV_OP: cout << "<DIV_OP>"; break;
case LEFT_PAREN: cout << "<LEFT_PAREN>"; break;
case RIGHT_PAREN: cout << "<RIGHT_PAREN>"; break;
case LETTER: cout << "<LETTER>"; break;
case DIGIT: cout << "<DIGIT>"; break;
case UNKNOWN: cout << "<UNKNOWN>"; break;
case ENDFILE: cout << "<ENDFILE>"; break;
} /* End of switch */
} /* End of function print */
int lex()
{ //function lex() returns a token for each lexeme read from the file
lexLen = 0; skipWhiteSpace();
switch (charClass)
{
case LETTER:
while (charClass == LETTER || charClass == DIGIT)
{
addChar();
getChar();
}
nextToken = IDENT;
break;
case DIGIT:
while (charClass == DIGIT)
{
addChar();
getChar();
}
nextToken = INT_LIT;
break;
case UNKNOWN: lookup(nextChar);
nextChar = ' ';
break;
case ENDFILE:
nextToken=ENDFILE;
lexeme[0]='E';
lexeme[1]='O';
lexeme[2] = 'F';
lexeme[3] = 0;
break;
} /* End of switch */
cout << "Next token is: "; print(nextToken);
cout <<" Next lexeme is " << lexeme << endl;
return nextToken;
} /* End of function lex */
int main() {
infp.open("front.in");
if (!infp) cout << "ERROR - cannot open front.in \n";
else {
nextChar = ' ';
lex();
do {
expr();
} while (nextToken != EOF);
}
return 0;
}
/* Function expr
Parses strings in the language generated by the rule:
<expr> → <term> {(+ | -) <term>}
equivalent to:
<expr> → <term> | <term + <term> | <term> - <term>
*/
void expr() {
printf("Enter <expr>\n");
term(); // Parse the first term
/* As long as the next token is + or -, call lex to get the next token
and parse the next term */
while (nextToken == ADD_OP || nextToken == SUB_OP){
lex();
term();
}
printf("Exit <expr>\n");
}
/* Function: term(): Parses strings in the language generated by the rule:
<term> -> <factor> {(* | /) <factor>} or equivalent to:
<term> -> <factor> | * <factor> | / <factor>
*/
void term() {
printf("Enter <term>\n");
factor(); // Parse the first factor
/* As long as the next token is * or /, next token and parse the next
factor */
while (nextToken == MUL_OP || nextToken == DIV_OP) {
lex();
factor();
}
printf("Exit <term>\n");
} /* End of function term */
/* Function factor: Parses strings generated by the rule:
<factor> -> id | int_constant | (<expr>) */
void factor() {
printf("Enter <factor>\n");
/* Determine which RHS */
if (nextToken == IDENT || nextToken == INT_LIT)
lex(); /* For the RHS id, just call lex */
else if (nextToken == LEFT_PAREN) {
lex();
expr();
if (nextToken == RIGHT_PAREN)
lex();
else
cout << "Expected ), found " << nextChar << endl;
} /* End of else if (nextToken == ... */
else cout << "Error: Expected (, found " << nextChar << endl;
printf("Exit <factor>\n");
}
===========================
SEE OUTPUT

PLEASE COMMENT if there is any concern.
=============================
i am having issues correcting an error in my program here is my code: #include <iostream>...
rewrite this c code in python #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE...
Hello i am having a bit of trouble with a verified exit for my program. For some reason when trying to exit it loops to the team selection function? C++ Visual Studio 2017 #include "cPlayer.h" char chChoice1 = ' '; char chChoice3 = ' '; cPlayer::cPlayer() { } cPlayer::~cPlayer() { } void cPlayer::fMenu() { char chChoice3 = ' '; do { cout << "\n\t--Menu--" << endl; cout << "1) Enter Player Name" <<...
This is C++ code for parking fee management program #include <iostream> #include <iomanip> using namespace std; void input(char& car, int& ihour,int& imin, int& ohour, int& omin); void time(char car, int ihour, int imin, int ohour, int omin, int& phour, int& pmin, int& round, double& total); void parkingCharge (char car, int round, double& total); void print(char car, int ihour, int imin, int ohour, int omin, int phour, int pmin, int round, double total); int main() { char car; int ihour; int...
c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...
Here is the code I have so far. I'm trying to figure out how to
implement a boolean and use precedence. The 2nd expression should
be 14 but it comes out as 28 so I'm definitely not
understanding.
#include <stack>
#include <iostream>
#include <string>
using namespace std;
// Function to find precedence of
// operators.
int precedence(char op) {
if (op == '+' || op == '-')
return 1;
if (op == '*' || op ==...
How to code up the postfixEval function using the instructions in the comments of the function? // postfixEvalTest.cpp #include "Token.h" #include <iostream> #include <vector> #include <stack> using namespace std; // postfix evaluate function prototype double postfixEval(vector<Token> &expr); int main() { vector<Token> postfix; postfix.push_back(Token(VALUE,4.0)); postfix.push_back(Token(VALUE,3.0)); postfix.push_back(Token(VALUE,2.0)); postfix.push_back(Token(OPERATOR,'*')); postfix.push_back(Token(OPERATOR,'+')); postfix.push_back(Token(VALUE,18.0)); postfix.push_back(Token(VALUE,6.0)); postfix.push_back(Token(OPERATOR,'/')); postfix.push_back(Token(OPERATOR,'-')); double result = 0.0; //result = postfixEval(postfix); cout << "The result of 4 3 2 * + 18 6 / - .... is " << result << endl; vector<Token>...
This is my code where i am having errors: #include <iostream> #include <string> #include <ctime> #include <cstdlib> namespace Action { enum Shape { ROCK, PAPER, SCISSORS, INVALID_SHAPE }; Shape charToShape(char selection); bool validShape(char selection); string shapeToString(Shape object); <--------[ERROR ON THIS LINE] Shape winningShape(Shape shape1, Shape shape2); } string Action::shapeToString(Action::Shape object) [ERROR HERE ON THIS LINE] { // Return the name of the shape. switch (object) { case Action::ROCK: return "Rock"; case Action::PAPER: return "Paper"; case Action::SCISSORS: return "Scissors"; default: return...
Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found; string document[1000][6]; ifstream infile; char s[1000];...
// C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList { struct patient *patient; struct patientList *next; } *list = NULL; ...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...