GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Mar-2004, 13:54
CronoX CronoX is offline
New Member
 
Join Date: Mar 2004
Posts: 26
CronoX is on a distinguished road

Issue with stacks


Hi if you have seen my other thread up about File I/O you can tell I'm having issues with C++ stuff. To ease confusion and to keep two problems seperate, I'm trying to create a stack for a parser that will print out a parse table. What im trying to do is giving some string I need to call different functions that associate with the string and use those to print out the table. For example:

Given a token from a file

Prog

I would call a function <program> associated with this token and use it.

prog has a predifined setup of

<program> -><program heading> .

to follow for my function definition of <program> so in short the <program> function first be pushed onto the stack.
Then I would indent for the tree so i was planning on having a variable to show indentation and whenever something is pushed onto the stack I add 2 or 3 to the variable for the indentation and whenever I pop something off it I subtract 2 or 3.
would call the <program heading> function which would then push <program heading> onto my stack

In this function I have another function called <program heading> which pushes <program heading> onto the stack which calls other tokens already defind within it. Then I would check the current token and see if its a PERIOD if so i exit. So in the end the tree would look something like

<program>
<program heading>
PERIOD

(PERIOD is for the period at the end of the rule.)

My problem is the only way i can figure to do this is to create a big stack that would go into each function so that when it sees PROG it goes into the program function which pushes <program> on the stack. Then it will look in program and check the current token. If the token is the next one needed in the rule it goes on otherwise gives a error. What I can't figure out is how to create a stack to do that. Would I make a function just for the stack and implment it into each rule function or would I have to do something else all together.


Sorry for the long post and thanks for any help provided.
  #2  
Old 09-Mar-2004, 17:06
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Crono.

I would definitely use a stack implementation and let it take care of the storage, so you could just basically push and pop as needed. I am almost certain that C++ has a stack implementation. I have written a stack implementation that is pretty small if you can't find anything else.
  #3  
Old 09-Mar-2004, 17:11
CronoX CronoX is offline
New Member
 
Join Date: Mar 2004
Posts: 26
CronoX is on a distinguished road
Well most of what I have found to do this has push and pop but it also has a bunch of things that I don't think I would need plus they want me to create a large amount of classes so if you have anything better than that it would be great to take a look at.
  #4  
Old 09-Mar-2004, 17:18
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by CronoX
Well most of what I have found to do this has push and pop but it also has a bunch of things that I don't think I would need plus they want me to create a large amount of classes so if you have anything better than that it would be great to take a look at.

I will peruse throught my files and see what I've got. I will post something a little later tonight that may or may not help you.
  #5  
Old 09-Mar-2004, 18:57
CronoX CronoX is offline
New Member
 
Join Date: Mar 2004
Posts: 26
CronoX is on a distinguished road
Just so I know dsmith was the linked list you showed me the way to go about doing a Stack in C++ and if so is it as simple as if i wanna push or pop something off the stack and add two or three
would i do

[c]
stack.Push("thing_to_push"); // Here I want to push output that will be displayed
space + 2; // Im using this to indent by 2 places


stack.Pop("thing_to_pop"); //Here I want to pop the output that i pushed
space + 2; //this is to end indentation.

Thanks
  #6  
Old 09-Mar-2004, 19:15
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
No, that was a linked list representation. I think I found a stack that I did. It is not nearly as good. Here is the class definition of my stack
CPP / C++ / C Code:
struct data{
	char *string;
	struct data	*back;
};


class estack{
	
    struct data	*current;

	public:
   	estack();
	~estack();
   	char *pop();
	void push(char *err_string);
	void apush(char *format,...);
};

It is written only to hold a char* (string). If you need something different you will have to rewrite it.
Give me a bit and I will post the whole thing.
  #7  
Old 09-Mar-2004, 19:47
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Okay, I have posted my code with a sample for its use here. A couple of things about this. First it was written to handle error strings thus the comments as well as the functions apush. This will push a printf type string, such as:
CPP / C++ / C Code:
mystack.apush("#%d: %s",error_number,error_string);

Also as a bonus, I wrote a queue implementation in the same file. Neither of these are big and if you don't want it hanging around, just delete the declaration and member functions.

Hope this is of some use to you,
d
  #8  
Old 10-Mar-2004, 09:58
CronoX CronoX is offline
New Member
 
Join Date: Mar 2004
Posts: 26
CronoX is on a distinguished road
Hey dsmith I wanted to thank you for the stack implmentation but I was having so many problems trying to make it work for me I thought about using another method. I was thinking that if i had a rule like
<program> -><program heading>

that i use a string called spaces to do my indentation and a function with the name like getStringOfSpaces or something like that to work with.



So by thats if i had the above rule my code would be something like

CPP / C++ / C Code:

Program(...)
{
 cout << getStringOfSpaces << "<program>";
 spaces + 3;  // to indent

ProgHead(); //This would call the program heading function which would take care of printing its name and spaces out. 
}


The only problem I see and was wondering if you or anyone reading could pass along any help with is how to work with the getStringOfSpaces function I was thinking of something like
CPP / C++ / C Code:
getStringOfSpaces(string spaces)
{
  num = 1;

  for(i=0; i< spaces; i++)
  {
    num += 1;

  }

return num;
}

I'm not sure if I'm handling num the right way for what I wanna do which is to be able to handle the spaces string the way I handled it for the first function. So If you see a better way to go about it or think thats fine I would greatly appreciate it if you let me know what ya think.
  #9  
Old 10-Mar-2004, 13:49
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
I think I know what you are wanting, but your function isn't right and as is doesn't do much at all...
CPP / C++ / C Code:
char* getStringOfSpaces(int spaces)
{
  char* string = new char(spaces);
  int i;

  for(i=0;i<spaces;i++)
    *(string+i) = ' ';
  *(string+i) = 0;     //Null terminate string

  return string;
}

This will give you a string containing "spaces" number of spaces. Is this what you are looking for?
  #10  
Old 10-Mar-2004, 13:53
CronoX CronoX is offline
New Member
 
Join Date: Mar 2004
Posts: 26
CronoX is on a distinguished road
yea i was trying to get something around those lines I also tried this

CPP / C++ / C Code:

// Function for the getSpaces

string getSpaces(char spaces)
{
	// Prints out the space
	string spstr = " ";

	for (int i=0; i<spaces; i++)
	{
		spstr += " ";
	}

	return spstr; 
	


}

But i wasnt sure if it was gonna work out the way I wantd it too.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP/Apache memory usage issue bacchus Apache Web Server Forum 0 18-Aug-2003 12:57
Possible issue with HTML BBCode? JdS GIDForums™ 2 16-Aug-2003 11:14
setiosflags issue dcj1978 C++ Forum 1 08-Aug-2003 05:16
Php bbcode issue Caged MySQL / PHP Forum 3 06-Aug-2003 18:55
Loading issue jrobbio Websites Reviewed Forum 4 15-Jan-2003 05:36

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:59.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.