Skip to content

Commit

Permalink
(#318) implemented Symbol parser
Browse files Browse the repository at this point in the history
  • Loading branch information
xendalm committed Jun 23, 2024
1 parent 0e8fafb commit 64ea5c5
Show file tree
Hide file tree
Showing 21 changed files with 340 additions and 280 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ project(chipollino)
# Add sub directories
add_subdirectory(libs/Fraction)
add_subdirectory(libs/AutomatonToImage)
add_subdirectory(libs/AutomataParser)
add_subdirectory(libs/AutomatonParser)
add_subdirectory(libs/Objects)
add_subdirectory(libs/Tester)
add_subdirectory(libs/Logger)
Expand Down
254 changes: 127 additions & 127 deletions apps/MetamorphicTestsApp/src/MetamorphicTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <string>
#include <unordered_set>

#include "MetamorphicTestsApp/MetamorphicTests.h"
#include "AutomataParser/Parser.h"
#include "AutomatonParser/Parser.h"
#include "InputGenerator/AutomatonGenerator.h"
#include "InputGenerator/RegexGenerator.h"
#include "MetamorphicTestsApp/MetamorphicTests.h"
#include "Objects/BackRefRegex.h"
#include "Objects/FiniteAutomaton.h"
#include "Objects/MemoryFiniteAutomaton.h"
Expand Down Expand Up @@ -178,6 +178,56 @@ TEST(TestMFA, ToTxt) {
}
}

TEST(TestNFA, ToMFA) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 2);
SCOPED_TRACE("Regex: " + rgx_str);
MemoryFiniteAutomaton mfa1 = BackRefRegex(rgx_str).to_mfa();
ASSERT_TRUE(MemoryFiniteAutomaton::equal(mfa1, mfa1.to_symbolic_fa().to_mfa()));
MemoryFiniteAutomaton mfa2 = BackRefRegex(rgx_str).to_mfa_additional();
ASSERT_TRUE(MemoryFiniteAutomaton::equal(mfa2, mfa2.to_symbolic_fa().to_mfa()));
}
}

TEST(TestMFA, ToFA) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = rg.generate_regex();
SCOPED_TRACE("Regex: " + rgx_str);
Regex r = Regex(rgx_str);
FiniteAutomaton fa = r.to_glushkov();
ASSERT_TRUE(FiniteAutomaton::equal(fa, fa.to_mfa().to_action_fa()));
ASSERT_TRUE(FiniteAutomaton::equal(fa, fa.to_mfa().to_symbolic_fa()));
}
}

TEST(TestMFA, Bisimilar) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 1);
SCOPED_TRACE("Regex: " + rgx_str);
BackRefRegex r = BackRefRegex(rgx_str);
MemoryFiniteAutomaton mfa = r.to_mfa_additional();

ASSERT_TRUE(MemoryFiniteAutomaton::action_bisimilar(mfa, mfa));
ASSERT_TRUE(MemoryFiniteAutomaton::symbolic_bisimilar(mfa, mfa));
ASSERT_TRUE(MemoryFiniteAutomaton::bisimilar(mfa, mfa).value());
}
}

TEST(TestMFA, MergeBisimilar) {
RegexGenerator rg(6, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 2);
SCOPED_TRACE("Regex: " + rgx_str);
BackRefRegex r = BackRefRegex(rgx_str);
MemoryFiniteAutomaton mfa = r.to_mfa_additional();

MetamorphicTests::cmp_automatons(mfa.merge_bisimilar(), mfa);
}
}

