|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | +#include <ctype.h> |
| 4 | + |
| 5 | +#define MAXTOKEN 100 |
| 6 | + |
| 7 | +enum { NAME, PARENS, BRACKETS }; |
| 8 | +enum { NO, YES }; |
| 9 | + |
| 10 | +void dcl(void); |
| 11 | +void dirdcl(void); |
| 12 | + |
| 13 | +int gettoken(void); |
| 14 | +int tokentype; /* type of last token */ |
| 15 | +char token[MAXTOKEN]; /* last token string */ |
| 16 | +char name[MAXTOKEN]; /* indetifier name */ |
| 17 | +char datatype[MAXTOKEN]; /* data type = char, int, etc. */ |
| 18 | +char out[1024]; /* output string */ |
| 19 | +int prevtoken = NO; /* to indicate if there was a previous token */ |
| 20 | + |
| 21 | +/* undcl: convert word description to declaration */ |
| 22 | +int main() { |
| 23 | + int type; |
| 24 | + char temp[MAXTOKEN]; |
| 25 | + |
| 26 | + while (gettoken() != EOF) { /* 1st token on line */ |
| 27 | + strcpy(out, token); /* is the datatype */ |
| 28 | + while ((type = gettoken()) != '\n') |
| 29 | + if (type == PARENS || type == BRACKETS) |
| 30 | + strcat(out, token); |
| 31 | + else if (type == '*') { |
| 32 | + int nexttype = gettoken(); |
| 33 | + prevtoken = YES; |
| 34 | + if (nexttype != PARENS && nexttype != BRACKETS) |
| 35 | + sprintf(temp, "*%s", out); |
| 36 | + else |
| 37 | + sprintf(temp, "(*%s)", out); |
| 38 | + strcpy(out, temp); |
| 39 | + } else if (type == NAME) { |
| 40 | + sprintf(temp, "%s %s", token, out); |
| 41 | + strcpy(out, temp); |
| 42 | + } else |
| 43 | + printf("invalid input at %s\n", token); |
| 44 | + printf("%s\n", out); |
| 45 | + } |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +/* dcl: parse a declarator */ |
| 50 | +void dcl(void) { |
| 51 | + int ns; |
| 52 | + |
| 53 | + for (ns = 0; gettoken() == '*'; ) /* count *'s */ |
| 54 | + ns++; |
| 55 | + dirdcl(); |
| 56 | + while (ns-- > 0) |
| 57 | + strcat(out, " pointer to"); |
| 58 | +} |
| 59 | + |
| 60 | +/* dirdcl: parse a direct declarator */ |
| 61 | +void dirdcl(void) { |
| 62 | + int type; |
| 63 | + |
| 64 | + if (tokentype == '(') { /* ( dcl ) */ |
| 65 | + dcl(); |
| 66 | + if (tokentype != ')') |
| 67 | + printf("error: missing )\n"); |
| 68 | + } else if (tokentype == NAME) /* variable name */ |
| 69 | + strcpy(name, token); |
| 70 | + else { |
| 71 | + printf("error: expected name or (dcl)\n"); |
| 72 | + prevtoken = YES; |
| 73 | + } |
| 74 | + while ((type = gettoken()) == PARENS || type == BRACKETS) |
| 75 | + if (type == PARENS) |
| 76 | + strcat(out, " function returning"); |
| 77 | + else { |
| 78 | + strcat(out, " array"); |
| 79 | + strcat(out, token); |
| 80 | + strcat(out, " of"); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +int gettoken(void) /* return next token */ { |
| 85 | + int c, getch(void); |
| 86 | + void ungetch(int); |
| 87 | + char *p = token; |
| 88 | + |
| 89 | + if (prevtoken == YES) { |
| 90 | + prevtoken = NO; |
| 91 | + return tokentype; |
| 92 | + } |
| 93 | + |
| 94 | + while((c = getch()) == ' ' || c == '\t') |
| 95 | + ; |
| 96 | + if (c == '(') { |
| 97 | + if ((c = getch()) == ')') { |
| 98 | + strcpy(token, "()"); |
| 99 | + return tokentype = PARENS; |
| 100 | + } else { |
| 101 | + ungetch(c); |
| 102 | + return tokentype = '('; |
| 103 | + } |
| 104 | + } else if (c == '[') { |
| 105 | + for (*p++ = c; (*p++ = getch()) != ']'; ) |
| 106 | + ; |
| 107 | + *p = '\0'; |
| 108 | + return tokentype = BRACKETS; |
| 109 | + } else if (isalpha(c)) { |
| 110 | + for (*p++ = c; isalnum(c = getch()); ) |
| 111 | + *p++ = c; |
| 112 | + *p = '\0'; |
| 113 | + ungetch(c); |
| 114 | + return tokentype = NAME; |
| 115 | + } else |
| 116 | + return tokentype = c; |
| 117 | +} |
0 commit comments