Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdint.h>
- #include <string.h>
- #include <ctype.h>
- #include "pencil.h"
- /*
- * This function is called before anything else, to initialize the
- * state machine. It is certainly possible to create implementations
- * which don't require any initialization, so just leave this blank if
- * you don't need it.
- */
- int states[20][20];
- void
- init_transtab(void) {
- states[0]['s'] = 4;
- states[0]['t'] = 2;
- states[0]['o'] = 1;
- states[0]['f'] = 3;
- states[1]['n'] = 8;
- states[2]['w'] = 7;
- states[2]['h'] = 9;
- states[3]['f'] = 13;
- states[4]['t'] = 16;
- states[5]['e'] = 6;
- states[7]['o'] = 8;
- states[9]['n'] = 10;
- states[10]['e'] = 11;
- states[11]['e'] = 12;
- states[13]['w'] = 14;
- states[14]['r'] = 15;
- states[16]['o'] = 17;
- states[17]['p'] = 18;
- }
- command_t
- getCommandAtAcceptingState(int state) {
- switch (state) {
- case 6:
- return ONE;
- case 8:
- return TWO;
- case 12:
- return THREE;
- case 15:
- return FOUR;
- case 18:
- return STOP;
- default:
- return NULL;
- }
- }
- /*
- * Return the next token from reading the given input stream.
- * The words to be recognized are 'turn', 'draw' and 'move',
- * while the returned tokens may be TURN, DRAW, MOVE or END (as
- * enumerated in 'pencil.h').
- */
- command_t
- next(FILE *input) {
- int currentState = 0;
- while (getCommandAtAcceptingState(currentState) == NULL) {
- // Get next character
- int c = fgetc(input);
- // If we're at the End Of File, break out.
- if (c == EOF) break;
- if (isspace(c)) continue;
- // Make a move based on next character and current state.
- // currentState = getState(c, currentState);
- currentState = states[currentState][tolower(c)];
- }
- return getCommandAtAcceptingState(currentState);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement