home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / clint / syntax / syntax.c next >
Text File  |  1987-09-11  |  2KB  |  91 lines

  1. /* syntax.c Program running a quick syntax check on C source
  2.    code Version 1.11  J. Tomasik; created 05/23/87
  3.  
  4.    From BYTE magazine - September 1987
  5.  
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. main(argc, argv)
  11. int argc;
  12. char *argv[];
  13. {
  14.         FILE *fopen(), *infile;
  15.         char c;
  16.         int lbrace = 0, rbrace = 0;
  17.         int squote = 0, dquote = 0;
  18.         int lpar = 0, rpar = 0;
  19.         int lbrkt = 0, rbrkt = 0;
  20.         int bytecount = 0, errorcount = 0;
  21.  
  22.         if (argc != 2) {
  23.           printf("SYNTAX checker for C source code, version 1.11\n");
  24.           printf("Copyright (C) J. Tomadik 1987, 1988\n\n");
  25.           printf("Usage: syntax fname.ext \n");
  26.         exit(1);
  27.         }
  28. infile = fopen(argv[1],"r");
  29. if (infile == NULL) {
  30.         printf("Cannot open %s \n", argv[1]);
  31.         exit(2);
  32. }
  33.  
  34. while ((c = fgetc(infile)) != EOF) {
  35.         ++bytecount;
  36.         if (c == '{') ++lbrace;
  37.        else
  38.         if (c == '}') ++rbrace;
  39.        else
  40.         if (c == '\'') ++squote;
  41.        else
  42.         if (c == '\"') ++dquote;
  43.        else
  44.         if (c == '(') ++lpar;
  45.        else
  46.         if (c == ')') ++rpar;
  47.        else
  48.         if (c == '[') ++lbrkt;
  49.        else
  50.         if (c == ']') ++rbrkt;
  51. }
  52.  
  53. fclose(infile);
  54. printf("The file length is %d \n", bytecount);
  55.  
  56.  
  57. if (lbrace != rbrace) {
  58.         printf("There are %3d left and %3d right braces \n",
  59.                 lbrace, rbrace);
  60.         ++errorcount;
  61. }
  62.  
  63. if (lpar != rpar) {
  64.         printf("There are %3d left and %3d right parentheses \n",
  65.                 lpar, rpar);
  66.         ++errorcount;
  67. }
  68.  
  69. if (lbrkt != rbrkt) {
  70.         printf("There are %3d left and %3d right brackets \n",
  71.                 lbrkt,rbrkt);
  72.         ++errorcount;
  73. }
  74.  
  75. if (squote %2) {
  76.         printf("The single quote marks are not paired \n");
  77.         ++errorcount;
  78. }
  79.  
  80. if (dquote %2) {
  81.         printf("The double quote marks are not paired \n");
  82.         ++errorcount;
  83. }
  84.  
  85. if (errorcount == 0) {
  86.         printf("No error found, OK to compile \n");
  87. }
  88. else
  89.         exit(errorcount);
  90. }
  91.