# Graph-structured stack

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Graph-structured_stack
> Markdown URL: https://mediated.wiki/source/Graph-structured_stack.md
> Source: https://en.wikipedia.org/wiki/Graph-structured_stack
> Source revision: 1076467531
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. Please help improve this article by introducing more precise citations. (January 2022) (Learn how and when to remove this message)

In [computer science](/source/Computer_science), a **graph-structured stack** (GSS) is a [directed acyclic graph](/source/Directed_acyclic_graph) where each directed [path](/source/Path_(graph_theory)) represents a [stack](/source/Stack_(data_structure)). The graph-structured stack is an essential part of [Tomita's algorithm](/source/GLR_parser), where it replaces the usual [stack](/source/Stack_(data_structure)) of a [pushdown automaton](/source/Pushdown_automaton). This allows the algorithm to encode the nondeterministic choices in parsing an [ambiguous grammar](/source/Ambiguous_grammar), sometimes with greater efficiency.

In the following diagram, there are four stacks: {7,3,1,0}, {7,4,1,0}, {7,5,2,0}, and {8,6,2,0}.

Another way to simulate nondeterminism would be to duplicate the stack as needed. The duplication would be less efficient since vertices would not be shared. For this example, 16 vertices would be needed instead of 9.

## Operations

GSSnode* GSS::add(GSSnode* prev, int elem)
{
	int prevlevel = prev->level;
	assert(levels.size() >= prevlevel + 1);
	int level = prevlevel + 1;
	if (levels.size() == level)
	{
		levels.resize(level + 1);
	}
	GSSnode* node = findElemAtLevel(level, elem);
	if (node == nullptr)
	{
		node = new GSSnode();
		node->elem = elem;
		node->level = level;
		levels[level].push_back(node);
	}
	node->add(prev);
	return node;
}

void GSS::remove(GSSnode* node)
{
	if (levels.size() > node->level + 1)
		if (findPrevAtLevel(node->level + 1, node)) throw Exception("Can remove only from top.");
	for (int i = 0; i < levels[node->level].size(); i++)
		if (levels[node->level][i] == node)
		{
			levels[node->level].erase(levels[node->level].begin() + i);
			break;
		}
	delete node;
}

## References

- Masaru Tomita. *Graph-Structured Stack And Natural Language Parsing*. Annual Meeting of the Association of Computational Linguistics, 1988. [\[1\]](http://www.aclweb.org/anthology/P88-1031)

- Elizabeth Scott, Adrian Johnstone *GLL Parsing* [gll.pdf](http://dotat.at/tmp/gll.pdf)

This computer science article is a stub. You can help Wikipedia by adding missing information.

- [v](https://en.wikipedia.org/wiki/Template:Comp-sci-stub)
- [t](/source/Template_talk%3AComp-sci-stub)
- [e](https://en.wikipedia.org/wiki/Special:EditPage/Template:Comp-sci-stub)

---
Adapted from the Wikipedia article [Graph-structured stack](https://en.wikipedia.org/wiki/Graph-structured_stack) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Graph-structured_stack?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
