• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Stack use for extracting characters

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;
      inFile.open ( 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!!!
  • 3
  • 2
1 Solution
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 //.
DancingFighterGAuthor Commented:
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?
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

DancingFighterGAuthor Commented:
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.
DancingFighterGAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now