summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-01-16 09:14:15 +0100
committerRoland Reichwein <mail@reichwein.it>2020-01-16 09:14:15 +0100
commit5e00d651f172f43917f381edef95c001ed97c904 (patch)
tree298368c4bb0c243439cf475c925b0f15d033a211
parentc413fee0cde65e379f82afffd8d701f663aeb0be (diff)
Lex concept
-rw-r--r--minicc.cpp22
1 files 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<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);
}
}