// De expresión regular a NFA con la construcción de Thompson: thompson EXPRESIÓN [ALFABETO]
// Escribe el NFA en el formato .nfa. Sin alfabeto, usa los símbolos de la expresión.
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

#include "nfa.h"
#include "regex.h"
#include "thompson.h"

int main(int argc, char** argv) {
    if (argc < 2 || argc > 3) {
        std::cerr << "uso: thompson EXPRESIÓN [ALFABETO]\n";
        return 2;
    }
    try {
        RegexPtr r = parse_regex(argv[1]);
        std::vector<char> alphabet = symbols_of(r);
        if (argc == 3) {
            std::string given = argv[2];
            alphabet.assign(given.begin(), given.end());
        }
        Nfa n = thompson(r, alphabet);
        std::cout << "# NFA de Thompson de " << to_string(r) << '\n';
        write_nfa(std::cout, n);
    } catch (const std::runtime_error& error) {
        std::cerr << "error: " << error.what() << '\n';
        return 1;
    }
}