TEST(IsDeterministic, Test_is_deterministic) {
string test_path = "./test_data/MetamorphicTest/test1.txt";
for (int i = 0; i < RegexNumber; i++) {
Expand Down Expand Up @@ -216,53 +266,53 @@ TEST(AutomatonGenerator, Test_MergeBisim_equivalent) {

/*
TEST(Statistics, Test_statistics) {
string test_path = "./TestData/MetamorphicTest/test1.txt";
std::vector<int> OX;
std::vector<float> OY;
AutomatonGenerator::set_initial_state_not_terminal(true);
for (int term = 5; term <= 100; term = term + 5) {
AutomatonGenerator::set_final_probability(term);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::NFA);
a.write_to_file(test_path);
Parser parser;
FiniteAutomaton FA;
try {
FA = parser.parse_NFA(test_path);
} catch (const std::runtime_error& re) {
std::ifstream t(test_path);
stringstream buffer;
buffer << t.rdbuf();
string file = buffer.str();
throw(std::runtime_error(file));
}
if (FA.is_finite()) {
count++;
}
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) <<
"%" << std::endl; OX.push_back(term); OY.push_back(float(count) / float(ALL));
}
std::cout << "OX = [";
for (int i = 0; i < OX.size() - 1; i++) {
std::cout << OX[i] << ",";
}
std::cout << OX[OX.size() - 1] << "]\n";
std::cout << "OY = [";
for (int i = 0; i < OY.size() - 1; i++) {
std::cout << OY[i] << ",";
}
std::cout << OY[OY.size() - 1] << "]\n";
string test_path = "./TestData/MetamorphicTest/test.txt";
std::vector<int> OX;
std::vector<float> OY;
AutomatonGenerator::set_initial_state_not_terminal(true);
for (int term = 5; term <= 100; term = term + 5) {
AutomatonGenerator::set_final_probability(term);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::NFA);
a.write_to_file(test_path);
Parser parser;
FiniteAutomaton FA;
try {
FA = parser.parse_NFA(test_path);
} catch (const std::runtime_error& re) {
std::ifstream t(test_path);
stringstream buffer;
buffer << t.rdbuf();
string file = buffer.str();
throw(std::runtime_error(file));
}
if (FA.is_finite()) {
count++;
}
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) <<
"%" << std::endl; OX.push_back(term); OY.push_back(float(count) / float(ALL));
}
std::cout << "OX = [";
for (int i = 0; i < OX.size() - 1; i++) {
std::cout << OX[i] << ",";
}
std::cout << OX[OX.size() - 1] << "]\n";
std::cout << "OY = [";
for (int i = 0; i < OY.size() - 1; i++) {
std::cout << OY[i] << ",";
}
std::cout << OY[OY.size() - 1] << "]\n";
}
TEST(AutomatonGenerator, Test_Arden_Glushkov_equivalent) {
int ALL = 50;
for (int i = 0; i < ALL; i++) {
string test_path = "./TestData/MetamorphicTest/test1.txt";
string test_path = "./TestData/MetamorphicTest/test.txt";
AutomatonGenerator a(FA_type::NFA, 5);
a.write_to_file(test_path);
Parser parser;
Expand All @@ -286,7 +336,7 @@ ard.minimize().to_txt() << "\n" << FA.to_regex().to_txt();
TEST(AutomatonGenerator, Test_Arden_Glushkov_Ambiguity_equivalent) {
int ALL = 50;
for (int i = 0; i < ALL; i++) {
string test_path = "./TestData/MetamorphicTest/test1.txt";
string test_path = "./TestData/MetamorphicTest/test.txt";
AutomatonGenerator a(FA_type::NFA, 5);
a.write_to_file(test_path);
Parser parser;
Expand All @@ -307,92 +357,42 @@ ard.minimize().to_txt() << "\n" << FA.to_regex().to_txt();
}
TEST(Statistics, Test_dfa) {
for (int term = 5; term <= 50; term = term + 5) {
AutomatonGenerator::set_final_probability(20);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::DFA);
a.write_to_file("./TestData/tmp/test.txt");
auto FA = Parser::parse_DFA("./TestData/tmp/test.txt");
if (FA.is_deterministic() && FA.is_finite()) {
count++;
}
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) * 100<<
for (int term = 5; term <= 50; term = term + 5) {
AutomatonGenerator::set_final_probability(20);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::DFA);
a.write_to_file("./TestData/tmp/test.txt");
auto FA = Parser::parse_DFA("./TestData/tmp/test.txt");
if (FA.is_deterministic() && FA.is_finite()) {
count++;
}
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) * 100<<
"%" << std::endl;
}
}
}
TEST(Statistics, Test_fa) {
std::cout << "TEST\n";
for (int term = 5; term <= 50; term = term + 5) {
AutomatonGenerator::set_final_probability(20);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::FA);
std::cout << "write_to_file START\n";
a.write_to_file("./TestData/tmp/test.txt");
std::cout << "write_to_file DONE\n";
auto FA = Parser::parse_FA("./TestData/tmp/test.txt");
std::cout << i << " " << std::endl;
if (FA.is_deterministic() && FA.is_finite()) {
count++;
}
std::cout << i << " " << std::endl;
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) * 100
std::cout << "TEST\n";
for (int term = 5; term <= 50; term = term + 5) {
AutomatonGenerator::set_final_probability(20);
int count = 0;
int ALL = 10000;
for (int i = 0; i < ALL; i++) {
AutomatonGenerator a(FA_type::FA);
std::cout << "write_to_file START\n";
a.write_to_file("./TestData/tmp/test.txt");
std::cout << "write_to_file DONE\n";
auto FA = Parser::parse_FA("./TestData/tmp/test.txt");
std::cout << i << " " << std::endl;
if (FA.is_deterministic() && FA.is_finite()) {
count++;
}
std::cout << i << " " << std::endl;
}
std::cout << "final_probability = " << term << " : " << float(count) / float(ALL) * 100
<< "%" << std::endl;
}
}*/

TEST(TestNFA, ToMFA) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 2);
SCOPED_TRACE("Regex: " + rgx_str);
MemoryFiniteAutomaton mfa1 = BackRefRegex(rgx_str).to_mfa();
ASSERT_TRUE(MemoryFiniteAutomaton::equal(mfa1, mfa1.to_symbolic_fa().to_mfa()));
MemoryFiniteAutomaton mfa2 = BackRefRegex(rgx_str).to_mfa_additional();
ASSERT_TRUE(MemoryFiniteAutomaton::equal(mfa2, mfa2.to_symbolic_fa().to_mfa()));
}
}

