0%

用FSM实现cdecl

摘抄自《C语言专家编程》

  • 添加atoi()的头文件

  • 把this改成了thisss

  • 运行环境 gcc

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;

/*在第一个标识符(identifier)前保存所有的标记(token)*/
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)
{
/*读入下一个标记,保存在"thisss"中*/
char *p = thisss.string;
/*略过所有的空白字符*/
while((*p = getchar()) == ' ');
if (isalnum(*p))
{
/*在标识符中读入A-Z,1-9字符*/
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[])
/*用有限状态机实现的cdecl*/
{
/*在不同的状态中转换,直到指针为NULL*/
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");
}