Skip to content

Instantly share code, notes, and snippets.

@nnathan
Created October 30, 2019 06:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnathan/d2b73e9201b329fc55ac779fb7b41c56 to your computer and use it in GitHub Desktop.
Save nnathan/d2b73e9201b329fc55ac779fb7b41c56 to your computer and use it in GitHub Desktop.
freenode ##c caze K&R C exercise 1.24 example from https://ideone.com/94U6aJ
#include <stdio.h>
#include <stdlib.h>
typedef void effector(int);
typedef struct transition transition;
struct transition {
int state;
effector *effect;
};
enum sigil { Letter, Slash, Star, EndOfFile, Squote, Dquote, Bslash };
typedef enum sigil sigil;
sigil sigilify(int c) {
switch (c) {
case '/': return Slash;
case '*': return Star;
case EOF: return EndOfFile;
case '\'': return Squote;
case '"': return Dquote;
case '\\': return Bslash;
default: return Letter;
}
}
void emit (int c) { putchar(c); }
void slash (int c) { putchar('/'); putchar(c); }
void nothing (int c) { }
void halt (int c) { exit(EXIT_SUCCESS); }
#define T(state, sigil) (((state) << 4) | sigil)
int main() {
transition state = { 0, emit };
transition table[] = {
[T(0, Slash)] = { 1, nothing },
[T(0, Letter)] = { 0, emit },
[T(0, Star)] = { 0, emit },
[T(0, EndOfFile)] = { 0, halt },
[T(0, Squote)] = { 6, emit },
[T(0, Dquote)] = { 4, emit },
[T(0, Bslash)] = { 0, emit },
[T(1, Slash)] = { 0, slash },
[T(1, Letter)] = { 0, slash },
[T(1, Star)] = { 2, nothing },
[T(1, EndOfFile)] = { 0, halt },
[T(1, Squote)] = { 0, slash },
[T(1, Dquote)] = { 0, slash },
[T(1, Bslash)] = { 0, slash },
[T(2, Slash)] = { 2, nothing },
[T(2, Letter)] = { 2, nothing},
[T(2, Star)] = { 3, nothing },
[T(2, EndOfFile)] = { 0, halt },
[T(2, Squote)] = { 2, nothing },
[T(2, Dquote)] = { 2, nothing },
[T(2, Bslash)] = { 2, nothing },
[T(3, Slash)] = { 0, nothing },
[T(3, Letter)] = { 2, nothing },
[T(3, Star)] = { 2, nothing },
[T(3, EndOfFile)] = { 0, halt },
[T(3, Squote)] = { 2, nothing },
[T(3, Dquote)] = { 2, nothing },
[T(3, Bslash)] = { 2, nothing },
[T(4, Slash)] = { 4, emit },
[T(4, Letter)] = { 4, emit },
[T(4, Star)] = { 4, emit },
[T(4, EndOfFile)] = { 0, halt },
[T(4, Squote)] = { 4, emit },
[T(4, Dquote)] = { 0, emit },
[T(4, Bslash)] = { 5, emit },
[T(5, Slash)] = { 4, emit },
[T(5, Letter)] = { 4, emit },
[T(5, Star)] = { 4, emit },
[T(5, EndOfFile)] = { 0, halt },
[T(5, Squote)] = { 4, emit },
[T(5, Dquote)] = { 4, emit },
[T(5, Bslash)] = { 4, emit},
[T(6, Slash)] = { 8, emit },
[T(6, Letter)] = { 8, emit },
[T(6, Star)] = { 8, emit },
[T(6, EndOfFile)] = { 0, halt },
[T(6, Squote)] = { 8, emit },
[T(6, Dquote)] = { 8, emit },
[T(6, Bslash)] = { 7, emit },
[T(7, Slash)] = { 8, emit },
[T(7, Letter)] = { 8, emit },
[T(7, Star)] = { 8, emit },
[T(7, EndOfFile)] = { 8, emit },
[T(7, Squote)] = { 8, emit },
[T(7, Dquote)] = { 8, emit },
[T(7, Bslash)] = { 8, emit },
[T(8, Slash)] = { 0, emit },
[T(8, Letter)] = { 0, emit },
[T(8, Star)] = { 0, emit },
[T(8, EndOfFile)] = { 0, halt },
[T(8, Squote)] = { 0, emit },
[T(8, Dquote)] = { 0, emit },
[T(8, Bslash)] = { 0, emit },
};
for (;;) {
int c = getchar();
(state = table[T(state.state, sigilify(c))]).effect(c);
}
}
@nnathan
Copy link
Author

nnathan commented Oct 30, 2019

transitiontable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment