//===--- LRTable.h - Define LR Parsing Table ---------------------*- C++-*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // The LRTable (referred as LR parsing table in the LR literature) is the core // component in LR parsers, it drives the LR parsers by specifying an action to // take given the current state on the top of the stack and the current // lookahead token. // // The LRTable can be described as a matrix where the rows represent // the states of the LR graph, the columns represent the symbols of the // grammar, and each entry of the matrix (called action) represents a // state transition in the graph. // // Typically, based on the category of the grammar symbol, the LRTable is // broken into two logically separate tables: // - ACTION table with terminals as columns -- e.g. ACTION[S, a] specifies // next action (shift/reduce) on state S under a lookahead terminal a // - GOTO table with nonterminals as columns -- e.g. GOTO[S, X] specifies // the state which we transist to from the state S with the nonterminal X // // LRTable is *performance-critial* as it is consulted frequently during a // parse. In general, LRTable is very sparse (most of the entries are empty). // For example, for the C++ language, the SLR table has ~1500 states and 650 // symbols which results in a matrix having 975K entries, ~90% of entries are // empty. // // This file implements a speed-and-space-efficient LRTable. // //===----------------------------------------------------------------------===// #ifndef CLANG_PSEUDO_GRAMMAR_LRTABLE_H #define CLANG_PSEUDO_GRAMMAR_LRTABLE_H #include "clang-pseudo/grammar/Grammar.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/BitVector.h" #include "llvm/ADT/SmallSet.h" #include "llvm/Support/Capacity.h" #include "llvm/Support/MathExtras.h" #include <cstdint> #include <vector> namespace clang { namespace pseudo { // Represents the LR parsing table, which can efficiently the question "what is // the next step given the lookahead token and current state on top of the // stack?". // // This is a dense implementation, which only takes an amount of space that is // proportional to the number of non-empty entries in the table. // // Unlike the typical LR parsing table which allows at most one available action // per entry, conflicted actions are allowed in LRTable. The LRTable is designed // to be used in nondeterministic LR parsers (e.g. GLR). // // There are no "accept" actions in the LRTable, instead the stack is inspected // after parsing completes: is the state goto(StartState, StartSymbol)? class LRTable { … }; } // namespace pseudo } // namespace clang #endif // CLANG_PSEUDO_GRAMMAR_LRTABLE_H