home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18279 < prev    next >
Encoding:
Internet Message Format  |  1992-12-12  |  2.9 KB

  1. Path: sparky!uunet!crdgw1!rdsunx.crd.ge.com!bart!volpe
  2. From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: HELP with C syntax checking programm
  5. Keywords: c help braces { } fix syntax
  6. Message-ID: <1992Dec13.012016.261@crd.ge.com>
  7. Date: 13 Dec 92 01:20:16 GMT
  8. References: <Bz3pCy.380@access.digex.com>
  9. Sender: volpe@bart (Christopher R Volpe)
  10. Reply-To: volpe@ausable.crd.ge.com
  11. Distribution: all
  12. Organization: GE Corporate Research & Development
  13. Lines: 81
  14. Nntp-Posting-Host: bart.crd.ge.com
  15.  
  16. In article <Bz3pCy.380@access.digex.com>, jhurwitz@access.digex.com (j. hurwitz) writes:
  17. |> Okay, I'm new to C so you'll have to forgive my lame question. Anyhoot,
  18. |> I'm trying to write a program that will check for balanced braces in my
  19. |> C source code. I hve no problem when I do this:
  20. |> 
  21. |> =========================== Begin Hack =============================
  22. |> //programm to read a C source file and verify # of left to right braces
  23. |> #include <stdio.h>
  24. |> 
  25. |> int main(int argc, char *argv[])
  26. |> {
  27. |>    FILE *fptr;
  28. |>    int l=0, r=0;
  29. |>    char ch;
  30.  
  31. Here is a problem, though it's not what's causing your problems. "Ch" must
  32. be declared an int, not a char, because that is what getc returns. The reason
  33. is that a char cannot hold all the characters AND a unique EOF value. So,
  34. depending on how your system implements chars (are they signed or unsigned?),
  35. either a valid character will be mistaken for EOF, or EOF will be mistaken
  36. for a valid char. 
  37.  
  38. |> 
  39. |>    fptr  = fopen("brachek2.c","r");
  40. |> 
  41. |>    while ((ch =getc(fptr)) !=EOF)      //gets chars
  42. |>    {
  43. |>    if(ch=='{') l++;
  44. |>    if(ch=='}') r++;
  45. |>    }
  46. |>    if (l != r)
  47. |>   printf("\nBraces do not match\n");
  48. |>    else printf("\nBraces match !!!\n");
  49. |>    fclose(fptr);
  50. |>    return(0);
  51. |> }
  52. |> =========================== End Hack ===============================
  53. |> 
  54. |> But, when I try this:
  55. |> 
  56. |> ============================== Begin Hack ==========================
  57. |> 
  58. |> //programm to read a C source file and verify # of left to right braces
  59. |> #include <stdio.h>
  60. |> 
  61. |> int main(int argc, char *argv[])
  62. |> {
  63. |>    FILE *fptr;
  64. |>    int l=0, r=0;
  65. |>    char ch;
  66. |> 
  67. |>    fptr  = fopen("brachek3.c","r");
  68. |> 
  69. |>    while ((ch =getc(fptr)) !=EOF)      //gets chars
  70. |>    {
  71. |>    switch (ch)
  72. |>      {
  73. |>      case '{':  l++; 
  74.  
  75. Here's your real problem. You need a "break" separating the cases, otherwise
  76. execution will flow from one case right into the next.
  77.  
  78. |>      case '}':  r++; 
  79. |>      }
  80. |>    }
  81. |>    printf("\nl= %d r= %d", l, r);
  82. |>    if (l != r)
  83. |>    printf("\nBraces do not match\n");
  84. |>    else printf("\nBrace match !!!\n");
  85. |>    fclose(fptr);
  86. |> }
  87. |> ================================ End Hack ==========================
  88. |> I get an uneven number of braces at the end. I can't figure out what
  89. |> I am doing wrong. I'm sure it is a minor oversight on my part. Any help
  90. |> would be greatly appreciated. 
  91.  
  92. -- 
  93. ==================
  94. Chris Volpe
  95. G.E. Corporate R&D
  96. volpecr@crd.ge.com
  97.