Κωδικοποίηση

#include <stdio.h>

/* States the system can be in */
enum states {
        NORMAL,
        HASH,
        SLASH,
        COMMENT,
        STAR,
        STRING,
        BACKSLASH_STRING,
        CHARACTER,
        BACKSLASH_CHARACTER,
        NEWLINE,
};

/* Current state */
int state = NORMAL;

/* Current level of indent (tabs to print at the beginning of a line) */
int indent_level = 0;

/*
 * Print tabs to indent the following character by indent_level
 */
void
indent(void)
{
        int i;

        for (i = 0; i < indent_level; i++)
                putchar('\t');
}

/*
 * State processing functions start here
 */
void
comment(char c)
{
        putchar(c);
        if (c == '*')
                state = STAR;
}

void
slash(char c)
{
        putchar(c);
        if (c == '*')
                state = COMMENT;
        else
                state = NORMAL;
}

void
string(char c)
{
        putchar(c);
        if (c == '"')
                state = NORMAL;
        else if (c == '\\')
                state = BACKSLASH_STRING;
}

void
backslash_string(char c)
{
        putchar(c);
        state = STRING;
}


void
character(char c)
{
        putchar(c);
        if (c == '\'')
                state = NORMAL;
        else if (c == '\\')
                state = BACKSLASH_CHARACTER;
}

void
backslash_character(char c)
{
        putchar(c);
        state = CHARACTER;
}

void
hash(char c)
{
        putchar(c);
        if (c == '\n')
                state = NORMAL;
}

void
normal(char c)
{
        putchar(c);
        switch (c) {
        case '#':
                state = HASH;
                break;
        case '/':
                state = SLASH;
                break;
        case '\'':
                state = CHARACTER;
                break;
        case '"':
                state = STRING;
                break;
        case '{':
                indent_level++;
                break;
        case '\n':
                state = NEWLINE;
                break;
        }
}

void
star(char c)
{
        putchar(c);
        if (c == '/')
                state = NORMAL;
        else
                state = COMMENT;
}

void
newline(char c)
{
        switch (c) {
        case ' ':
        case '\n':
        case '\t':
                break;
        case '}':
                indent_level--;
                /* FALLTRHOUGH */
        default:
                indent();
                state = NORMAL;
                normal(c);
                break;
        }
}

/*
 * Process a single character.
 * Call the appropriate state handling function
 */
void
process(char c)
{
        switch (state) {
        case NORMAL:
                normal(c);
                break;
        case COMMENT:
                comment(c);
                break;
        case HASH:
                hash(c);
                break;
        case SLASH:
                slash(c);
                break;
        case STAR:
                star(c);
                break;
        case STRING:
                string(c);
                break;
        case BACKSLASH_STRING:
                backslash_string(c);
                break;
        case NEWLINE:
                newline(c);
                break;
        case CHARACTER:
                character(c);
                break;
        case BACKSLASH_CHARACTER:
                backslash_character(c);
                break;
        }
}

main()
{
        int c;

        while ((c = getchar()) != EOF)
                process(c);
}