Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* C program koji analizira tekst */
- #include <ctype.h>
- #include <stdio.h>
- // tokeni
- #define _EOF 0
- #define _CAPITAL_WORD 1
- #define _WORD 2
- // pomocne funkcije
- #define get_char() getc(stdin)
- #define unget_char(c) ungetc(c, stdin)
- // stanja konacnog automata
- enum states{ST_START, ST_DOT, ST_CAPITAL, ST_CAPITAL_WORD, ST_SMALL, ST_WORD, ST_ERROR};
- // bafer za prikupljanje znakova jedne rijeci
- char word[100];
- int scanner() {
- char ch;
- int state = ST_START;
- int line = 1;
- int pos = 0;
- word[0] = 0;
- while(1) {
- switch(state) {
- case ST_START : {
- ch = get_char();
- if (ch == ' ' || ch == '\t')
- state = ST_START;
- else if (ch == '\n') {
- line++;
- state = ST_START;
- }
- else if (ch == '.')
- state = ST_DOT;
- else if (isupper(ch)) {
- word[pos++] = ch; word[pos] = 0;
- state = ST_CAPITAL;
- }
- else if (islower(ch)) {
- word[pos++] = ch; word[pos] = 0;
- state = ST_SMALL;
- }
- else if (ch == EOF)
- return _EOF;
- else state = ST_ERROR;
- }; break;
- case ST_DOT : {
- printf("\n.\t\t\t _DOT");
- state = ST_START;
- }; break;
- case ST_CAPITAL : {
- ch = get_char();
- if (islower(ch)) {
- word[pos++] = ch; word[pos] = 0;
- state = ST_CAPITAL;
- }
- else
- state = ST_CAPITAL_WORD;
- }; break;
- case ST_CAPITAL_WORD : {
- unget_char(ch);
- printf("\n%-24s _CAPITAL_WORD", word);
- word[pos = 0] = 0;
- state = ST_START;
- }; break;
- case ST_SMALL : {
- ch = get_char();
- if (islower(ch)) {
- state = 4;
- word[pos++] = ch; word[pos] = 0;
- }
- else
- state = ST_WORD;
- }; break;
- case ST_WORD : {
- unget_char(ch);
- printf("\n%-24s _WORD", word);
- word[pos = 0] = 0;
- state = ST_START;
- }; break;
- case ST_ERROR : {
- printf("\nLEXICAL ERROR on character %c in line %d", ch, line);
- state = 0;
- }; break;
- }
- }
- }
- int main(void) {
- scanner();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement