![]() |
|
#1
|
|||
|
|||
Issue with stacksHi 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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Quote:
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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:
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
|
||||
|
||||
|
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:
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
|
|||
|
|||
|
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:
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:
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
|
||||
|
||||
|
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:
This will give you a string containing "spaces" number of spaces. Is this what you are looking for? |
|
#10
|
|||
|
|||
|
yea i was trying to get something around those lines I also tried this
CPP / C++ / C Code:
But i wasnt sure if it was gonna work out the way I wantd it too. |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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