aboutsummaryrefslogtreecommitdiff
path: root/parts/table.h
blob: fc63488b7da91bdd8f73d6c30ec9b326ce1f39c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef TABLE_H
#define TABLE_H

extern struct action {
    enum action_type {
        ACTION_NOT_SET = 0, ACTION_SHIFT,
        ACTION_GOTO, ACTION_REDUCE,
        ACTION_ACCEPT
    } type;
    size_t arg;
} **table;

extern size_t table_states;

extern int (*table_fill)(); // non-zero on fail
extern void (*table_free)();

void table_print();
void table_print_cstyle();

#include "symbol.h"

void table_print()
{
    printf("    ");
    for(size_t sym = 0; sym < total_symbols; sym++) printf("%2zu  ", sym);
    printf("\n");

    char action_to_char[] = {[ACTION_SHIFT] = 's', [ACTION_REDUCE] = 'r', [ACTION_GOTO] = 'g'};
    for(size_t i = 0; i < table_states; i++) {
        printf("%2zu  ", i);
        for(size_t sym = 0; sym < total_symbols; sym++)
            if(table[i][sym].type == ACTION_ACCEPT) printf(" a  ");
            else if(table[i][sym].type) printf("%c%-2zu ", action_to_char[table[i][sym].type], table[i][sym].arg);
            else printf("    ");
        printf("\n");
    }


}

void table_print_cstyle()
{
    for(size_t i = 0; i < table_states; i++) {
        printf("(struct action[]){");
        for(size_t sym = 0; sym < total_symbols; sym++)
            printf("{%d, %zu},", table[i][sym].type, table[i][sym].arg);
        printf("},\n");
    }
}

#endif