Skip to content

Commit

Permalink
ANYCC 29
Browse files Browse the repository at this point in the history
- Add terminals to a terminals set.
- Generate a md file that contains Predictive Table.
- Call generateMarkdownTable in Parser.
  • Loading branch information
Bazina committed Dec 24, 2023
1 parent 31a6add commit d989d19
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/Parser/PredictiveTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ class PredictiveTable {
*/
void printPredictiveTable();

void generateMarkdownTable(const std::string &outputFilePath);


private:
std::unordered_map<CellKey, CellValue *, CellKeyHashFn> predictive_table;
std::set<std::string> non_terminals;
std::set<std::string> terminals;
std::unordered_map<std::string, std::set<std::pair<std::string, Production>, CompareFirst>> computed_first_sets;
std::unordered_map<std::string, std::set<std::string>> computed_follow_sets;

Expand Down
1 change: 1 addition & 0 deletions src/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void Parser::buildPredictiveTable() {
predictiveTable->buildPredictiveTable();
std::cout << "\nPredictive Table:\n";
predictiveTable->printPredictiveTable();
predictiveTable->generateMarkdownTable("../PredictiveTable.md");
}

void Parser::buildPredictiveTopDownParser() {
Expand Down
46 changes: 46 additions & 0 deletions src/Parser/PredictiveTable.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <fstream>
#include "Parser/PredictiveTable.h"
#include "constants.h"

Expand All @@ -8,6 +9,7 @@ PredictiveTable::PredictiveTable(
this->computed_first_sets = computed_first_sets;
this->computed_follow_sets = computed_follow_sets;
this->non_terminals = non_terminals;
this->terminals.insert("$");
}

PredictiveTable::~PredictiveTable() {
Expand Down Expand Up @@ -103,6 +105,7 @@ void PredictiveTable::insertProduction(const std::string &non_terminal, const st
const Production &production, ParsingTableEntryType predictive_table_enum) {
CellKey cell_key = CellKey(non_terminal, terminal);
auto cell_value = new CellValue(production, predictive_table_enum);
terminals.insert(terminal);
if (containsKey(non_terminal, terminal)) {
std::cout << "\nGrammar isn't LL(1)\n";
printConflict(non_terminal, terminal, production);
Expand Down Expand Up @@ -150,3 +153,46 @@ PredictiveTable::printConflict(const std::string &non_terminal, const std::strin
}
std::cout << "\n";
}

// Function to generate a Markdown table
void PredictiveTable::generateMarkdownTable(const std::string &outputFilePath) {
std::ofstream outputFile(outputFilePath);

// Write header row
outputFile << "| **Non-Terminal** |";
for (const auto &terminal: terminals) {
if (terminal == "*")
outputFile << " __*__ |";
else
outputFile << " **" << terminal << "** |";
}
outputFile << "\n|------------------|";
for (int i = 0; i < terminals.size(); ++i) {
outputFile << "------------|";
}
outputFile << "\n";

// Iterate through non-terminals and terminals to fill in the table
for (const auto &non_terminal: non_terminals) {
outputFile << "| **" << non_terminal << "** |";
for (const auto &terminal: terminals) {
const CellValue *cellValue = lookUp(non_terminal, terminal);
if (hasProduction(non_terminal, terminal)) {
const auto &production = cellValue->getProduction();
std::string productionStr;
if (!production.productions.empty()) {
for (const auto &symbol: production.productions[0]) {
productionStr += symbol + " ";
}
productionStr.pop_back(); // Remove the extra space
}
outputFile << " `" << productionStr << "` |";
} else if (isSynchronizing(non_terminal, terminal)) {
outputFile << " `Synch` |";
}
}
outputFile << "\n";
}

outputFile.close();
}

0 comments on commit d989d19

Please sign in to comment.