commit ed65d13844d6a6ee1e30c0378e87b9eca18effb7
parent e61f0c4cb1f16d469d7c21c58ac1c967dc7a69a8
Author: Pranav Prabhu Kumble <kumblepranavprabhu@gmail.com>
Date: Mon, 30 Mar 2026 15:32:18 +0530
Week 2: Add parser testing utility (Part 2: Core logic)
Diffstat:
1 file changed, 42 insertions(+), 0 deletions(-)
diff --git a/src/parser_tester.cpp b/src/parser_tester.cpp
@@ -30,3 +30,45 @@ int main(int argc, char* argv[]) {
// Optional: Test strings for validation
std::string testFilename = (argc > 2) ? argv[2] : "";
std::vector<std::string> testStrings;
+ if (!testFilename.empty()) {
+ std::ifstream testFile(testFilename);
+ if (testFile.is_open()) {
+ std::string testLine;
+ while (std::getline(testFile, testLine)) {
+ std::string trimmed = trim(testLine);
+ if (trimmed.empty() || trimmed[0] == '#') continue;
+ testStrings.push_back(trimmed);
+ }
+ }
+ }
+
+ std::string line;
+ int lineNum = 0;
+ int regexIdx = 0;
+ std::vector<std::unique_ptr<NFA>> nfas;
+ std::vector<std::string> originalRegexes;
+
+ // Core Pipeline: Regex -> Tokens -> AST -> NFA
+ while (std::getline(regexFile, line)) {
+ lineNum++;
+ std::string trimmedLine = trim(line);
+ if (trimmedLine.empty() || trimmedLine[0] == '#') continue;
+
+ originalRegexes.push_back(trimmedLine);
+ Lexer lexer(trimmedLine, lineNum);
+ try {
+ std::vector<Token> tokens = lexer.tokenize();
+ Parser parser(tokens);
+ auto ast = parser.parse();
+
+ // Glushkov NFA construction
+ auto nfa = NFABuilder::build(ast.get(), regexIdx++);
+ if (nfa) {
+ nfas.push_back(std::move(nfa));
+ }
+ } catch (const std::exception& e) {
+ std::cerr << "Error processing regex '" << trimmedLine << "': " << e.what() << std::endl;
+ }
+ }
+
+ std::cout << "Successfully built " << nfas.size() << " NFAs." << std::endl;