summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-02-15 15:42:20 +0100
committerRoland Reichwein <mail@reichwein.it>2020-02-15 15:42:20 +0100
commit0950c449066770292beae220ba4289ce124b4680 (patch)
tree49dc92b5890b3323dd092d1f68c4fdf9ae4e0ecb
parented724a70c92560f13213d745409f45d8c60cf5e0 (diff)
Bugfixing
-rw-r--r--grammer.cpp22
-rw-r--r--test-lexer.cpp5
2 files changed, 13 insertions, 14 deletions
diff --git a/grammer.cpp b/grammer.cpp
index bbaaa79..55e31c8 100644
--- a/grammer.cpp
+++ b/grammer.cpp
@@ -77,7 +77,7 @@ void Compiler::DumpTree()
std::cout << "--- Nodes ------------------------------------" << std::endl;
std::cout << "root_node_id=" << root_node_id << std::endl;
for (const auto& node : nodes) {
- std::cout << node.type << "(" << node.node_id << "): ";
+ std::cout << node.type << "(" << node.node_id << "):";
for (const auto& child : node.child_ids) {
std::cout << " " << child;
}
@@ -382,26 +382,24 @@ index_t Compiler::AddNode(const std::string& child_type, index_t parent_index)
index_t index = nodes.size();
parent.child_ids.push_back(index);
- index_t variant;
+ index_t variant{};
std::deque<std::pair<std::string, index_t>> alternatives;
- const auto& lists { bnf[parent.type] };
+ const auto& variants { bnf[child_type] };
bool found{false};
- for (int i = 0; i < lists.size(); i++) { // variants
- if (lists[i].size() > 0 && lists[i][0] == child_type) {
- if (!found) { // use first match
- variant = i;
- found = true;
- } else { // defer all others
- alternatives.emplace_back(child_type, i);
- }
+ for (int i = 0; i < variants.size(); i++) { // variants
+ if (!found) { // use first match
+ variant = i;
+ found = true;
+ } else { // defer all others
+ alternatives.emplace_back(child_type, i);
}
}
nodes.emplace_back(TreeNode{parent_index, index, child_type, variant, alternatives, std::vector<int32_t>{}});
//root stays, tokens_used stays
- std::cout << "AddNode(): " << index << std::endl;
+ std::cout << "AddNode(): " << parent.type << "->" << child_type << ": "<< index << std::endl;
DumpTree();
return index;
diff --git a/test-lexer.cpp b/test-lexer.cpp
index 6733081..0449af9 100644
--- a/test-lexer.cpp
+++ b/test-lexer.cpp
@@ -61,7 +61,7 @@ TEST_F(Test, BNF) {
// implicit?
//std::set<std::string> Terminals{"identifier", "=", ";"};
- std::string Code{"a = bc ; c = 123 ; esd = Ff ; 1 = XYZ"};
+ std::string Code{"a = bc ; c = 123 ; esd = Ff ; 1 = XYZ ;"};
std::vector<Token> tokens_reference{
{"identifier", "a", { 1, 1} },
{"preprocessing-op-or-punc", "=", { 1, 3}},
@@ -77,7 +77,8 @@ TEST_F(Test, BNF) {
{"preprocessing-op-or-punc", ";", { 1, 29}},
{"pp-number", "1", { 1, 31}},
{"preprocessing-op-or-punc", "=", { 1, 33}},
- {"identifier", "XYZ", { 1, 34}},
+ {"identifier", "XYZ", { 1, 35}},
+ {"preprocessing-op-or-punc", ";", { 1, 38}},
};
Lex::Lexer lexer(LexBNF, LexTop);