int main (int argc, char *argv[]) {

	char * in_name, * in_ext, * in_filename, * out_filename;

	if (argc != 2) {
		printf("Usage %s <grammar_filename>\n", argv[0]);
		return 1;
	}

	in_filename = argv[1];
	infile = fopen(in_filename, "r");
	if (infile == NULL) {
		fprintf(stderr, "ERROR: Could not open file \"%s\"\n", in_filename);
		return 1;
	}

	in_ext = get_filename_extension(in_filename);
	in_name = get_filename(in_filename);
	out_filename = store_to_file(in_name, in_ext);

	printf("###\tRecursive Generator\t###\n");

	outfile = fopen(out_filename, "w");
	if (outfile == NULL) {
		fprintf(stderr, "ERROR:Could not open file \"%s\"\n", outfile);
		fclose(infile);
		return 1;
	}

	printf("Parsing file %s...\n", in_filename);
	parse_file(infile);

	//print_grammar();
	printf("Output filename: %s\n", out_filename);
	printf("Generating sentence... \t");

	generate_sentence(START_ELEMENT);

	printf("Done!\n");

	fprintf(outfile, "\n");
	fclose(infile);
	fclose(outfile);

	printf("Quiting...\n");
	destroy_grammar();

	return 0;
}
Exemplo n.º 2
0
int main(int argc, char** argv) {

    if (argc < 2) {
        usage(argv[0]);
    }

    int len = strlen(argv[1]);
    char* dot = ".dot";
    char* gc = ".gr";

    struct grammar* g;
    struct automata* a;

    if (strcmp(dot, argv[1]+len-4) == 0) {
        
        a = parse_automata_file(argv[1]);

        // 1. Símbolos terminales.
        printf("Terminal symbols: \n\t");
        for (int i = 0; i < a->number_chars; i++) {
            if (i != 0) printf(", ");
            printf("%c", a->chars[i]);
        }

        // 2. Estados.
        printf("\n\nStates: \n\t{");
        for (int i = 0; i < a->number_states; i++) {
            if (i != 0) printf(", ");
            printf("q%d", a->states[i]);
        }

        // 3. Estado inicial.
        printf("}\n\nInitial state: q%d\n", a->states[0]);

        // 4. Conjunto de estados finales.
        printf("\nFinal states = {");
        bool first = true;
        for (int i = 0; i < a->number_states; i++) {
            if (a->final_state[i]) {
                if (!first) printf(", ");
                printf("q%d", a->states[i]);
            }
        }

        printf("}\n");

        // 5. Tabla de la función de transición.
        // TODO
        print_ascii_table_for_automata(a);

        // 6. La especificación completa de la gramática equivalente.
        g = automata_to_grammar(a);
        struct grammar * aux = take_out_unreachable(g);
        destroy_grammar(g);
        g = aux;
        char filename[255];
        memset(filename, 0, 255);
        strcpy(filename, argv[1]);
        strcpy(filename+len-3, "gr\0");
        filename[len-1] = '\0';
        FILE* file = fopen(filename, "w");
        grammar_output(file, g);

        destroy_grammar(g);
        free(a);
    }

    else if (strcmp(gc, argv[1]+len-3) == 0) {

        g = parse_grammar_file(argv[1]);
        // 1) Símbolos terminales
        bool first = true;
        printf("Terminal symbols:\n\t");
        for (int i = 0; i < g->number_symbols; i++) {
            if (g->symbols[i].terminal && g->symbols[i].representation != '\\') {
                if (!first) printf(", ");
                first = false;
                printf("%c", g->symbols[i].representation);
            }
        }
        
        // 2) Símbolos no terminales
        first = true;
        printf("\n\nNon-terminal symbols:\n\t");
        for (int i = 0; i < g->number_symbols; i++) {
            if (!g->symbols[i].terminal) {
                if (!first) printf(", ");
                first = false;
                printf("%c", g->symbols[i].representation);
            }
        }

        // 3) Símbolo inicial.
        printf("\n\nDistinguished symbol: %c", g->distinguished_symbol);

        // 4) Si la gramática es válida.
        // ... a little late for that

        // 5) Si la gramática es regular (en caso de que lo sea,
        //    si es regular a derecha o a izquierda).
        printf("\n\n");
        if (g->alignment == RIGHT_ALIGNED) {
            printf("Right-Aligned grammar");
        } else if (g->alignment == LEFT_ALIGNED) {
            printf("Left-Aligned grammar");
        } else {
            printf("Undefined alignment");
        }
        printf("\n\n");

        // 6) Un gráfico del automata finito equivalente.

        char filename[255], execCmd[255], pngFileName[50];
        memset(filename, 0, 255);
        strcpy(filename, argv[1]);
        strcpy(filename+len-2, "dot");
        int fileNameIndex = baseName(argv[1]) + 1;
        strcpy(pngFileName, argv[1] + fileNameIndex);
        int pngFileNameLen = strlen(pngFileName);
        strcpy(pngFileName + pngFileNameLen - 2, "png");

        FILE* file = fopen(filename, "w");
        struct grammar* aux;
        if (g->alignment != RIGHT_ALIGNED) {
            aux = as_right_normal_form(g); 
        }
        else{
            struct grammar * aux2 = take_out_unreachable(g);
            while(has_unproductive_productions(aux2)){
                aux = aux2;
                aux2 = take_out_unproductive_production(g);
                destroy_grammar(aux);
            }
            aux = aux2;
            
        }
        destroy_grammar(g);
        g = aux;
        
        automata_output(file, g);
        mkdir("output", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
        sprintf(execCmd, "dot -T png -o output/%s %s",
            pngFileName, filename
        );
        fclose(file);
        system(execCmd);
        destroy_grammar(g);
    }

    else {
        usage(argv[0]);
    }

    return 0;
}