diff options
| author | Roland Reichwein <mail@reichwein.it> | 2020-01-16 09:14:15 +0100 | 
|---|---|---|
| committer | Roland Reichwein <mail@reichwein.it> | 2020-01-16 09:14:15 +0100 | 
| commit | 5e00d651f172f43917f381edef95c001ed97c904 (patch) | |
| tree | 298368c4bb0c243439cf475c925b0f15d033a211 | |
| parent | c413fee0cde65e379f82afffd8d701f663aeb0be (diff) | |
Lex concept
| -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);    }   } | 
