fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef void effector(int);
  5. typedef struct transition transition;
  6. struct transition {
  7. int state;
  8. effector *effect;
  9. };
  10.  
  11. enum sigil { Letter, EndOfFile };
  12. typedef enum sigil sigil;
  13.  
  14. sigil sigilify(int c) {
  15. switch (c) {
  16. case EOF: return EndOfFile;
  17. default: return Letter;
  18. }
  19. }
  20.  
  21. void push (int c);
  22. void pop (int c);
  23. void swap (int c);
  24. void halt (int c);
  25.  
  26. #define T(state, sigil) (((state) << 1) | sigil)
  27.  
  28. int main() {
  29. transition state = { 0, push };
  30. transition table[] = {
  31. [T(0, Letter)]={ 1, push }, [T(0, EndOfFile)]={ 0, halt },
  32. [T(1, Letter)]={ 0, swap }, [T(1, EndOfFile)]={ 0, pop },
  33. };
  34.  
  35. for (;;) {
  36. int c = getchar();
  37. (state = table[T(state.state, sigilify(c))]).effect(c);
  38. }
  39. }
  40.  
  41. int b;
  42.  
  43. void push (int c) { b = c; }
  44. void pop (int c) { putchar(b); }
  45. void swap (int c) { putchar(c); pop(0); }
  46. void halt (int c) { exit(EXIT_SUCCESS); }
  47.  
  48.  
Success #stdin #stdout 0s 9424KB
stdin
DEADBEEF
stdout
EDDAEBFE