#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 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)] = { 6, nothing },
[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 (;;) {
(state = table[T(state.state, sigilify(c))]).effect(c);
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnR5cGVkZWYgdm9pZCBlZmZlY3RvcihpbnQpOwp0eXBlZGVmIHN0cnVjdCB0cmFuc2l0aW9uIHRyYW5zaXRpb247CnN0cnVjdCB0cmFuc2l0aW9uIHsKICBpbnQgc3RhdGU7CiAgZWZmZWN0b3IgKmVmZmVjdDsKfTsKCmVudW0gc2lnaWwgeyBMZXR0ZXIsIFNsYXNoLCBTdGFyLCBFbmRPZkZpbGUsIFNxdW90ZSwgRHF1b3RlLCBCc2xhc2ggfTsKdHlwZWRlZiBlbnVtIHNpZ2lsIHNpZ2lsOwoKc2lnaWwgc2lnaWxpZnkoaW50IGMpIHsKICBzd2l0Y2ggKGMpIHsKICAgIGNhc2UgJy8nOiAgcmV0dXJuIFNsYXNoOwogICAgY2FzZSAnKic6ICByZXR1cm4g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