Advertisement
Lauda

C automat

Feb 5th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | None | 0 0
  1. /* C program koji analizira tekst */
  2.  
  3. #include <ctype.h>
  4. #include <stdio.h>
  5.  
  6. // tokeni
  7. #define _EOF             0
  8. #define _CAPITAL_WORD    1
  9. #define _WORD            2
  10.  
  11. // pomocne funkcije
  12. #define get_char()       getc(stdin)
  13. #define unget_char(c)    ungetc(c, stdin)
  14.  
  15. // stanja konacnog automata
  16. enum states{ST_START, ST_DOT, ST_CAPITAL, ST_CAPITAL_WORD, ST_SMALL, ST_WORD, ST_ERROR};
  17.  
  18. // bafer za prikupljanje znakova jedne rijeci
  19. char word[100];
  20.  
  21. int scanner() {
  22.     char ch;
  23.     int state = ST_START;
  24.     int line = 1;
  25.     int pos = 0;
  26.     word[0] = 0;
  27.  
  28.     while(1) {
  29.         switch(state) {
  30.             case ST_START : {
  31.                 ch = get_char();
  32.                 if (ch == ' ' || ch == '\t')
  33.                     state = ST_START;
  34.                 else if (ch == '\n') {
  35.                     line++;
  36.                     state = ST_START;
  37.                 }
  38.                 else if (ch == '.')
  39.                     state = ST_DOT;
  40.                 else if (isupper(ch)) {
  41.                     word[pos++] = ch; word[pos] = 0;
  42.                     state = ST_CAPITAL;
  43.                 }
  44.                 else if (islower(ch)) {
  45.                     word[pos++] = ch; word[pos] = 0;
  46.                     state = ST_SMALL;
  47.                 }
  48.                 else if (ch == EOF)
  49.                     return _EOF;
  50.                 else state = ST_ERROR;
  51.             }; break;
  52.  
  53.             case ST_DOT : {
  54.                     printf("\n.\t\t\t _DOT");
  55.                     state = ST_START;
  56.             }; break;
  57.  
  58.             case ST_CAPITAL : {
  59.                 ch = get_char();
  60.                 if (islower(ch)) {
  61.                     word[pos++] = ch; word[pos] = 0;
  62.                     state = ST_CAPITAL;
  63.                 }
  64.                 else
  65.                     state = ST_CAPITAL_WORD;
  66.             }; break;
  67.  
  68.             case ST_CAPITAL_WORD : {
  69.                     unget_char(ch);
  70.                     printf("\n%-24s _CAPITAL_WORD", word);
  71.                     word[pos = 0] = 0;
  72.                     state = ST_START;
  73.             }; break;
  74.  
  75.             case ST_SMALL : {
  76.                 ch = get_char();
  77.                 if (islower(ch)) {
  78.                     state = 4;
  79.                     word[pos++] = ch; word[pos] = 0;
  80.                 }
  81.                 else
  82.                     state = ST_WORD;
  83.             }; break;
  84.  
  85.             case ST_WORD : {
  86.                     unget_char(ch);
  87.                     printf("\n%-24s _WORD", word);
  88.                     word[pos = 0] = 0;
  89.                     state = ST_START;
  90.             }; break;
  91.  
  92.             case ST_ERROR : {
  93.                 printf("\nLEXICAL ERROR on character %c in line %d", ch, line);
  94.                 state = 0;
  95.             }; break;
  96.         }
  97.     }
  98. }
  99.  
  100. int main(void) {
  101.     scanner();
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement