Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
dcl
char buf; #include <stdio.h> int getch(void) { char c; if(buf) { c = buf; buf = 0; } else c = getchar(); return c; } #include <assert.h> void ungetch(int c) { assert(buf != 0); buf = c; } #include <string.h> #include <ctype.h> #define MAXTOKEN 100 enum { NAME, PARENS, BRACKETS }; void dcl(void); void dirdcl(void); int gettoken(void); int tokentype; /* тип последней лексемы */ char token[MAXTOKEN]; /* текст последней лексемы */ char name[MAXTOKEN]; /* имя */ char datatype[MAXTOKEN]; /* тип = char, int и т.д. */ char out[1000]; /* выдаваемый текст */ /* преобразование декларации в словесное описание */ main() { while (gettoken() != EOF) { /* 1-я лексема в строке */ strcpy(datatype, token); /* это тип данных */ out[0] = '\0'; dcl(); /* разбор остальной части строки */ if (tokentype != '\n') printf("синтаксическая ошибка\n"); printf("%s: %s %s\n", name, out, datatype); } return 0; } /* dcl: разбор декларатора */ void dcl(void) { int ns; for (ns = 0; gettoken() == '*'; ) /* подсчет звездочек */ ns++; dirdcl(); while (ns-- > 0) strcat(out, " указ. на"); } /* dirdcl: разбор непосредственного декларатора */ void dirdcl(void) { int type; if (tokentype == '(') { /* (dcl) */ dcl(); if (tokentype != ')') printf(" ошибка: пропущена )\n"); } else if (tokentype == NAME) /* имя переменной */ strcpy(name, token); else printf("ошибка: должно быть name или (dcl)\n"); while ((type = gettoken()) == PARENS || type == BRACKETS) if (type == PARENS) strcat(out, " функц. возвр."); else { strcat(out, " массив"); strcat(out, token); strcat(out, " из"); } } /* возвращает следующую лексему */ int gettoken(void) { int c, getch(void); void ungetch(int); char *p = token; while ((c = getch()) == ' ' || c == '\t') ; if (c == '(') { if ((c = getch()) == ')') { strcpy(token, "()"); return tokentype = PARENS; } else { ungetch(c); return tokentype = '('; } } else if (c == '[') { for (*p++ = c; (*p++ = getch()) != ']'; ) ; *p ='\0'; return tokentype = BRACKETS; } else if (isalpha(c)) { for (*p++ = c; isalnum(c = getch()); ) *p++ = c; *p = '\0'; ungetch(c); return tokentype = NAME; } else return tokentype = c; }
run
|
edit
|
history
|
help
0
QuickSort
Official Hello world
3.1.2 Matrix add/sub file concept
Lab 7 blackjack v0.9
Sheldon 9 area none aliens as aliens and farms Lockheed Kelly and lame lead district 9
use_of_bool_in_loop
Kous
ARREGLO DE VARIABLES DE TIPO STRUCT
WAP in C to print the reverse of a string
22