Because anything from C++ and beyond have nice constructs that handles a “stack” like behavior(vectors, lists, array lists, etc), I want to keep the question in terms of C.
So, I am learning about compilers, stacks, inscript and postscript, parsers, lexers, and everything else that involves putting a compiler together as I will be required to later on in the my course for this semester. I want to make sure that my understanding of stacks is solid.
I believe I have the general idea on how one is put together. Basically you have an array following LIFO model. When you add an element, it is “pushed” in on the end of the array and “popped” off when removed. No issue there. My concern comes whith some the implementation detail. How are things like number of elements tracked? How large should a stack be initialized to? Should I use a structure to hold those details together? Are variables global?
My professor has already handed us some source code in relation to our assignment, I am just looking for some supplemental details for my own understanding (generally I have to put something together myself before getting an understanding of someone else’s code). In most of my programming experience, the stack was some magical place that was beyond my control, but would explode if my recursive functions got too deep (SO anyone?).
Thanks in advance.
EDIT:
Ok, one thing I think I need to clarify is that I am not making a “true” stack, but will be required to take a an infix epxression and convert it to postfix. I am looking at the stack data structure so anything architecure related may not apply in this since. I was given the impression a stack was the way to handle this, likewise with rule reduction described in BNF notation.
What kind of stack are you talking about, here?
Most CPU architectures have a notion of a stack, which is how the CPU tracks function calls, basically. C is typically compiled to use the same stack for this purpose, and also often to use it for argument-passing.
Such “architectural” stacks don’t require much, typically there’s a register in the CPU that points to the top of the stack. It’s the job of the operating system to make sure that every time a process starts, that process has a valid stack pointer. This is done by allocating the memory and initializing the register to the proper address. Modern “large” OS:es with virtual memory and so on often support growing the stack dynamically on demand.
The number of elements in the stack is not explicitly tracked in such cases, you can compute the number of bytes used by inspecting the current stack pointer and comparing to the process’ (assumed known) “stack start”, but that doesn’t tell you about the internal structure of those bytes. This is normal for such low-level constructs.
If, as nos hints in a comment, you’re really talking about a stack data structure, then there is no standard implementation for such a thing in C. It’s possible to imagine many possible API:s, for instance:
The above API leaves the initial depth and whether or not the stack should grow once you try to go beyond the limit up to the user. This is sensible for a general-purpose API, in my opinion.