home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!crdgw1!rdsunx.crd.ge.com!bart!volpe
- From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
- Newsgroups: comp.lang.c
- Subject: Re: HELP with C syntax checking programm
- Keywords: c help braces { } fix syntax
- Message-ID: <1992Dec13.012016.261@crd.ge.com>
- Date: 13 Dec 92 01:20:16 GMT
- References: <Bz3pCy.380@access.digex.com>
- Sender: volpe@bart (Christopher R Volpe)
- Reply-To: volpe@ausable.crd.ge.com
- Distribution: all
- Organization: GE Corporate Research & Development
- Lines: 81
- Nntp-Posting-Host: bart.crd.ge.com
-
- In article <Bz3pCy.380@access.digex.com>, jhurwitz@access.digex.com (j. hurwitz) writes:
- |> Okay, I'm new to C so you'll have to forgive my lame question. Anyhoot,
- |> I'm trying to write a program that will check for balanced braces in my
- |> C source code. I hve no problem when I do this:
- |>
- |> =========================== Begin Hack =============================
- |> //programm to read a C source file and verify # of left to right braces
- |> #include <stdio.h>
- |>
- |> int main(int argc, char *argv[])
- |> {
- |> FILE *fptr;
- |> int l=0, r=0;
- |> char ch;
-
- Here is a problem, though it's not what's causing your problems. "Ch" must
- be declared an int, not a char, because that is what getc returns. The reason
- is that a char cannot hold all the characters AND a unique EOF value. So,
- depending on how your system implements chars (are they signed or unsigned?),
- either a valid character will be mistaken for EOF, or EOF will be mistaken
- for a valid char.
-
- |>
- |> fptr = fopen("brachek2.c","r");
- |>
- |> while ((ch =getc(fptr)) !=EOF) //gets chars
- |> {
- |> if(ch=='{') l++;
- |> if(ch=='}') r++;
- |> }
- |> if (l != r)
- |> printf("\nBraces do not match\n");
- |> else printf("\nBraces match !!!\n");
- |> fclose(fptr);
- |> return(0);
- |> }
- |> =========================== End Hack ===============================
- |>
- |> But, when I try this:
- |>
- |> ============================== Begin Hack ==========================
- |>
- |> //programm to read a C source file and verify # of left to right braces
- |> #include <stdio.h>
- |>
- |> int main(int argc, char *argv[])
- |> {
- |> FILE *fptr;
- |> int l=0, r=0;
- |> char ch;
- |>
- |> fptr = fopen("brachek3.c","r");
- |>
- |> while ((ch =getc(fptr)) !=EOF) //gets chars
- |> {
- |> switch (ch)
- |> {
- |> case '{': l++;
-
- Here's your real problem. You need a "break" separating the cases, otherwise
- execution will flow from one case right into the next.
-
- |> case '}': r++;
- |> }
- |> }
- |> printf("\nl= %d r= %d", l, r);
- |> if (l != r)
- |> printf("\nBraces do not match\n");
- |> else printf("\nBrace match !!!\n");
- |> fclose(fptr);
- |> }
- |> ================================ End Hack ==========================
- |> I get an uneven number of braces at the end. I can't figure out what
- |> I am doing wrong. I'm sure it is a minor oversight on my part. Any help
- |> would be greatly appreciated.
-
- --
- ==================
- Chris Volpe
- G.E. Corporate R&D
- volpecr@crd.ge.com
-