1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>
#define MAXTOKENS 100 #define MAXTOKENLEN 64
enum type_tag { IDENTIFIER,QUALIFIER,TYPE};
struct token { char type; char string[MAXTOKENLEN]; };
int top = -1;
struct token stack[MAXTOKENS];
struct token thisss;
#define pop stack[top--] #define push(s) stack[++top]=s
enum type_tag classify_string(void)
{ char *s = thisss.string; if (!strcmp(s,"const")) {strcpy(s,"read-only");return QUALIFIER;} if (!strcmp(s,"volatile")) {return QUALIFIER;} if (!strcmp(s,"void")) {return TYPE;} if (!strcmp(s,"char")) {return TYPE;} if (!strcmp(s,"signed")) {return TYPE;} if (!strcmp(s,"unsigned")) {return TYPE;} if (!strcmp(s,"short")) {return TYPE;} if (!strcmp(s,"int")) {return TYPE;} if (!strcmp(s,"long")) {return TYPE;} if (!strcmp(s,"float")) {return TYPE;} if (!strcmp(s,"double")) {return TYPE;} if (!strcmp(s,"struct")) {return TYPE;} if (!strcmp(s,"union")) {return TYPE;} if (!strcmp(s,"enum")) {return TYPE;} return IDENTIFIER; }
void gettoken(void) { char *p = thisss.string; while((*p = getchar()) == ' '); if (isalnum(*p)) { while(isalnum( *++p = getchar() ) ); ungetc(*p,stdin); *p = '\0'; thisss.type = classify_string(); return; } thisss.string[1] = '\0'; thisss.type = *p; return; }
void initialize(), get_array(), get_params(), get_lparen(), get_ptr_part(), get_type();
void (*nextstate)(void) = initialize;
int main(int argc, char const *argv[])
{ while(nextstate != NULL) { (*nextstate)(); return 0; } }
void initialize() { gettoken(); while(thisss.type != IDENTIFIER) { push(thisss); gettoken(); } printf("%s is",thisss.string); gettoken(); nextstate = get_array; }
void get_array() { nextstate = get_params; while(thisss.type == '[') { printf("array "); gettoken(); if (isdigit(thisss.string[0])) { printf("0..%d\n", atoi(thisss.string) - 1 ); gettoken(); } gettoken(); printf("of "); nextstate = get_lparen; } }
void get_params() { nextstate = get_lparen; if (thisss.type == '(') { while(thisss.type != ')') { gettoken(); } gettoken(); printf("function returning "); } }
void get_lparen() { nextstate = get_ptr_part; if (top >= 0) { if (stack[top].type == '(') { pop; gettoken(); nextstate = get_array; } } }
void get_ptr_part() { nextstate = get_type; if (stack[top].type == '*') { printf("pointer to "); pop; nextstate = get_lparen; } else if (stack[top].type == QUALIFIER) { printf("%s ", pop.string); nextstate = get_lparen; } }
void get_type() { nextstate = NULL; while(top >= 0) { printf("%s ", pop.string); } printf("\n"); }
|