E
El kroty
Hi folks!! i'm trying to write k&r ex1-24 without a stack.
I almost have it but there's a bug that makes me crazy and
can't find it :'( whit some files it works and with others
it doesn't. Here is the code, any clues?? Thanks.
<code>
#include <stdio.h>
#include <stdlib.h>
enum states { FREE, INQUOTE, INCOMMENT, SLASH, ASTERISK, SCAPE };
int
main(void)
{
int quotes = 0;
int parenth = 0, bracket = 0, brace = 0, comment = 0;
char c;
enum states state = FREE;
while ((c = getchar()) != EOF) {
switch (c) {
case '/':
if (state == FREE)
state = SLASH;
else if (state == ASTERISK) {
comment--;
state = FREE;
}
break;
case '*':
if (state == INCOMMENT)
state = ASTERISK;
else if (state == SLASH) {
state = INCOMMENT;
comment++;
}
break;
case '"':
case '\'':
if (state == SCAPE)
state = INQUOTE;
else if (state == FREE) {
state = INQUOTE;
quotes++;
} else if (state == INQUOTE) {
state = FREE;
quotes++;
}
break;
case '(':
if (state == FREE)
parenth++;
break;
case ')':
if (state == FREE)
parenth--;
break;
case '[':
if (state == FREE)
bracket++;
break;
case ']':
if (state == FREE)
bracket--;
break;
case '{':
if (state == FREE)
brace++;
break;
case '}':
if (state == FREE)
brace--;
break;
case '\\':
if (state == INQUOTE)
state = SCAPE;
else if (state == SCAPE)
state = INQUOTE;
break;
default:
if (state == SLASH)
state = FREE;
else if (state == ASTERISK)
state = INCOMMENT;
else if (state == SCAPE)
state = INQUOTE;
}
}
if (comment != 0)
printf("Missing \'/*\' or \'*/\' in comment.\n");
if ((quotes % 2) != 0)
printf("Missing quotes.\n");
if (parenth != 0)
printf("Missing parentheses.\n");
if (bracket != 0)
printf("Missing brackets.\n");
if (brace != 0)
printf("Missing braces.\n");
exit(EXIT_SUCCESS);
}
</code>
I almost have it but there's a bug that makes me crazy and
can't find it :'( whit some files it works and with others
it doesn't. Here is the code, any clues?? Thanks.
<code>
#include <stdio.h>
#include <stdlib.h>
enum states { FREE, INQUOTE, INCOMMENT, SLASH, ASTERISK, SCAPE };
int
main(void)
{
int quotes = 0;
int parenth = 0, bracket = 0, brace = 0, comment = 0;
char c;
enum states state = FREE;
while ((c = getchar()) != EOF) {
switch (c) {
case '/':
if (state == FREE)
state = SLASH;
else if (state == ASTERISK) {
comment--;
state = FREE;
}
break;
case '*':
if (state == INCOMMENT)
state = ASTERISK;
else if (state == SLASH) {
state = INCOMMENT;
comment++;
}
break;
case '"':
case '\'':
if (state == SCAPE)
state = INQUOTE;
else if (state == FREE) {
state = INQUOTE;
quotes++;
} else if (state == INQUOTE) {
state = FREE;
quotes++;
}
break;
case '(':
if (state == FREE)
parenth++;
break;
case ')':
if (state == FREE)
parenth--;
break;
case '[':
if (state == FREE)
bracket++;
break;
case ']':
if (state == FREE)
bracket--;
break;
case '{':
if (state == FREE)
brace++;
break;
case '}':
if (state == FREE)
brace--;
break;
case '\\':
if (state == INQUOTE)
state = SCAPE;
else if (state == SCAPE)
state = INQUOTE;
break;
default:
if (state == SLASH)
state = FREE;
else if (state == ASTERISK)
state = INCOMMENT;
else if (state == SCAPE)
state = INQUOTE;
}
}
if (comment != 0)
printf("Missing \'/*\' or \'*/\' in comment.\n");
if ((quotes % 2) != 0)
printf("Missing quotes.\n");
if (parenth != 0)
printf("Missing parentheses.\n");
if (bracket != 0)
printf("Missing brackets.\n");
if (brace != 0)
printf("Missing braces.\n");
exit(EXIT_SUCCESS);
}
</code>