Previous: Maintaining the stack, Up: Maintaining the stack [Contents][Index]
In our discussion of the stack handler for the valuator, we will treat the stack as a 0-based array. If an implementation applies Libmarpa’s step instructions literally, using a physical stack, it must make sure that all locations in the stack are initialized. The range of locations that must be initialized is from stack location 0 to the “end of stack” location. The result of the parse tree is always stored in stack location 0, so that a stack location 0 is always needed. Therefore, the end of stack location is always a specified value, and greater than or equal to 0. The end of stack location must also be greater than or equal to
marpa_v_result(v)
for every MARPA_STEP_TOKEN
step,
marpa_v_result(v)
for every MARPA_STEP_NULLING_SYMBOL
step, and
marpa_v_arg_n(v)
for every MARPA_STEP_RULE
step.
In practice, an application will often extend the stack as it iterates through the steps, initializing the new stack locations as they are created.
Note that our requirement is not merely that the stack locations exist and be writable, but that they be initialized. This is necessary for C89 conformance. Because of write optimizations in our implementation, the first access to any stack location may be a read. C89 allows trap values, so that a read of an uninitialized location could result in undefined behavior. See Trap representations.