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