Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 939437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:47:26+00:00 2026-05-15T21:47:26+00:00

My issue is I am trying my first attempt at writing a very basic

  • 0

My issue is I am trying my first attempt at writing a very basic lexical analyzer for ascii text files. so far, it reads and compares to my token list properly, however I am unable to grab the final token without a space or pressing enter. I’ve tried using the delimiter ^Z ASCII 26 as another selection before comparing the string to my token list. This failed to work. I’ve also tried moving the f->eof() check to below the comparison location to see if it will snag it then check the eof flag. I’ve had no luck. could anyone possibly enlighten me? The code is below for the read method. m_TokenList is just a vector of type string.

void CelestialAnalyzer::ReadInTokens(ifstream *f){
 vector<string> statement;
 vector<string> tokens;
 string token;
 char c;
 do{
 f->get(c);   // Read in each character
if(f->eof())
 break;

if(c == '\n' || c == ' ' || c == '^Z' || c == '\r'){ // 26 ASCII ^Z (end of file marker)
 for(unsigned int i=0; i<m_TokenList.size(); i++){
  if(!token.compare(m_TokenList[i])){
  tokens.push_back(token);
  token.clear();
 }
}
} else {
 token.push_back(c); // Add it to the token array
}
} while (true);





f->close();

for(unsigned int i=0; i<tokens.size(); i++){
cout << "Found Token: " << tokens[i].c_str() << endl;
}

}

The m_TokenList is initialized as

CelestialAnalyzer::CelestialAnalyzer(){
m_TokenList.push_back("KEY");  // Prints data
m_TokenList.push_back("GETINPUT"); // Grabs user data
m_TokenList.push_back("+");   // Addition/Concation
m_TokenList.push_back("-");   // Subtraction
m_TokenList.push_back("==");  // Equator
m_TokenList.push_back("=");   // Assignment
m_TokenList.push_back(";");   // End statement
m_TokenList.push_back(" ");   // Blank
m_TokenList.push_back("{");   // Open Grouping
m_TokenList.push_back("}");   // Close Grouping
m_TokenList.push_back("(");   // Parameter opening
m_TokenList.push_back(")");   // Parameter closing
for(unsigned int i=48; i<=57; i++){
 string s; s.push_back((char)i);
 m_TokenList.push_back(s); s.clear();
}
}

A test file for reading is this simple example.
1 + 2 = KEY

It will register all but ‘KEY’ unless there is a space or a newline after it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:47:27+00:00Added an answer on May 15, 2026 at 9:47 pm

    Why don’t you just delete:

    if(f->eof())
    break;

    and use


    if(f->eof() || c == '\n' || c == ' ' || c == '^Z' || c == '\r'){

    then break afterwards? That way, when you hit EOF, you will add whatever remaining token you have.

    Alternately, you could just check if the token is nonempty after you break out of the loop, and add it in that case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.