summaryrefslogtreecommitdiffhomepage
path: root/cppbnf.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'cppbnf.cpp')
-rwxr-xr-xcppbnf.cpp51
1 files changed, 28 insertions, 23 deletions
diff --git a/cppbnf.cpp b/cppbnf.cpp
index 5266fa0..0b6e58f 100755
--- a/cppbnf.cpp
+++ b/cppbnf.cpp
@@ -21,7 +21,7 @@ namespace {
return result;
}
- std::unordered_map<std::string, std::unordered_set<std::string>> reverseBNF(const T_BNF& bnf)
+ std::unordered_map<std::string, std::unordered_set<std::string>> reverseBNF(const BNF& bnf)
{
std::unordered_map<std::string, std::unordered_set<std::string>> result;
for (const auto& [symbol, lists] : bnf) {
@@ -39,12 +39,12 @@ namespace {
return result;
}
- bool isTerminal(const std::string& symbol, const T_BNF& bnf)
+ bool isTerminal(const std::string& symbol, const BNF& bnf)
{
return bnf.find(symbol) == bnf.end();
}
- size_t numberOfStartSymbols(const T_BNF& bnf)
+ size_t numberOfStartSymbols(const BNF& bnf)
{
// exactly 1 start symbol
std::vector<std::string> startSymbols;
@@ -64,7 +64,7 @@ namespace {
return startSymbols.size();
}
- bool symbolsValid(const T_BNF& bnf)
+ bool symbolsValid(const BNF& bnf)
{
for (const auto& [symbol, lists] : bnf) {
if (boost::contains(symbol, " ")) {
@@ -74,14 +74,19 @@ namespace {
for (const auto& list : lists) {
for (const auto& i : list) {
- if (i.size() == 1 && !isTerminal(i, bnf)) {
- std::cerr << "Warning: Non-Terminal symbol " << i << " in " << symbol << " is too long." << std::endl;
- return false;
- }
- if (boost::contains(i, " ")) {
- std::cerr << "Warning: Symbol " << i << " in " << symbol << " contains space." << std::endl;
- return false;
+ if (!isTerminal(i, bnf)) {
+ // every non-terminal symbol must be longer that 1 char
+ if (i.size() == 1) {
+ std::cerr << "Warning: Non-Terminal symbol " << i << " in " << symbol << " is too short." << std::endl;
+ return false;
+ }
+
+ // non-terminal symbols must not contain space
+ if (boost::contains(i, " ")) {
+ std::cerr << "Warning: Symbol " << i << " in " << symbol << " contains space." << std::endl;
+ return false;
+ }
}
}
}
@@ -92,11 +97,11 @@ namespace {
// returns 1 if exactly one start symbol and
// all nodes size > 1, except terminal symbols
- bool valid(const T_BNF& bnf) {
+ bool valid(const BNF& bnf) {
return numberOfStartSymbols(bnf) == 1 && symbolsValid(bnf);
}
- bool validLex(const T_BNF& bnf) {
+ bool validLex(const BNF& bnf) {
// all terminal symbols exactly one character
for (const auto& [symbol, lists] : bnf) {
@@ -174,7 +179,7 @@ namespace {
}
}
- T_BNF& normalizeBNF(T_BNF& bnf)
+ BNF& normalizeBNF(BNF& bnf)
{
// resolve OPTIONAL symbols
for (auto& [symbol, lists] : bnf) {
@@ -184,7 +189,7 @@ namespace {
return bnf;
}
- T_BNF& normalizeLexBNF(T_BNF& bnf)
+ BNF& normalizeLexBNF(BNF& bnf)
{
normalizeBNF(bnf);
@@ -206,9 +211,9 @@ namespace {
} // namespace
-T_BNF GetCppBNFLex()
+BNF GetCppBNFLex()
{
- T_BNF bnf{
+ BNF bnf{
// [gram.lex]
{"hex-quad", {{"hexadecimal-digit", "hexadecimal-digit", "hexadecimal-digit", "hexadecimal-digit"}}},
@@ -520,9 +525,9 @@ T_BNF GetCppBNFLex()
return normalizeLexBNF(bnf);
}
-T_BNF GetCppBNFGram()
+BNF GetCppBNFGram()
{
- T_BNF bnf{
+ BNF bnf{
// [gram.key]
{"typedef-name", {{"identifier"}, {"simple-template-id"}}},
@@ -1919,15 +1924,15 @@ T_BNF GetCppBNFGram()
return normalizeBNF(bnf);
}
-TEST(Lex, Test2) {
- auto bnf = GetCppBNFLex();
+TEST(CppBnf, LexicalBnf) {
+ auto bnf = SubBNF(GetCppBNFLex(), "preprocessing-token");
EXPECT_TRUE(valid(bnf));
EXPECT_TRUE(validLex(bnf));
}
-TEST(Gram, Test3) {
- auto bnf = GetCppBNFGram();
+TEST(CppBnf, GrammarBnf) {
+ auto bnf = SubBNF(GetCppBNFGram(), "translation-unit");
EXPECT_TRUE(valid(bnf));
}