|
| 1 | +#include "utils/utils.h" |
| 2 | + |
1 | 3 | #include "gtest/gtest.h" |
2 | | -#include "utils/first_follow.h" |
3 | | -#include "utils/left_factoring.h" |
4 | | -#include "utils/left_recursion.h" |
5 | 4 |
|
6 | 5 | namespace grammar = jucc::grammar; |
7 | 6 | namespace utils = jucc::utils; |
@@ -583,3 +582,35 @@ TEST(utils, CalcFollows2) { |
583 | 582 | ASSERT_EQ(res.at("T'"), std::vector<std::string>({utils::STRING_ENDMARKER, ")", "+"})); |
584 | 583 | ASSERT_EQ(res.at("F"), std::vector<std::string>({utils::STRING_ENDMARKER, ")", "*", "+"})); |
585 | 584 | } |
| 585 | + |
| 586 | +TEST(utils, RemoveAllAmbiguity0) { |
| 587 | + // E -> ieStSt | a | b | ieStP |
| 588 | + grammar::Production p; |
| 589 | + p.SetParent("E"); |
| 590 | + p.SetRules({grammar::Rule({"i", "e", "S", "t", "S", "t"}), grammar::Rule({"a"}), grammar::Rule({"b"}), |
| 591 | + grammar::Rule({"i", "e", "S", "t", "P"})}); |
| 592 | + |
| 593 | + auto lf_removed = utils::RemoveAllPossibleAmbiguity({p}); |
| 594 | + auto non_terminals = utils::GetAllNonTerminals(lf_removed); |
| 595 | + |
| 596 | + ASSERT_EQ(lf_removed.size(), 2); |
| 597 | + // output |
| 598 | + // E -> ieStE' | a | b | |
| 599 | + // E' -> St | P | epsilon |
| 600 | + ASSERT_EQ(non_terminals.size(), 2); |
| 601 | + ASSERT_EQ(non_terminals[0], "E"); |
| 602 | + ASSERT_EQ(non_terminals[1], "E'"); |
| 603 | + ASSERT_EQ(lf_removed[0].GetParent(), "E"); |
| 604 | + ASSERT_EQ(lf_removed[1].GetParent(), "E" + std::string(utils::DASH)); |
| 605 | + |
| 606 | + ASSERT_EQ(lf_removed[0].GetRules().size(), 3); |
| 607 | + ASSERT_EQ(lf_removed[1].GetRules().size(), 3); |
| 608 | + |
| 609 | + ASSERT_EQ(lf_removed[0].GetRules()[0].ToString(), "ieStE'"); |
| 610 | + ASSERT_EQ(lf_removed[0].GetRules()[1].ToString(), "a"); |
| 611 | + ASSERT_EQ(lf_removed[0].GetRules()[2].ToString(), "b"); |
| 612 | + |
| 613 | + ASSERT_EQ(lf_removed[1].GetRules()[0].ToString(), "St"); |
| 614 | + ASSERT_EQ(lf_removed[1].GetRules()[1].ToString(), "P"); |
| 615 | + ASSERT_EQ(lf_removed[1].GetRules()[2].ToString(), std::string(grammar::EPSILON)); |
| 616 | +} |
0 commit comments