home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18209 < prev    next >
Encoding:
Text File  |  1992-12-11  |  2.6 KB  |  106 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!digex.com!access.digex.com!lastort
  3. From: lastort@access.digex.com (Mike Lastort)
  4. Subject: Re: HELP with C syntax checking programm
  5. Message-ID: <lastort.724098713@access.digex.com>
  6. Keywords: c help braces { } fix syntax 
  7. Sender: usenet@access.digex.com
  8. Nntp-Posting-Host: access.digex.com
  9. Organization: Express Access Online Communications, Greenbelt, Maryland USA
  10. References: <Bz3pCy.380@access.digex.com>
  11. Distribution: all
  12. Date: Fri, 11 Dec 1992 18:31:53 GMT
  13. Lines: 91
  14.  
  15. jhurwitz@access.digex.com (j. hurwitz) writes:
  16.  
  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. >   fptr  = fopen("brachek2.c","r");
  32.  
  33. >   while ((ch =getc(fptr)) !=EOF)      //gets chars
  34. >   {
  35. >   if(ch=='{') l++;
  36. >   if(ch=='}') r++;
  37. >   }
  38. >   if (l != r)
  39. >  printf("\nBraces do not match\n");
  40. >   else printf("\nBraces match !!!\n");
  41. >   fclose(fptr);
  42. >   return(0);
  43. >}
  44. >=========================== End Hack ===============================
  45.  
  46. >But, when I try this:
  47.  
  48. >============================== Begin Hack ==========================
  49.  
  50. >//programm to read a C source file and verify # of left to right braces
  51. >#include <stdio.h>
  52.  
  53. >int main(int argc, char *argv[])
  54. >{
  55. >   FILE *fptr;
  56. >   int l=0, r=0;
  57. >   char ch;
  58.  
  59. >   fptr  = fopen("brachek3.c","r");
  60.  
  61. >   while ((ch =getc(fptr)) !=EOF)      //gets chars
  62. >   {
  63. >   switch (ch)
  64. >     {
  65. >     case '{':  l++; 
  66. >     case '}':  r++; 
  67. >     }
  68. >   }
  69. >   printf("\nl= %d r= %d", l, r);
  70. >   if (l != r)
  71. >   printf("\nBraces do not match\n");
  72. >   else printf("\nBrace match !!!\n");
  73. >   fclose(fptr);
  74. >}
  75. >================================ End Hack ==========================
  76. >I get an uneven number of braces at the end. I can't figure out what
  77. >I am doing wrong. I'm sure it is a minor oversight on my part. Any help
  78. >would be greatly appreciated. 
  79.  
  80.  
  81. Ok, the problem is that when using a switch case, you need to break out,
  82. or else you "fall through" to the next case. In other words, your code:
  83.  
  84.  
  85.     switch (ch)
  86.       {
  87.        case '{': l++;
  88.       case '}': r++;
  89.       }
  90.  
  91. will increment BOTH l and r if ch == '{', and increments r if ch == '}'.
  92. You should do something like:
  93.  
  94.  
  95.     switch (ch)
  96.       {
  97.       case '{': l++; break;
  98.       case '}': r++;
  99.       }
  100.  
  101. That way, if ch == '{', l gets incremented, then the next character is
  102. examined. 
  103.  
  104. Mike
  105.  
  106.