Graph-structured stack
This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (January 2022) |
In computer science, a graph-structured stack (GSS) is a directed acyclic graph where each directed path represents a stack. The graph-structured stack is an essential part of Tomita's algorithm, where it replaces the usual stack of a pushdown automaton. This allows the algorithm to encode the nondeterministic choices in parsing an 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
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.
