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> 7 1732
El kroty wrote: 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.
My ancient copy of K&R only goes as high as 1-23,
so I don't know quite what you're trying to do. One of
your problems, though, is the topic of Question 12.1 in
the comp.lang.c Frequently Asked Questions (FAQ) list http://www.eskimo.com/~scs/C-faq/top.html
There may, of course, be other difficulties as well.
-- Er*********@sun.com El kroty wrote: 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.
Eric Sosman wrote: My ancient copy of K&R only goes as high as 1-23, so I don't know quite what you're trying to do. One of your problems, though, is the topic of Question 12.1 in the comp.lang.c Frequently Asked Questions (FAQ).
K&R2 ex 1-24: Write a program to check a C program for rudimentary syntax
errors like unbalanced parentheses, brackets and braces. Don't forget about
the quotes, both single and double, escape sequences, [sic] and comments.
(This program is hard to do in full generality.)
I think it's ironic that an exercise to detect rudimentary errors contains
the first English error I've read from K&R. ( A comma can precede the
terminal 'and' in a list or not, but the style MUST remain consistent.)
As far as getting this with or without a stack, my only contribution will be
to state the exercise clearly. MPJ
Merrill & Michele wrote: As far as getting this with or without a stack, my only contribution will be to state the exercise clearly.
#include "weak joke involving 'the state of the machine'"
Daniel Merrill & Michele wrote: As far as getting this with or without a stack, my only contribution
will be to state the exercise clearly.
"Daniel Fischer" wrote #include "weak joke involving 'the state of the machine'"
I made no joke, Junge; you're OT. MPJ
On Thu, 21 Oct 2004 14:52:09 UTC, El kroty <kr***@gawab.com> wrote: 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;
Bug!
getchar returns int not char.
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>
--
Tschau/Bye
Herbert
Visit http://www.ecomstation.de the home of german eComStation
Groovy hepcat Merrill & Michele was jivin' on Thu, 21 Oct 2004
11:36:38 -0500 in comp.lang.c.
Re: k&r Ex1-24 without stack's a cool scene! Dig it! Merrill & Michele wrote: > As far as getting this with or without a stack, my only contributionwill > be to state the exercise clearly.
"Daniel Fischer" wrote #include "weak joke involving 'the state of the machine'"
I made no joke, Junge; you're OT. MPJ
No, no, Daniel made the joke, and one alluding to the way (or *a*
way, at least) to do the parsing required to check for syntax errors.
And humour is always on topic. :)
--
Dig the even newer still, yet more improved, sig! http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
On Thu, 21 Oct 2004 11:52:09 -0300, El kroty <kr***@gawab.com> wrote: 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.
Aside from some minor points marked below and the (char)EOF issue
noted already -- which is only a problem on some platforms, and
obviously not yours or it would always hang -- I don't see any
problems. There are of course some errors you can make it doesn't
catch, but that's inherent in the stated problem/requirement. Can you
give a minimal or at least small test case on which it falsely reports
an error, or fails to report an error (you think) it should catch?
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;
Your variable 'comment' looks like it is trying to count nesting
level, but C comments don't nest and your state machine correctly
implements that, so the variable is essentially redundant.
case '"': case '\'': if (state == SCAPE) state = INQUOTE; else if (state == FREE) { state = INQUOTE; quotes++; } else if (state == INQUOTE) { state = FREE; quotes++; } break;
You won't catch a mismatch of "..' or '..". That is arguably not a
"rudimentary" error, but can be handled by a straightforward extension
of what you have already.
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");
You don't need to escape ' in a "string", although it is legal if you
want to. The only situation 'comment != 0' can detect given the state
machine above is a final open /* with no */, so the output might more
accurately be something like "Unclosed comment". And you could drop
the variable and just check state == COMMENT or == ASTERISK.
if ((quotes % 2) != 0) printf("Missing quotes.\n");
Again state == INQUOTE or == SCAPE would be as good.
if (parenth != 0) printf("Missing parentheses.\n"); if (bracket != 0) printf("Missing brackets.\n"); if (brace != 0) printf("Missing braces.\n");
I would describe these as "mismatched". It could be an open for which
the close is missing, or an "extra" or "stray" open that shouldn't be
there in the first place; or vice versa.
- David.Thompson1 at worldnet.att.net This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Collin VanDyck |
last post by:
I have a basic understanding of this, so forgive me if I am overly
simplistic in my explanation of my problem..
I am trying to get a Java/Xalan transform to pass through a numeric
character...
|
by: DrTebi |
last post by:
Hello,
I have the following problem:
I used to "encode" my email address within links, in order to avoid (most)
email spiders. So I had a link like this:
<a...
|
by: Thomas Scheffler |
last post by:
Hi,
I runned in trouble using XALAN for XSL-Transformation.
The following snipplet show what I mean:
<a href="http://blah.com/?test=test&test2=test2">Test1&</a>
<a...
|
by: johkar |
last post by:
When the output method is set to xml, even though I have CDATA around
my JavaScript, the operaters of && and < are converted to XML character
entities which causes errors in my JavaScript. I know...
|
by: Jim Bancroft |
last post by:
Hi all,
I'm writing an exception handler for one of my VB.Net methods and
wondered how best to dynamically put the class and method name in my message
string.
My code looks like this...
|
by: Jeremy |
last post by:
How can one stop a browser from converting
&
to
& ?
We have a textarea in our system wehre a user can type in some html code
and have it saved to the database. When the data is retireved...
|
by: Arne |
last post by:
A lot of Firefox users I know, says they have problems with validation
where the ampersand sign has to be written as & to be valid. I don't
have Firefox my self and don't wont to install it only...
|
by: InvalidLastName |
last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example:
// javascript
if (a > b)...
|
by: John Nagle |
last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally
hit content which has something like "Design & Advertising", that is,
an "&" instead of an "&". Is there some way I can get...
|
by: pbhandari |
last post by:
Hi,
I have following line in my Perl code-->
system("start my.exe myfile.ext") == 0 or die "error ($?) : $!";
Q1: How do I close my.exe?
Q2: If I give myfile.ex1 instead of myfile.ext,...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |