• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • 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" );
           exit(1);
      }

  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 );
      intStack.printStack();

   } // end for

   // pop integers from intStack
   char character;

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

   } // end while

   return 0;

} // end main

Where I process the for loop is where I want extract the character. Please help!!!
0
DancingFighterG
Asked:
DancingFighterG
  • 3
  • 2
1 Solution
 
brettmjohnsonCommented:
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 //.
0
 
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?
0
 
brettmjohnsonCommented:
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
   



      
0
 
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.
0
 
DancingFighterGAuthor Commented:
Ok, could I do something like thi:

string Lexeme;
string current_charater;
char bufferA127];

string character [10] = {"{", "<", ">", "/", "8", " ' ", "(", ")", \" }
readInput()
Lexeme.append(current_character);
while(inFile.eof())
{
    for(int i = 0; i < 10 i++)
    {
       if(character [i] == Lexeme)
       {
         charStack.push(Lexeme)
         charStack.printStack();
       else
        Lexeme.erase();
       }

void readInput()
{
   bufferA[0] = inFile.peek();
   if(inFile.eof())
   {
      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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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