From 5e00d651f172f43917f381edef95c001ed97c904 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Thu, 16 Jan 2020 09:14:15 +0100 Subject: Lex concept --- minicc.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/minicc.cpp b/minicc.cpp index f0f4e4c..e9bc918 100644 --- a/minicc.cpp +++ b/minicc.cpp @@ -46,6 +46,8 @@ struct TreeNode { using Tree = std::map; bool ValidTree(const Tree& tree) { + // Start symbol? + // All terminal symbols? return true; // TODO } @@ -61,7 +63,7 @@ std::vector Lex(std::string s, std::string Top, BNF bnf) for (size_t pos{0}; pos < s.size(); pos++) { char c{s[pos]}; - if (Whitespace.find(c) != std::string::npos) { + if (Whitespace.find(c) != std::string::npos) { // found whitespace character if (candidates.empty()) { // skip if (!token.empty()) throw std::runtime_error("Expected empty token, got "s + token); @@ -69,6 +71,8 @@ std::vector Lex(std::string s, std::string Top, BNF bnf) bool valid{false}; for (const auto& ct : candidates) { if (ValidTree(ct)) { + if (valid) + throw std::runtime_error("Found ambigous token"); result.push_back(token); token.clear(); valid = true; @@ -80,15 +84,19 @@ std::vector Lex(std::string s, std::string Top, BNF bnf) candidates.clear(); } } else { // no whitespace: try to add to tree - for (const auto& ct : candidates) { - if (!TryAdd(cti, c)) { - candidates.erase(i); // no candidate anymore + for (auto& ct : candidates) { + EraseList; + if (!Add(ct, c)) { + EraseList.add(ct); // no candidate anymore } } - if (candidates.empty()) { // no candidates anymore - } else { + + if (candidates.size() - EraseList.size() > 0) { // Added to some candidates: Continue growing token + token.push_back(c); + candidates.erase(EraseList); + } else { // no candidates left: new tree + // TODO: same as above "check candidates" } - //token.push_back(c); } } -- cgit v1.2.3