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/LRTable.h" 10 #include "clang-pseudo/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::Accept: 29 return OS << "acc"; 30 case LRTable::Action::Sentinel: 31 llvm_unreachable("unexpected Sentinel action kind!"); 32 } 33 llvm_unreachable("unexpected action kind!"); 34 } 35 36 std::string LRTable::dumpStatistics() const { 37 return llvm::formatv(R"( 38 Statistics of the LR parsing table: 39 number of states: {0} 40 number of actions: {1} 41 size of the table (bytes): {2} 42 )", 43 StateOffset.size() - 1, Actions.size(), bytes()) 44 .str(); 45 } 46 47 std::string LRTable::dumpForTests(const Grammar &G) const { 48 std::string Result; 49 llvm::raw_string_ostream OS(Result); 50 OS << "LRTable:\n"; 51 for (StateID S = 0; S < StateOffset.size() - 1; ++S) { 52 OS << llvm::formatv("State {0}\n", S); 53 for (uint16_t Terminal = 0; Terminal < NumTerminals; ++Terminal) { 54 SymbolID TokID = tokenSymbol(static_cast<tok::TokenKind>(Terminal)); 55 for (auto A : find(S, TokID)) { 56 if (A.kind() == LRTable::Action::Shift) 57 OS.indent(4) << llvm::formatv("'{0}': shift state {1}\n", 58 G.symbolName(TokID), A.getShiftState()); 59 else if (A.kind() == LRTable::Action::Reduce) 60 OS.indent(4) << llvm::formatv("'{0}': reduce by rule {1} '{2}'\n", 61 G.symbolName(TokID), A.getReduceRule(), 62 G.dumpRule(A.getReduceRule())); 63 else if (A.kind() == LRTable::Action::Accept) 64 OS.indent(4) << llvm::formatv("'{0}': accept\n", G.symbolName(TokID)); 65 } 66 } 67 for (SymbolID NontermID = 0; NontermID < G.table().Nonterminals.size(); 68 ++NontermID) { 69 if (find(S, NontermID).empty()) 70 continue; 71 OS.indent(4) << llvm::formatv("'{0}': go to state {1}\n", 72 G.symbolName(NontermID), 73 getGoToState(S, NontermID)); 74 } 75 } 76 return OS.str(); 77 } 78 79 llvm::ArrayRef<LRTable::Action> LRTable::getActions(StateID State, 80 SymbolID Terminal) const { 81 assert(pseudo::isToken(Terminal) && "expect terminal symbol!"); 82 return find(State, Terminal); 83 } 84 85 LRTable::StateID LRTable::getGoToState(StateID State, 86 SymbolID Nonterminal) const { 87 assert(pseudo::isNonterminal(Nonterminal) && "expected nonterminal symbol!"); 88 auto Result = find(State, Nonterminal); 89 assert(Result.size() == 1 && Result.front().kind() == Action::GoTo); 90 return Result.front().getGoToState(); 91 } 92 93 llvm::ArrayRef<LRTable::Action> LRTable::find(StateID Src, SymbolID ID) const { 94 assert(Src + 1 < StateOffset.size()); 95 std::pair<size_t, size_t> Range = 96 std::make_pair(StateOffset[Src], StateOffset[Src + 1]); 97 auto SymbolRange = llvm::makeArrayRef(Symbols.data() + Range.first, 98 Symbols.data() + Range.second); 99 100 assert(llvm::is_sorted(SymbolRange) && 101 "subrange of the Symbols should be sorted!"); 102 const LRTable::StateID *Start = 103 llvm::partition_point(SymbolRange, [&ID](SymbolID S) { return S < ID; }); 104 if (Start == SymbolRange.end()) 105 return {}; 106 const LRTable::StateID *End = Start; 107 while (End != SymbolRange.end() && *End == ID) 108 ++End; 109 return llvm::makeArrayRef(&Actions[Start - Symbols.data()], 110 /*length=*/End - Start); 111 } 112 113 LRTable::StateID LRTable::getStartState(SymbolID Target) const { 114 assert(llvm::is_sorted(StartStates) && "StartStates must be sorted!"); 115 auto It = llvm::partition_point( 116 StartStates, [Target](const std::pair<SymbolID, StateID> &X) { 117 return X.first < Target; 118 }); 119 assert(It != StartStates.end() && It->first == Target && 120 "target symbol doesn't have a start state!"); 121 return It->second; 122 } 123 124 } // namespace pseudo 125 } // namespace clang 126