Files
TDT4205/ps1/src/table.c
2026-01-29 17:57:08 +01:00

77 lines
1.6 KiB
C

// The number of states in your table
#define NSTATES 14
// The starting state, at the beginning of each line
#define START 0
// The state to go to after a valid line
// All lines end with the newline character '\n'
#define ACCEPT 12
// The state to jump to as soon as a line is invalid
#define ERROR 13
int table[NSTATES][256];
void fillTable() {
// Make all states lead to ERROR by default
for (int i = 0; i < NSTATES; i++) {
for (int c = 0; c < 256; c++) {
table[i][c] = ERROR;
}
}
// Skip whitespace
table[7][' '] = 7;
// If we reach a newline, and are not in the middle of a statement, accept
table[7]['\n'] = ACCEPT;
// Accept the statement "go"
table[7]['g'] = 1;
table[1]['o'] = 2;
table[2]['\n'] = ACCEPT;
table[2][' '] = 7;
table[7]['d'] = 1;
table[1]['x'] = 2;
table[1]['y'] = 2;
table[2]['='] = 3;
table[3]['-'] = 4;
for (int i = '0'; i <= '9'; i++) {
table[3][i] = 5;
table[4][i] = 5;
table[5][i] = 5;
}
table[5]['\n'] = ACCEPT;
table[5][' '] = 7;
for (int i = '0'; i <= '9'; i++) {
table[START][i] = 6;
table[6][i] = 6;
}
table[6][':'] = 7;
table[START][' '] = START;
table[START]['\n'] = ACCEPT;
table[START]['g'] = 1;
table[START]['d'] = 1;
table[START]['/'] = 8;
table[2]['/'] = 8;
table[5]['/'] = 8;
table[7]['/'] = 8;
table[8]['/'] = 9;
for (int c = 0; c < 256; c++) {
table[9][c] = 9;
}
table[9]['\n'] = ACCEPT;
// TODO Expand the table to pass (and fail) the described syntax
// table[...][...] = ...
}