From 365183e243d164185bca6ad9fa4e0d75664800f4 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 21 Mar 2020 22:11:57 +0100 Subject: C++ grammer (WIP) --- Makefile | 2 +- cpp.cpp | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- grammer.cpp | 2 ++ test-cpp.cpp | 9 +++++-- 4 files changed, 91 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index b8f5495..b28f356 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ PROJECTNAME=minicc CXX=clang++-9 #CXX=g++-8 -CXXFLAGS=-O0 -D_DEBUG +CXXFLAGS=-O0 -g -D_DEBUG # -fprofile-instr-generate -fcoverage-mapping # gcc:--coverage #CXXFLAGS=-O2 -DNDEBUG diff --git a/cpp.cpp b/cpp.cpp index d877564..69ca847 100644 --- a/cpp.cpp +++ b/cpp.cpp @@ -99,7 +99,85 @@ namespace { std::unordered_set keywords { "alignas", "alignof", - // ... Keywords table, p.15 + "asm", + "auto", + "bool", + "break", + "case", + "catch", + "char", + "char8_t", + "char16_t", + "char32_t", + "class", + "concept", + "const", + "consteval", + "constexpr", + "constinit", + "const_cast", + "continue", + "co_await", + "co_return", + "co_yield", + "decltype", + "default", + "delete", + "do", + "double", + "dynamic_cast", + "else", + "enum", + "explicit", + "export", + "extern", + "false", + "float", + "for", + "friend", + "goto", + "if", + "inline", + "int", + "long", + "mutable", + "namespace", + "new", + "noexcept", + "nullptr", + "operator", + "private", + "protected", + "public", + "register", + "reinterpret_cast", + "requires", + "return", + "short", + "signed", + "sizeof", + "static", + "static_assert", + "static_cast", + "struct", + "switch", + "template", + "this", + "thread_local", + "throw", + "true", + "try", + "typedef", + "typeid", + "typename", + "union", + "unsigned", + "using", + "virtual", + "void", + "volatile", + "wchar_t", + "while", }; } @@ -119,12 +197,10 @@ std::vector CPP::tokens_from_pptokens(std::vector pp_tokens) for (auto& token: pp_tokens) { if (pp_types.find(token.type) != pp_types.end()) { if (token.type == "identifier") { -#if 0 if (keywords.find(token.value) != keywords.end()) - result.emplace_back("keyword", token.value); + result.emplace_back(token.value, token.value); else -#endif - result.emplace_back(Token{"identifier"s, token.value}); + result.emplace_back(Token{"identifier"s, token.value}); } else if (token.type == "preprocessing-op-or-punc") result.emplace_back(Token{token.value, token.value}); diff --git a/grammer.cpp b/grammer.cpp index 2379e9a..5557b12 100644 --- a/grammer.cpp +++ b/grammer.cpp @@ -230,6 +230,8 @@ bool Compiler::AddRootNode() const auto& variants{bnf[type]}; for (int i = 0; i < variants.size(); i++) { const std::vector & variant{variants[i]}; + if (variant.size() == 0) + continue; // TODO: Handle case of empty rule (see e.g. C++ "attribute-list") if (child_type == variant[0]) { if (node_type == "") { node_type = type; diff --git a/test-cpp.cpp b/test-cpp.cpp index d08f9b0..2a67b38 100644 --- a/test-cpp.cpp +++ b/test-cpp.cpp @@ -24,7 +24,7 @@ class CppTest: public ::testing::Test { protected: CppTest() { - //debug = true; + debug = true; } ~CppTest() { } @@ -40,8 +40,13 @@ TEST_F(CppTest, preprocessing_tokenize) { auto tokens = cpp.tokens_from_pptokens(pp_tokens); ASSERT_EQ(tokens.size(), 9); +#if 0 + for (auto &i: tokens) { + std::cout << i.type << ": " << i.value << std::endl; + } +#endif - //auto result = cpp.analysis(tokens); + auto result = cpp.analysis(tokens); } #endif -- cgit v1.2.3