// Muestra una gramática como cuádrupla y lista las cadenas más cortas que genera:
//   show_cfg ARCHIVO.cfg [LONGITUD]
// Sin LONGITUD, lista las cadenas de hasta 6 símbolos.
#include <cstddef>
#include <iostream>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>

#include "cfg.h"

int main(int argc, char** argv) {
    if (argc < 2 || argc > 3) {
        std::cerr << "uso: show_cfg ARCHIVO.cfg [LONGITUD]\n";
        return 2;
    }
    try {
        Cfg g = load_cfg(argv[1]);
        std::size_t max_length = (argc == 3) ? std::stoul(argv[2]) : 6;
        std::cout << "G = (V, Σ, R, S)\n  V = {";
        for (std::size_t i = 0; i < g.variables.size(); i++) {
            std::cout << (i == 0 ? "" : ", ") << g.variables[i];
        }
        std::cout << "}\n  Σ = {";
        for (std::size_t i = 0; i < g.terminals.size(); i++) {
            std::cout << (i == 0 ? "" : ", ") << g.terminals[i];
        }
        std::cout << "}\n  S = " << g.start << "\n  R tiene " << g.rules.size() << " reglas:\n";
        for (const Rule& rule : g.rules) {
            std::cout << "    " << rule.head << " → " << form_text(g, rule.body) << '\n';
        }
        std::vector<std::string> words = shortest_first(generated_strings(g, max_length));
        std::cout << "Cadenas de L(G) con longitud a lo sumo " << max_length << " (" << words.size() << "):\n ";
        for (const std::string& w : words) {
            std::cout << ' ' << (w.empty() ? "ε" : w);
        }
        std::cout << '\n';
    } catch (const std::exception& error) {
        std::cerr << "error: " << error.what() << '\n';
        return 1;
    }
}
