Stack use for extracting characters

Posted on 2005-05-09
Last Modified: 2010-04-01
Hello, I'm trying to tap into the stack ability or C++ to build a procedure that would extract certain character from a file, place them in the stack, print them out and then verify that the right amout of characters are present. In a since, I'm trying to bulid a mini-compiler. I've bulit compilers in c++ before but I used a table based system to bulid tokens and find lexmes but I never used OO design to do it. To understand how to this I'm trying to use simpler example and extract the following characters:

//, /*, */, <, >, ', ", { , }, ( , ), [ , ]

and print out the stack. After that I want to try to do some syntax analysis by simple verifying for example that there is an open and closed brace, things like that.

Get get a better idea of how a stack works I made a simply program that would handle some integers and do some pushing and popping. I'm using a file called list.h and listnode.h to implement the popping an pushing so that works.

The problem that I am having is the following. Typically when I build a compiler I read in a whole line, then analysis every character in a buffer. I just want to push those certain character on the stack and ignore everything else in a file. I've haven't started thinking of how to do the systax part but I want to get this part done first.

This is what I have right now:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <sstream>

using std::endl;

#include "stack.h"  // Stack class definition

ifstream inFile;
ofstream outFile("out.txt", ios::out);

int main()
      cout << "Enter in a file name" << endl;
      cin >> fileName; ( fileName );
      if ( !inFile )
           cout << endl << "Error opening file: " << fileName << endl;
           cout << endl << "Program will be terminated" << endl;
           system ( "PAUSE" );

  while (! inFile.eof())
    inFile.getline (buffer,100);
      cout << buffer << endl;
   Stack< int > intStack;  // create Stack of ints

   cout << "processing an character Stack" << endl;

   // push integers onto intStack
   for ( int i = 0; i < 4; i++ ) {
      intStack.push( i );

   } // end for

   // pop integers from intStack
   char character;

   while ( !intStack.isStackEmpty() ) {
      intStack.pop( character );
      cout << character << " popped from stack" << endl;

   } // end while

   return 0;

} // end main

Where I process the for loop is where I want extract the character. Please help!!!
Question by:DancingFighterG
    LVL 23

    Expert Comment

    Keep in mind that //, /*, and */ are multi-character sequences,
    so you must either cheat, storing two chars in each int on your
    stack, or your integer values should be offsets into a list of tokens.

    Also keep in mind that end-of-line is the "closing" token for //.

    Author Comment

    Well, for simple reason I can will just have it where i will put every individual character on the screen so like:

    / * * / { / / / / ( ) { [ ] ( ) { [ ] ( ( [ ] ) ) } } }

    How would extract those individual characters?
    LVL 23

    Accepted Solution

    I would use strcspn() to locate the delimiters in your text.  As I mentioned,
    you will need to treat the comment blocks and quoted strings especially.  
    In general, use strcspn(buffer, "/\"{}'[]()").  
        If it locates /*, push it and scan forward looking only for */.  
        If the initial scan locates //, scan forward looking only for newline.  
        If the scan locates ' or ", scan forward looking only for the closing quote character


    Author Comment

    I read that strcspn() determines the length of the initial part of the string in its first argument that does not contain any characters from the stirng in its second argument. The functions return the length of the segment. Do you mean something else.

    Author Comment

    Ok, could I do something like thi:

    string Lexeme;
    string current_charater;
    char bufferA127];

    string character [10] = {"{", "<", ">", "/", "8", " ' ", "(", ")", \" }
        for(int i = 0; i < 10 i++)
           if(character [i] == Lexeme)

    void readInput()
       bufferA[0] = inFile.peek();
          bufferA[0] = '\0';
       current_character = bufferA[0];

    How would I scan ahead to see if there is a opposing symbol. Would I compare that on the stake.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now