Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. #include "pencil.h"
  7.  
  8. /*
  9.  * This function is called before anything else, to initialize the
  10.  * state machine. It is certainly possible to create implementations
  11.  * which don't require any initialization, so just leave this blank if
  12.  * you don't need it.
  13.  */
  14.  
  15. int states[20][20];
  16.  
  17. void
  18. init_transtab(void) {
  19.     states[0]['s'] = 4;
  20.     states[0]['t'] = 2;
  21.     states[0]['o'] = 1;
  22.     states[0]['f'] = 3;
  23.     states[1]['n'] = 8;
  24.     states[2]['w'] = 7;
  25.     states[2]['h'] = 9;
  26.     states[3]['f'] = 13;
  27.     states[4]['t'] = 16;
  28.     states[5]['e'] = 6;
  29.     states[7]['o'] = 8;
  30.     states[9]['n'] = 10;
  31.     states[10]['e'] = 11;
  32.     states[11]['e'] = 12;
  33.     states[13]['w'] = 14;
  34.     states[14]['r'] = 15;
  35.     states[16]['o'] = 17;
  36.     states[17]['p'] = 18;
  37. }
  38.  
  39. command_t
  40. getCommandAtAcceptingState(int state) {
  41.     switch (state) {
  42.         case 6:
  43.             return ONE;
  44.         case 8:
  45.             return TWO;
  46.         case 12:
  47.             return THREE;
  48.         case 15:
  49.             return FOUR;
  50.         case 18:
  51.             return STOP;
  52.         default:
  53.             return NULL;
  54.     }
  55. }
  56.  
  57. /*
  58.  * Return the next token from reading the given input stream.
  59.  * The words to be recognized are 'turn', 'draw' and 'move',
  60.  * while the returned tokens may be TURN, DRAW, MOVE or END (as
  61.  * enumerated in 'pencil.h').
  62.  */
  63. command_t
  64. next(FILE *input) {
  65.  
  66.     int currentState = 0;
  67.  
  68.     while (getCommandAtAcceptingState(currentState) == NULL) {
  69.         // Get next character
  70.         int c = fgetc(input);
  71.  
  72.         // If we're at the End Of File, break out.
  73.         if (c == EOF) break;
  74.         if (isspace(c)) continue;
  75.  
  76.         // Make a move based on next character and current state.
  77.         // currentState = getState(c, currentState);
  78.         currentState = states[currentState][tolower(c)];
  79.     }
  80.  
  81.     return getCommandAtAcceptingState(currentState);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement