#include #include #include "tree.h" #include "bu-parser.h" #include "truth_table.h" struct lex_str_data { const char *str; size_t slen; size_t needle; uint32_t lttr_bitfield; }; struct chartoken lex_str(void *v) { struct lex_str_data *d = v; if (d->needle >= d->slen) { return (struct chartoken){.id = TOKEN_EOS, .c = '\0' }; } struct chartoken token; do { token.c = d->str[d->needle++]; enum token_id id = TOKEN_INVALID; if (token.c >= 'a' && token.c <= 'z') { token.id = TOKEN_VARIABLE; int alph_pos = token.c - 'a'; d->lttr_bitfield |= 1 << alph_pos; } else { switch (token.c) { case '!': token.id = TOKEN_UNARY_OPERATOR; break; case '*': case '+': token.id = TOKEN_BINARY_OPERATOR; break; case '(': token.id = TOKEN_OPEN_PAREN; break; case ')': token.id = TOKEN_CLOSE_PAREN; break; default: break; } } } while (token.c == ' '); return token; } int main(){ char str[100]; printf("type in your tree(no spaces please): "); scanf("%[^\n]", str); struct lex_str_data lxd = { .str = str, .slen = strlen(str), .needle = 0, .lttr_bitfield = 0, }; struct expr_parser ep = expr_parser_init(lex_str, &lxd); enum parser_state s = expr_parser_run(&ep); if (s == PARSER_ACCEPT) { printTree(ep.stack[0].expr, 0); } else { expr_parser_debug_print(stderr, &ep); return 1; } struct truth_table *tt = truth_table_create(ep.stack[0].expr, lxd.lttr_bitfield); printf( "%zu : %d : %s\n", tt->letters_count, tt->lines_count, tt->letters ); truth_table_fprint(stdout, tt); truth_table_destroy(tt); expr_parser_finish(&ep); return 0; }