TEST(TestMFA, ToFA) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = rg.generate_regex();
SCOPED_TRACE("Regex: " + rgx_str);
Regex r = Regex(rgx_str);
FiniteAutomaton fa = r.to_glushkov();
ASSERT_TRUE(FiniteAutomaton::equal(fa, fa.to_mfa().to_action_fa()));
ASSERT_TRUE(FiniteAutomaton::equal(fa, fa.to_mfa().to_symbolic_fa()));
}
}

TEST(TestMFA, Bisimilar) {
RegexGenerator rg(5, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 1);
SCOPED_TRACE("Regex: " + rgx_str);
BackRefRegex r = BackRefRegex(rgx_str);
MemoryFiniteAutomaton mfa = r.to_mfa_additional();

ASSERT_TRUE(MemoryFiniteAutomaton::action_bisimilar(mfa, mfa));
ASSERT_TRUE(MemoryFiniteAutomaton::symbolic_bisimilar(mfa, mfa));
ASSERT_TRUE(MemoryFiniteAutomaton::bisimilar(mfa, mfa).value());
}
}

TEST(TestMFA, MergeBisimilar) {
RegexGenerator rg(6, 3, 3, 2);
for (int i = 0; i < RegexNumber; i++) {
string rgx_str = MetamorphicTests::generate_bregex(rg, 2);
SCOPED_TRACE("Regex: " + rgx_str);
BackRefRegex r = BackRefRegex(rgx_str);
MemoryFiniteAutomaton mfa = r.to_mfa_additional();

MetamorphicTests::cmp_automatons(mfa.merge_bisimilar(), mfa);
}
}
}*/
36 changes: 19 additions & 17 deletions apps/UnitTestsApp/src/UnitTests.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "UnitTestsApp/UnitTests.h"
#include "AutomatonParser/Parser.h"
#include "AutomatonToImage/AutomatonToImage.h"
#include "Interpreter/Interpreter.h"
#include "AutomataParser/Parser.h"
#include "Objects/AlgExpression.h"
#include "Objects/BackRefRegex.h"
#include "Objects/FiniteAutomaton.h"
Expand Down Expand Up @@ -439,11 +439,11 @@ TEST(TestGetOneUnambigous, GetOneUnambigousWorks) {
TEST(TestInterpreter, RunLineTest) {
Interpreter interpreter;
interpreter.set_log_mode(Interpreter::LogMode::nothing);
ASSERT_TRUE(!interpreter.run_line("A = Annote (Glushkova {a})"));
ASSERT_FALSE(interpreter.run_line("A = Annote (Glushkova {a})"));
ASSERT_TRUE(interpreter.run_line(" N1 = ( ( Glushkov ({ab|a}) )) "));
ASSERT_TRUE(interpreter.run_line(" N2 = (Annote N1)"));
ASSERT_TRUE(!interpreter.run_line("N2 = (Glushkov N1)"));
ASSERT_TRUE(!interpreter.run_line("Equiv N1 N3"));
ASSERT_FALSE(interpreter.run_line("N2 = (Glushkov N1)"));
ASSERT_FALSE(interpreter.run_line("Equiv N1 N3"));
ASSERT_TRUE(interpreter.run_line(" Equiv (( N1)) ( (Reverse .Reverse (N2) !! ))"));
ASSERT_TRUE(interpreter.run_line("Test (Glushkov {a*}) {a*} 1"));

Expand All @@ -461,14 +461,14 @@ TEST(TestInterpreter, RunLineTest) {
ASSERT_TRUE(interpreter.run_line("A = [[] []]"));
ASSERT_TRUE(interpreter.run_line("A = [{a} {b}]"));
ASSERT_TRUE(interpreter.run_line("A = [[(([{a}]))] [{a} []]]"));
ASSERT_TRUE(!interpreter.run_line("A = [[(([{a}])] [{a} []]]"));
ASSERT_TRUE(!interpreter.run_line("A = [[([{a}]))] [{a} []]]"));
ASSERT_TRUE(!interpreter.run_line("A = [[(([{a}]))] [{a} []]"));
ASSERT_TRUE(!interpreter.run_line("A = [[(([a}]))] [{a} (Glushkov(DeAnnote {a} !!) !!) []]]"));
ASSERT_FALSE(interpreter.run_line("A = [[(([{a}])] [{a} []]]"));
ASSERT_FALSE(interpreter.run_line("A = [[([{a}]))] [{a} []]]"));
ASSERT_FALSE(interpreter.run_line("A = [[(([{a}]))] [{a} []]"));
ASSERT_FALSE(interpreter.run_line("A = [[(([a}]))] [{a} (Glushkov(DeAnnote {a} !!) !!) []]]"));

// Normalize
ASSERT_TRUE(interpreter.run_line("A = Normalize {abc} [[{a} {b}]]"));
ASSERT_TRUE(!interpreter.run_line("A = Normalize {abc} [[{a} []]]"));
ASSERT_FALSE(interpreter.run_line("A = Normalize {abc} [[{a} []]]"));
}

TEST(TestTransformationMonoid, IsMinimal) {
Expand Down Expand Up @@ -960,9 +960,9 @@ TEST(TestAutomatonParser, MFA_correctness_failure) {
try {
Parser parser;
parser.parse_MFA(cycle_with_cell_reopen);
} catch (const std::runtime_error& re) {
ASSERT_EQ(string(re.what()), string("Parser: incorrect memory usage in MFA"));
}
} catch (const std::logic_error& re) {
ASSERT_EQ(string(re.what()), "AutomatonParser: incorrect memory usage in MFA");
}
}

TEST(TestAutomatonParser, MFA_correctness) {
Expand All @@ -974,11 +974,13 @@ TEST(TestAutomatonParser, MFA_correctness) {

// TODO: FAILED:
/*TEST(AutomatonGenerator, Test_Arden_Glushkov_Ambiguity_equivalent) {
Regex r("((e|k)he*cg)*(|(e|k)he*|((e|k)(b|i)|(e|k)he*(e|ck)))");
auto ard = r.to_glushkov();
auto first = ard.ambiguity();
auto second = ard.to_regex().to_glushkov().ambiguity();
Regex r("((e|k)he*cg)*(|(e|k)he*|((e|k)(b|i)|(e|k)he*(e|ck)))");
auto ard = r.to_glushkov();
auto first = ard.ambiguity();
auto second = ard.to_regex().to_glushkov().ambiguity();
ASSERT_EQ(first,second) << "\n" << ard.minimize().to_txt() << "\n" << ard.to_regex().to_glushkov().minimize().to_txt() << "\n" << ard.to_regex().to_txt() << "\n" << ard.to_regex().to_glushkov().to_regex().to_txt();
ASSERT_EQ(first,second) << "\n" << ard.minimize().to_txt() << "\n" <<
ard.to_regex().to_glushkov().minimize().to_txt() << "\n" << ard.to_regex().to_txt() << "\n" <<
ard.to_regex().to_glushkov().to_regex().to_txt();
}*/
Loading

0 comments on commit 64ea5c5

Please sign in to comment.