-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex5_20.c
161 lines (141 loc) · 3.11 KB
/
ex5_20.c
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
/*
I have taken lots of libery with this exercise.
The exercise tells us to extend dcl so it can
handle functions with argument types and qualifiers.
This seems tedious. One would have to make the program
look for keywords to decide which path to take in the
program.
For this exercise, I've decided to concentrate on the
"so on" part of the exercise and have implemented a very
simple return value checker, so obviously incorrect decl-
arations such as "array of arrays" or "function returning
array" etc are pointed out as incorrect.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXTOKEN 100
enum { NAME, PARENS, BRACKETS };
void dcl(void);
void dirdcl(void);
int tokentype;
char token[MAXTOKEN];
char name[MAXTOKEN];
char datatype[MAXTOKEN];
char out[1000];
int gettoken(void)
{
int c;
char *p = token;
while ((c = getc(stdin)) == ' ' || c == '\t')
;
if (c == '(') {
if ((c = getc(stdin)) == ')') {
strcpy(token, "()");
return tokentype = PARENS;
}
else {
ungetc(c, stdin);
return tokentype = '(';
}
}
else if (c == '[') {
for (*p++ = c; (*p++ = getc(stdin)) != ']'; )
;
*p = '\0';
return tokentype = BRACKETS;
}
else if (isalpha(c)) {
*p++ = c;
while (isalnum(c = getc(stdin))) {
*p++ = c;
}
ungetc(c, stdin);
*p = '\0';
return tokentype = NAME;
}
else {
return tokentype = c;
}
}
void dcl(void)
{
int ns;
for (ns = 0; gettoken() == '*'; )
++ns;
dirdcl();
while (ns-- > 0)
strcat(out, "pointer to ");
}
void dirdcl(void)
{
int type, error = 0;
char temp[MAXTOKEN];
if (tokentype == '(') {
dcl();
if (tokentype != ')') {
error = 1;
printf("Error : Missing parentheses\n");
}
}
else if (tokentype == NAME)
strcpy(name, token);
else {
printf("Error : Expression is neither a name nor a '(dcl)'\n");
}
while ((error != 1) && ((type = gettoken()) == PARENS) || (type == BRACKETS)) {
if (type == PARENS)
strcat(out, "function returning ");
else if (type == BRACKETS) {
strcat(out, "array");
strcat(out, token);
strcat(out, " of ");
}
}
}
// returns a pointer to second word after the current word in out
char *two_words(char *p)
{
int space_counter = 0;
while (space_counter < 2) {
if (*p++ == ' ') {
++space_counter;
}
}
return p;
}
void output_return_checker()
{
char *p, *op;
p = out;
while (*p != '\0') {
if ((strncmp(p, "function", 8) == 0) ||
(strncmp(p, "array", 5) == 0)) {
op = two_words(p);
if ((strncmp(op, "pointer", 7) == 0) || (*op == '\0'))
printf("Seems to be a correct declaration\n");
else {
printf("Seems to be an incorrect declaration\n");
break;
}
}
++p;
}
}
int main(void)
{
while (gettoken() != EOF) {
if (tokentype == NAME) {
strcpy(datatype, token);
out[0] = name[0] = '\0';
dcl();
if (tokentype != '\n') {
printf("Syntax Error\n");
}
printf("%s : %s%s\n", name, out, datatype);
output_return_checker();
}
}
return 0;
}