1 //===--- LRTable.cpp - Parsing table for LR parsers --------------*- C++-*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang-pseudo/grammar/LRTable.h" 10 #include "clang-pseudo/grammar/Grammar.h" 11 #include "llvm/ADT/ArrayRef.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/Support/ErrorHandling.h" 14 #include "llvm/Support/FormatVariadic.h" 15 #include "llvm/Support/raw_ostream.h" 16 17 namespace clang { 18 namespace pseudo { 19 20 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const LRTable::Action &A) { 21 switch (A.kind()) { 22 case LRTable::Action::Shift: 23 return OS << llvm::formatv("shift state {0}", A.getShiftState()); 24 case LRTable::Action::Reduce: 25 return OS << llvm::formatv("reduce by rule {0}", A.getReduceRule()); 26 case LRTable::Action::GoTo: 27 return OS << llvm::formatv("go to state {0}", A.getGoToState()); 28 case LRTable::Action::Sentinel: 29 llvm_unreachable("unexpected Sentinel action kind!"); 30 } 31 llvm_unreachable("unexpected action kind!"); 32 } 33 34 std::string LRTable::dumpStatistics() const { 35 return llvm::formatv(R"( 36 Statistics of the LR parsing table: 37 number of states: {0} 38 number of actions: {1} 39 size of the table (bytes): {2} 40 )", 41 StateOffset.size() - 1, Actions.size(), bytes()) 42 .str(); 43 } 44 45 std::string LRTable::dumpForTests(const Grammar &G) const { 46 std::string Result; 47 llvm::raw_string_ostream OS(Result); 48 OS << "LRTable:\n"; 49 for (StateID S = 0; S < StateOffset.size() - 1; ++S) { 50 OS << llvm::formatv("State {0}\n", S); 51 for (uint16_t Terminal = 0; Terminal < NumTerminals; ++Terminal) { 52 SymbolID TokID = tokenSymbol(static_cast<tok::TokenKind>(Terminal)); 53 for (auto A : find(S, TokID)) { 54 if (A.kind() == LRTable::Action::Shift) 55 OS.indent(4) << llvm::formatv("'{0}': shift state {1}\n", 56 G.symbolName(TokID), A.getShiftState()); 57 else if (A.kind() == LRTable::Action::Reduce) 58 OS.indent(4) << llvm::formatv("'{0}': reduce by rule {1} '{2}'\n", 59 G.symbolName(TokID), A.getReduceRule(), 60 G.dumpRule(A.getReduceRule())); 61 } 62 } 63 for (SymbolID NontermID = 0; NontermID < G.table().Nonterminals.size(); 64 ++NontermID) { 65 if (find(S, NontermID).empty()) 66 continue; 67 OS.indent(4) << llvm::formatv("'{0}': go to state {1}\n", 68 G.symbolName(NontermID), 69 getGoToState(S, NontermID)); 70 } 71 } 72 return OS.str(); 73 } 74 75 llvm::Optional<LRTable::StateID> 76 LRTable::getShiftState(StateID State, SymbolID Terminal) const { 77 // FIXME: we spend a significant amount of time on misses here. 78 // We could consider storing a std::bitset for a cheaper test? 79 assert(pseudo::isToken(Terminal) && "expected terminal symbol!"); 80 for (const auto &Result : getActions(State, Terminal)) 81 if (Result.kind() == Action::Shift) 82 return Result.getShiftState(); // unique: no shift/shift conflicts. 83 return llvm::None; 84 } 85 86 llvm::ArrayRef<LRTable::Action> LRTable::getActions(StateID State, 87 SymbolID Terminal) const { 88 assert(pseudo::isToken(Terminal) && "expect terminal symbol!"); 89 return find(State, Terminal); 90 } 91 92 LRTable::StateID LRTable::getGoToState(StateID State, 93 SymbolID Nonterminal) const { 94 assert(pseudo::isNonterminal(Nonterminal) && "expected nonterminal symbol!"); 95 auto Result = find(State, Nonterminal); 96 assert(Result.size() == 1 && Result.front().kind() == Action::GoTo); 97 return Result.front().getGoToState(); 98 } 99 100 llvm::ArrayRef<LRTable::Action> LRTable::find(StateID Src, SymbolID ID) const { 101 assert(Src + 1u < StateOffset.size()); 102 std::pair<size_t, size_t> Range = 103 std::make_pair(StateOffset[Src], StateOffset[Src + 1]); 104 auto SymbolRange = llvm::makeArrayRef(Symbols.data() + Range.first, 105 Symbols.data() + Range.second); 106 107 assert(llvm::is_sorted(SymbolRange) && 108 "subrange of the Symbols should be sorted!"); 109 const LRTable::StateID *Start = 110 llvm::partition_point(SymbolRange, [&ID](SymbolID S) { return S < ID; }); 111 if (Start == SymbolRange.end()) 112 return {}; 113 const LRTable::StateID *End = Start; 114 while (End != SymbolRange.end() && *End == ID) 115 ++End; 116 return llvm::makeArrayRef(&Actions[Start - Symbols.data()], 117 /*length=*/End - Start); 118 } 119 120 LRTable::StateID LRTable::getStartState(SymbolID Target) const { 121 assert(llvm::is_sorted(StartStates) && "StartStates must be sorted!"); 122 auto It = llvm::partition_point( 123 StartStates, [Target](const std::pair<SymbolID, StateID> &X) { 124 return X.first < Target; 125 }); 126 assert(It != StartStates.end() && It->first == Target && 127 "target symbol doesn't have a start state!"); 128 return It->second; 129 } 130 131 } // namespace pseudo 132 } // namespace clang 133