Last active
January 17, 2025 06:44
-
-
Save Andersama/f69e781db2e4413676b52518ee864baa to your computer and use it in GitHub Desktop.
Generates a dfa designed to accept a list of keywords
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <vector> | |
| #include <array> | |
| #include <string> | |
| #include <string_view> | |
| #include <cstdint> | |
| #include <algorithm> | |
| #include <format> | |
| #include <iostream> | |
| namespace dfa { | |
| template<typename unsigned_type, typename unsigned_alphabet_type = unsigned_type> | |
| struct keyword_tree { | |
| uint32_t states; | |
| uint32_t inputs; | |
| std::array<unsigned_alphabet_type, (1 << (sizeof(unsigned_type) * 8))> alphabet; | |
| std::vector<unsigned_type> transitions; | |
| std::vector<uint8_t> final_states; | |
| }; | |
| template<size_t N> | |
| constexpr size_t keyword_tree_max_states(std::array<std::string_view, N> views) { | |
| size_t states = 0; | |
| for (size_t i = 0; i < views.size(); i++) { | |
| states += views[i].size(); | |
| } | |
| return states; | |
| } | |
| template <size_t N> | |
| constexpr size_t keyword_tree_max_nonfinal_states(std::array<std::string_view, N> views) { | |
| size_t non_final_states = 0; | |
| for (size_t i = 0; i < views.size(); i++) { | |
| non_final_states += views[i].size() > 0 ? (views[i].size()-1) : 0; | |
| } | |
| return non_final_states; | |
| } | |
| template <size_t N> constexpr size_t keyword_tree_min_nonfinal_states(std::array<std::string_view, N> views) { | |
| std::sort(views.begin(), views.end(), [](std::string_view lhs, std::string_view rhs) { | |
| //-1 lt, 0 ==, 1 gt | |
| int cmp = std::memcmp(lhs.data(), rhs.data(), std::min(lhs.size(), rhs.size())); | |
| // unlike the usual compare function here we flip the order on size, largest first | |
| // the end result is that we get very similar strings in order, largest -> smallest | |
| if (cmp != 0) | |
| return cmp < 0; | |
| else | |
| return lhs.size() > rhs.size(); | |
| }); | |
| size_t non_final_states = 0; | |
| for (size_t i = 0; i < views.size(); i++) { | |
| uint8_t is_different = 0; | |
| for (size_t c = 0; c < views[i].size(); c++) { | |
| if (i > 0) { | |
| if (is_different) { | |
| // if we've detected the string is different add new states | |
| non_final_states++; | |
| } else if (c < views[i - 1].size() && views[i][c] != views[i - 1][c]) { | |
| // different transition from current state*, not a new state | |
| is_different = 1; | |
| } else if (c >= views[i - 1].size()) { | |
| // different transition from current state*, not a new state | |
| is_different = 1; | |
| } | |
| } else { | |
| non_final_states++; | |
| } | |
| } | |
| } | |
| return non_final_states; | |
| } | |
| template <typename unsigned_type, size_t N> | |
| constexpr std::pair<std::array<unsigned_type, N>, size_t> keyword_tree_alphabet(std::array<std::string_view, N> views) { | |
| std::array<unsigned_type, N> alphabet{}; | |
| for (size_t i = 0; i < views.size(); i++) { | |
| for (size_t c = 0; c < views[i].size(); c++) { | |
| alphabet[views[i][c]] = 1; | |
| } | |
| } | |
| //find first non-zero input | |
| size_t i = 0; | |
| for (; i < alphabet.size(); i++) { | |
| if (alphabet[i]) | |
| break; | |
| } | |
| //start renumbering | |
| size_t input = 0; | |
| for (; i < alphabet.size(); i++) { | |
| input = (alphabet[i]) ? input + 1 : input; | |
| alphabet[i] = (alphabet[i]) ? input : alphabet[i]; | |
| } | |
| //our renumbered inputs + 1 == number of actual inputs (have to include 0 input) | |
| input + 1; | |
| return std::make_pair(alphabet,input); | |
| } | |
| template<typename unsigned_type, size_t N> | |
| keyword_tree<unsigned_type> construct_keyword_tree(std::array<std::string_view, N> views) { | |
| //while a decent sort we can better approximate the end state size w/ a modification | |
| /* | |
| std::sort(views.begin(), views.end(), [](std::string_view lhs, std::string_view rhs) { | |
| return lhs < rhs; | |
| }); | |
| */ | |
| //here we do a lexigraphical sort with a twist | |
| std::sort(views.begin(), views.end(), [](std::string_view lhs, std::string_view rhs) { | |
| //-1 lt, 0 ==, 1 gt | |
| int cmp = std::memcmp(lhs.data(), rhs.data(), std::min(lhs.size(), rhs.size())); | |
| // unlike the usual compare function here we flip the order on size, largest first | |
| // the end result is that we get very similar strings in order, largest -> smallest | |
| if (cmp != 0) | |
| return cmp < 0; | |
| else | |
| return lhs.size() > rhs.size(); | |
| }); | |
| keyword_tree<unsigned_type> kw_tree; | |
| for (size_t i = 0; i < kw_tree.alphabet.size(); i++) { | |
| kw_tree.alphabet[i] = 0; | |
| } | |
| kw_tree.states = 0; | |
| kw_tree.inputs = 0; | |
| size_t complete_state_size = 0; | |
| for (size_t i = 0; i < views.size(); i++) { | |
| complete_state_size += views[i].size(); | |
| for (size_t c = 0; c < views[i].size(); c++) { | |
| kw_tree.inputs += (kw_tree.alphabet[views[i][c]] == 0); | |
| kw_tree.alphabet[views[i][c]] = 1; | |
| } | |
| uint8_t is_different = 0; | |
| for (size_t c = 0; c < views[i].size(); c++) { | |
| if (i > 0) { | |
| if (is_different) { | |
| //if we've detected the string is different add new states | |
| kw_tree.states++; | |
| } else if (c < views[i - 1].size() && views[i][c] != views[i - 1][c]) { | |
| // different transition from current state*, not a new state | |
| is_different = 1; | |
| } else if (c >= views[i - 1].size()) { | |
| // different transition from current state*, not a new state | |
| is_different = 1; | |
| } | |
| } else { | |
| kw_tree.states++; | |
| } | |
| } | |
| } | |
| if (kw_tree.states > ((1 << sizeof(unsigned_type) * 8) - 1)) { | |
| //absurd number indicates error | |
| kw_tree.inputs = ~uint32_t{0}; | |
| return kw_tree; | |
| } | |
| //check if we've got enough inputs to consider the alphabet filled | |
| if (kw_tree.inputs == kw_tree.alphabet.size()) { | |
| //no need to map inputs, make an identity array | |
| for (size_t i = 0; i < kw_tree.alphabet.size(); i++) { | |
| kw_tree.alphabet[i] = i; | |
| } | |
| } else { | |
| //otherwise remap inputs in order | |
| size_t input = 0; | |
| for (size_t i = 0; i < kw_tree.alphabet.size(); i++) { | |
| input = (kw_tree.alphabet[i]) ? input+1: input; | |
| kw_tree.alphabet[i] = (kw_tree.alphabet[i]) ? input : kw_tree.alphabet[i]; | |
| } | |
| kw_tree.inputs = input+1; | |
| } | |
| uint32_t count = (kw_tree.inputs * kw_tree.states); | |
| kw_tree.transitions.reserve(count); | |
| for (size_t t = 0; t < count; t++) { | |
| kw_tree.transitions.emplace_back(0); | |
| } | |
| uint32_t total_states = kw_tree.states + views.size(); | |
| kw_tree.final_states.reserve(total_states); | |
| kw_tree.final_states.clear(); | |
| for (size_t f = 0; f < total_states; f++) { | |
| kw_tree.final_states.emplace_back(0); | |
| } | |
| uint8_t n_state = 1; | |
| for (size_t v = 0; v < views.size(); v++) { | |
| uint8_t state = 0; | |
| size_t n = 0; | |
| //intermediate states | |
| std::string_view vw = views[v]; | |
| for (; (n+1) < vw.size(); n++) { | |
| uint8_t alpha_c = kw_tree.alphabet[(uint8_t)vw[n]]; | |
| //if we've not been down this path | |
| if (kw_tree.transitions[((uint32_t)state * kw_tree.inputs) + alpha_c] == 0) { | |
| //fill in / do the state transition | |
| kw_tree.transitions[((uint32_t)state * kw_tree.inputs) + alpha_c] = n_state; | |
| state = n_state; | |
| n_state++; | |
| } else { | |
| //do the state transition | |
| state = kw_tree.transitions[((uint32_t)state * kw_tree.inputs) + alpha_c]; | |
| } | |
| } | |
| //end state (jump off end of table) | |
| if (n < vw.size()) { | |
| uint8_t alpha_c = kw_tree.alphabet[(uint8_t)vw[n]]; | |
| if (kw_tree.transitions[((uint32_t)state * kw_tree.inputs) + alpha_c] == 0) { | |
| //fill in last state transition | |
| kw_tree.transitions[((uint32_t)state * kw_tree.inputs) + alpha_c] = (kw_tree.states + v); | |
| //mark state as final | |
| kw_tree.final_states[kw_tree.states + v] = true; | |
| } else { | |
| //mark state as final | |
| uint32_t final_state = kw_tree.transitions[((uint32_t)state * kw_tree.inputs) + alpha_c]; | |
| kw_tree.final_states[final_state] = true; | |
| } | |
| } | |
| } | |
| return kw_tree; | |
| } | |
| template<typename array_type, size_t N> | |
| std::string kw_array_to_cpp(const std::array<array_type, N> &arr, std::string_view name, bool make_constexpr) { | |
| std::string buf; | |
| std::back_insert_iterator<std::string> str_back = std::back_inserter(buf); | |
| if (make_constexpr) | |
| std::format_to(str_back, "constexpr "); | |
| std::format_to(str_back, "std::array<{}, {}> {} = [](){}\n", typeid(array_type).name(), | |
| arr.size(), name, '{'); | |
| std::format_to(str_back, "\tstd::array<{}, {}> lamda_table{};\n", typeid(array_type).name(), | |
| arr.size(), | |
| "{}"); | |
| for (size_t i = 0; i < arr.size(); i++) { | |
| std::format_to(str_back, "\tlamda_table[{}] = {};\n", i, arr[i]); | |
| } | |
| std::format_to(str_back, "\treturn lamda_table;\n"); | |
| std::format_to(str_back, "{}();\n", '}'); | |
| return buf; | |
| } | |
| template <typename vector_type> | |
| std::string kw_array_to_cpp(const std::vector<vector_type> &arr, std::string_view name, bool make_constexpr) { | |
| std::string buf; | |
| std::back_insert_iterator<std::string> str_back = std::back_inserter(buf); | |
| if (make_constexpr) | |
| std::format_to(str_back, "constexpr "); | |
| std::format_to(str_back, "std::array<{}, {}> {} = [](){}\n", typeid(vector_type).name(), | |
| arr.size(), name, '{'); | |
| std::format_to(str_back, "\tstd::array<{}, {}> lamda_table{};\n", typeid(vector_type).name(), | |
| arr.size(), "{}"); | |
| for (size_t i = 0; i < arr.size(); i++) { | |
| std::format_to(str_back, "\tlamda_table[{}] = {};\n", i, arr[i]); | |
| } | |
| std::format_to(str_back, "\treturn lamda_table;\n"); | |
| std::format_to(str_back, "{}();\n", '}'); | |
| return buf; | |
| } | |
| template <typename unsigned_type, typename unsigned_alphabet_type> | |
| std::string kw_tree_to_cpp(const keyword_tree<unsigned_type, unsigned_alphabet_type> &kw_tree, std::string_view name_space, bool make_constexpr) { | |
| std::string buf; | |
| std::back_insert_iterator<std::string> str_back = std::back_inserter(buf); | |
| std::format_to(str_back, | |
| "#pragma once\n" | |
| "#include <array>;\n" | |
| "\n"); | |
| std::format_to(str_back, "namespace {}{}\n", name_space, '{'); | |
| bool uses_alphabet_table = false; | |
| for (size_t i = 0; i < kw_tree.alphabet.size(); i++) { | |
| if (kw_tree.alphabet[i] != i) { | |
| uses_alphabet_table = true; | |
| break; | |
| } | |
| } | |
| if (make_constexpr) | |
| std::format_to(str_back, "constexpr "); | |
| std::format_to(str_back, "bool uses_alphabet = {};\n", uses_alphabet_table); | |
| if (make_constexpr) | |
| std::format_to(str_back, "constexpr "); | |
| std::format_to(str_back, "size_t states = {};\n", kw_tree.states); | |
| if (make_constexpr) | |
| std::format_to(str_back, "constexpr "); | |
| std::format_to(str_back, "size_t inputs = {};\n", kw_tree.inputs); | |
| if (make_constexpr) | |
| std::format_to(str_back, "constexpr "); | |
| std::format_to(str_back, "std::array<{}, {}> transition_table = [](){}\n", typeid(unsigned_type).name(), kw_tree.transitions.size(), '{'); | |
| std::format_to(str_back, "\tstd::array<{}, {}> lamda_table{};\n", typeid(unsigned_type).name(), kw_tree.transitions.size(), "{}"); | |
| std::array<std::array<char, 257>, 256> inv_alphabet{}; | |
| std::array<size_t, 256> inv_widths{}; | |
| for (size_t from = 0; from < 256; from++) { | |
| size_t to = kw_tree.alphabet[from]; | |
| inv_alphabet[to][inv_widths[to]] = from; | |
| //only add printable chrs | |
| inv_widths[to] += (from >= ' ' && from < 127); | |
| } | |
| for (size_t i = 0; i < kw_tree.states; i++) { | |
| std::format_to(str_back, "//state: {}\n", i); | |
| for (size_t c = 0; c < kw_tree.inputs; c++) { | |
| size_t index = (i * kw_tree.inputs) + c; | |
| std::string_view inv_view = std::string_view{inv_alphabet[c].data(), inv_widths[c]}; | |
| std::format_to(str_back, "\tlamda_table[{}] = {}; //transition {} \"{}\"\n", index, | |
| kw_tree.transitions[index], c, inv_view); | |
| } | |
| } | |
| std::format_to(str_back, "\treturn lamda_table;\n"); | |
| std::format_to(str_back, "{}();\n", '}'); | |
| std::string arr_str = kw_array_to_cpp(kw_tree.alphabet, std::string_view{"alphabet"}, make_constexpr); | |
| std::format_to(str_back, "{}", arr_str); | |
| std::string final_str = kw_array_to_cpp(kw_tree.final_states, std::string_view{"final_states"}, make_constexpr); | |
| std::format_to(str_back, "{}", final_str); | |
| std::format_to(str_back, "{};", '}'); | |
| return buf; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment