Skip to content

Commit 1efa869

Browse files
committed
sort output alphabetically
1 parent c208e49 commit 1efa869

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/ll1_parser.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,26 @@ void LL1Parser::PrintTableUsingTabulate() {
322322
.font_color(Color::yellow)
323323
.font_style({FontStyle::bold});
324324

325+
std::vector<std::string> non_terminals;
325326
for (const auto& outerPair : ll1_t_) {
326-
const std::string& nonTerminal = outerPair.first;
327-
Table::Row_t row_data = {nonTerminal};
327+
non_terminals.push_back(outerPair.first);
328+
}
329+
330+
std::sort(non_terminals.begin(), non_terminals.end(),
331+
[this](const std::string& a, const std::string& b) {
332+
if (a == gr_.axiom_)
333+
return true; // Axiom comes first
334+
if (b == gr_.axiom_)
335+
return false; // Axiom comes first
336+
return a < b; // Sort the rest alphabetically
337+
});
338+
339+
for (const std::string& nonTerminal : non_terminals) {
340+
Table::Row_t row_data = {nonTerminal};
328341

329342
for (const auto& col : columns) {
330-
auto innerIt = outerPair.second.find(col.first);
331-
if (innerIt != outerPair.second.end()) {
343+
auto innerIt = ll1_t_.at(nonTerminal).find(col.first);
344+
if (innerIt != ll1_t_.at(nonTerminal).end()) {
332345
std::string cell_content;
333346
for (const auto& prod : innerIt->second) {
334347
cell_content += "[ ";
@@ -342,7 +355,8 @@ void LL1Parser::PrintTableUsingTabulate() {
342355
row_data.push_back("-");
343356
}
344357
}
345-
auto& tb = table.add_row(row_data);
358+
359+
table.add_row(row_data);
346360
}
347361

348362
table[0].format().font_color(Color::cyan).font_style({FontStyle::bold});
@@ -355,5 +369,7 @@ void LL1Parser::PrintTableUsingTabulate() {
355369
}
356370
table.format().font_align(FontAlign::center);
357371
table.column(0).format().font_color(Color::cyan);
372+
373+
// Print the table
358374
std::cout << table << std::endl;
359375
}

0 commit comments

Comments
 (0)