1. 151.
    0
    #include <stdlib.h>
    8. include <stdio.h>
    9. include <string.h>

    10. define pop -1
    11. define accept -2
    12. define error -3

    13. define alphabet 3 /* größe des eingabealphabets */

    14. define max_len 80
    /*
    ein einfacher kellerautomat ("pushdown automaton")

    symbol | ( | ) |
    ---+---+---+---
    state 0 | push 1 | error | accept
    state 1 | push 1 | pop | error
    • /

    int states[2][alphabet*2] =
    {
    {
    '(', 1 /* push 1 */,
    ')', error,
    '', accept
    },
    {
    '(', 1 /* push 1 */,
    ')', pop,
    '', error
    }
    };


    int main( int argc, char** argv )
    {
    int stack[100] = { 0 };
    int i = 0;
    int action = 0;
    int* tos = stack;
    char s [max_len+1];
    char* p = s;

    /* eingabestring */
    printf("bitte ausdruck eingeben: ");
    fgets(s, sizeof(s), stdin);
    s[strlen(s)-1]=''; /* newline von fgets durch binäre null ersetzen */


    /* start-state auf stack pushen */
    *(tos++) = 0;

    /* ausführungsschleife */
    do
    {
    /* aktion auf basis des eingabesymbols ermitteln */
    action = error;
    for( i = 0; i < alphabet; ++i )
    {
    if( states[*(tos-1)][i*2] == *p )
    {
    action = states[*(tos-1)][i*2+1];
    break;
    }
    }

    /* ausführen der aktionen */
    if( action

    error )
    {
    printf("unerwartetes zeichen an position %d", p-s);
    break;
    }
    else if( action

    accept )
    printf("ausdruck akzeptiert!");
    else if( action == pop )
    --tos;
    else
    *(tos++) = action;

    /* eingabe erweitern... */
    ++p;
    }
    while( action != accept );

    getchar();
    return 0;
    }
    ···
   tümünü göster