Question: I am doing a lexical analyzer program which reads code from a text file, read all the lexeme inside, separates them to different token categories,
I am doing a lexical analyzer program which reads code from a text file, read all the lexeme inside, separates them to different token categories, and output the outcome to another text file. However, my code is not recognizing digits properly. I attached the code, input text file and test output below. Please point what did I do wrong. If you can help me finish this incomplete program, I can't appreciate you more.
Thank you so much for your help.
void output(string lexeme, string token) { cout
void keyword_identifier(string lexeme) { for (size_t i = 0; i
bool isseparator(string lexeme) { for (size_t i = 0; i
bool isoperator(string lexeme) { for (size_t t = 0; t
int main() { //Initialize a string that used to store lexeme //it will be emptied at whenever an output has been written into text file string lexeme = ""; char reader;
ifstream fin; fin.open("input.txt");
while (!fin.eof()) // Statement1: read the code until reaches the end of code { lexeme = ""; reader = fin.get();
if (isdigit(reader)) { while (isdigit(reader)) { lexeme += reader; reader = fin.get(); if (reader == ' ') break; } } else if (isalpha(reader)) { while (isalpha(reader) || isdigit(reader) || reader == '$') { lexeme += reader; reader = fin.get(); if (reader == ' ') break; } output(lexeme, "test alpha"); } else if (reader == '!') { lexeme += reader; reader = fin.get(); while (reader != '!') { reader = fin.get(); } lexeme += reader; output(lexeme, "test Separator"); } else if (reader == ' ' || reader == ' ') ; }
//close fstreams fin.close();
do { cout
+++++++++++++++++++++++++++++++++++++++++++++++
input text file:
! Find largest value between two numbers! int num1, num2, large$ if(num1 > num2) { large = num1$; } else { large = num2$; }
+++++++++++++++++++++++++++++++++++++++++++++++
test output:

! test Separator int test alpha num1 test alpha num2 test alpha large$ test alpha if test alpha num1 test alpha num2 test alpha large test alpha num1$ test alpha else test alpha large test alpha num2$ test alpha Press the Enter key to continue
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
