11
11
* paramlist -> paramdecl | paramlist , paramdecl
12
12
* paramdecl -> declaration-specifiers dcl
13
13
*
14
- * TODO: error checking is still finicky, e.g.: "int f(" will not recover; add
14
+ * TODO: error checking is still finicky, e.g.: "int f(char f() " will not recover; add
15
15
* type-qualifiers support.
16
16
*
17
17
* By Faisal Saadatmand
27
27
#define RECOVER while (tokentype != EOF && gettoken() != '\n')
28
28
#define SKIP_BLANKS (c ) while (((c) = getch()) == ' ' || (c) == '\t')
29
29
30
- enum { NAME , PARENS , BRACKETS };
30
+ enum { NAME , PARENS , BRACKETS , ERROR };
31
31
32
32
/* functions */
33
33
int gettoken (void );
@@ -119,25 +119,33 @@ void dirdcl(void)
119
119
120
120
if (tokentype == '(' ) { /* ( dcl ) */
121
121
dcl ();
122
- if (tokentype != ')' )
122
+ if (tokentype != ')' ) {
123
+ tokentype = ERROR ;
123
124
printf ("error: missing )\n" );
124
- } else if (tokentype == NAME ) /* variable name */
125
+ }
126
+ } else if (tokentype == NAME ) { /* variable name */
125
127
strcpy (name , token );
126
- else
128
+ } else {
129
+ tokentype = ERROR ;
127
130
printf ("error: expected name or (dcl)\n" );
128
-
129
- while ((type = gettoken ()) == PARENS || type == BRACKETS || type == '(' )
130
- if (type == PARENS )
131
- strcat (out , " function returning" );
132
- else if (type == '(' ) {
133
- strcat (out , " function accepts" );
134
- paramList ();
135
- strcat (out , " returning" );
136
- } else {
137
- strcat (out , " array" );
138
- strcat (out , token );
139
- strcat (out , " of" );
131
+ }
132
+
133
+ if (tokentype != ERROR ) {
134
+ while ((type = gettoken ()) == PARENS || type == BRACKETS || type == '(' )
135
+ if (type == PARENS )
136
+ strcat (out , " function returning" );
137
+ else if (type == '(' ) {
138
+ strcat (out , " function accepts" );
139
+ paramList ();
140
+ strcat (out , " returning" );
141
+ if (tokentype == ERROR )
142
+ break ;
143
+ } else {
144
+ strcat (out , " array" );
145
+ strcat (out , token );
146
+ strcat (out , " of" );
140
147
}
148
+ }
141
149
}
142
150
143
151
void paramdcl (void )
@@ -154,9 +162,10 @@ void paramList(void)
154
162
do {
155
163
if (gettoken () == '\n' ) {
156
164
printf ("error: parameters syntax\n" );
165
+ tokentype = ERROR ;
157
166
continue ;
158
167
}
159
- // gettoken();
168
+
160
169
if (tokentype == NAME ) {
161
170
sprintf (paramDataType , " %s" , token );
162
171
paramdcl ();
@@ -166,7 +175,7 @@ void paramList(void)
166
175
if (tokentype == ',' )
167
176
strcat (out , " and" );
168
177
}
169
- } while (tokentype != ')' );
178
+ } while (tokentype != ')' && tokentype != ERROR );
170
179
}
171
180
172
181
/* convert declaration to words */
@@ -178,10 +187,9 @@ int main(void)
178
187
if (EMPTY_LINE )
179
188
continue ; /* skip empty input lines */
180
189
dcl ();
181
- if (tokentype != '\n' ) {
190
+ if (tokentype != '\n' || tokentype == ERROR )
182
191
printf ("syntax error\n" );
183
- RECOVER ;
184
- } else
192
+ else
185
193
printf ("%s: %s %s\n" , name , out , datatype );
186
194
}
187
195
return 0 ;
0 commit comments