diff options
-rw-r--r-- | minicc.cpp | 22 |
1 files changed, 15 insertions, 7 deletions
@@ -46,6 +46,8 @@ struct TreeNode { using Tree = std::map<index_t, TreeNode>; bool ValidTree(const Tree& tree) { + // Start symbol? + // All terminal symbols? return true; // TODO } @@ -61,7 +63,7 @@ std::vector<std::string> 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<std::string> 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<std::string> 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); } } |