home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume11 / dangle / part01 next >
Encoding:
Text File  |  1990-03-25  |  6.7 KB  |  146 lines

  1. Newsgroups: comp.sources.misc
  2. From: jjsc@inf.rl.ac.uk (John Cullen)
  3. Subject: v11i063: Program solution to the "missing comment terminator" problem!
  4. Message-Id: <8826@nfs4.rl.ac.uk>
  5. Date: 15 Mar 90 10:05:38 GMT
  6. Reply-To: jjsc@inf.rl.ac.uk ()
  7. Distribution: world
  8. Organization: Rutherford Appleton Laboratory, Informatics Department, U.K.
  9. Lines: 113
  10. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  11.  
  12. Posting-number: Volume 11, Issue 63
  13. Submitted-by: jjsc@inf.rl.ac.uk (John Cullen)
  14. Archive-name: dangle/part01
  15.  
  16.  
  17. I am posting this program on behalf of a friend without access to usenet news.
  18. I wasn't sure where I should post, however since there has been a lot of
  19. discussion of late about the problems of inadvertantly commenting out code
  20. (due to nested comments, loss of terminating */, etc) I thought that at the
  21. least it should go to comp.lang.c. Also, since it's of a general nature (and
  22. not all C programmers read comp.lang.c :-) I'm cross posting to c.sources.misc.
  23. [Cross-posting between moderated and unmoderated groups is not a good idea.
  24. I have not cross-posted to comp.lang.c.  ++bsa]
  25.  
  26. Any comments, suggestions, complaints (heaven forbid :-) to the author please,
  27. although if people have difficutly reaching him, I will forward messages for
  28. a while.
  29.  
  30. Enjoy,
  31. John.
  32.  
  33. PS. Please note that Barry cannot receive mail sent via the UUCP link at
  34. uk.ac.ukc - anyone sending mail via uucp, please send here and I'll forward.
  35.  
  36. --------------------------------  dangle.c  --------------------------------- 
  37. I have often encountered problems with mailing programs due to some mailers
  38. truncating long lines - in fact you may find this program suffers as it is my
  39. style to use the full screen width. The commonest problem is due to a close-
  40. comment symbol being corrupted, with the result that the compiled program is
  41. somewhat shortened and less than functional!
  42.  
  43. I wrote this little program to scan suspect files, and to report on possible
  44. problems. It's up to the user to check the output, and decide if there is a
  45. true problem. It checks for comments and strings which bridge lines etc.
  46. It is over zealous in reporting; but misses nothing in valid C, and some other
  47. languages also.
  48.  
  49. The technique used is that of a finite state machine, one that I have found to
  50. be very robust in the presence of invalid inputs; and one that recovers quickly.
  51. The original program was written for a Prime 9955, for which the CI compiler
  52. produces amazingly compact code at optimize 1 level. I must admit that I have
  53. fiddled with the source a bit, simply out of interest to see the effects.
  54.  
  55. Please feel free to use it as it stands, modify it, and use the technique for
  56. other purposes. If expanded, it could become a fast full C lexer. Have a go with
  57. it, and let me know how you got on.
  58.  
  59. Barry
  60.  
  61. ----------------------------------- cut here -----------------------------------
  62. #! /bin/sh
  63. # This file was wrapped with "dummyshar".  "sh" this file to extract.
  64. # Contents:  dangle.c
  65. echo extracting 'dangle.c'
  66. if test -f 'dangle.c' -a -z "$1"; then echo Not overwriting 'dangle.c'; else
  67. sed 's/^X//' << \EOF > 'dangle.c'
  68. X/* PROGRAM DANGLE: usage is: DANGLE <file_name> - no options . This program will
  69. X/* inspect C programs (and others) and report on comments which bridge several
  70. X/* lines, in case a closing quote has been omitted. It also checks strings and
  71. X/* quoted characters, and reports if they bridge lines, or a quote is missing.
  72. X/* The program operates as a finite state machine, using an enum state variable.
  73. X/* Author: GORMAN_B@UK.AC.LANCSP.P1   Date: February 15th 1990
  74. X
  75. X/******************************************************************************/
  76. X#include <stdio.h>                          /* standard i/o routines, EOF etc */
  77. X#ifndef __CI                                    /* test for Prime CI compiler */
  78. X#define short       int       /* the use of long and short in this program is */
  79. X#define long        int        /* optimum for CI, what about other compilers? */
  80. X#endif
  81. X#if EOF<0
  82. X#define NOT_EOF(c)  (c)>=0                 /* slightly better code generated  */
  83. X#define IS_EOF(c)   (c)<0                  /* by avoiding compare instruction */
  84. X#else
  85. X#define NOT_EOF(c)  (c)!=EOF             /* EOF should be -1, but may not be; */
  86. X#define IS_EOF(c)   (c)==EOF            /* so define something that will work */
  87. X#endif
  88. X#define case_NORMAL default /* save the compiler generating two instructions! */
  89. X/******************************************************************************/
  90. Xmain(argc,argv) int argc; char *argv[];   /* expects name of file to be given */
  91. X/******************************************************************************/
  92. X   {short copy=0; long chr, line=0, quote; char buffer[162], *p=buffer;
  93. X    FILE *input; enum {NORMAL,HYPER,QUOTED,SLASH,COMMENT,STAR} state=NORMAL;
  94. X
  95. X    if(argc!=2||!(input=fopen(argv[1], "r"))) exit(1);
  96. X
  97. X    printf("Checking file %snn", argv[1]);
  98. X
  99. X    do {*p++=chr=getc(input);
  100. X
  101. X        if(chr=='n'||IS_EOF(chr))
  102. X
  103. X           {short query=(state>=QUOTED); *p=0; p=buffer; line++;
  104. X
  105. X            if(query|copy) {copy=query; printf("%d: %s", line, p);}}
  106. X
  107. X        switch(state) {
  108. X/*----------------------------finite state machine----------------------------*/
  109. Xcase_NORMAL: if(chr=='"'||chr==''') {state=QUOTED; quote=chr; break;} /* "' */
  110. X             if(chr=='/') state=SLASH; break;  /* check if start of a comment */
  111. X
  112. Xcase HYPER:  state=QUOTED; break;  /* allow  to skip next char, including n */
  113. X
  114. Xcase QUOTED: if(chr==quote||chr=='n') {state=NORMAL; break;} /* note n trap */
  115. X             if(chr=='\') state=HYPER; break;       /* deal with  in quotes */
  116. X
  117. Xcase SLASH:  if(chr!='/') state=(chr=='*'?COMMENT:NORMAL); break;  /* //* etc */
  118. X
  119. Xcase COMMENT:if(chr=='*') state=STAR; break;      /* skip to *, n dealt with */
  120. X
  121. Xcase STAR:   if(chr!='*') state=(chr=='/'?NORMAL:COMMENT);}} /* deal with -> **/
  122. X/*----------------------------------------------------------------------------*/
  123. X    while(NOT_EOF(chr));
  124. X
  125. X    printf("n");}
  126. X
  127. X/************************************THE END***********************************/
  128. EOF
  129. chars=`wc -c < 'dangle.c'`
  130. if test $chars !=     2954; then echo 'dangle.c' is $chars characters, should be     2954 characters!; fi
  131. fi
  132. exit 0
  133.  
  134.  
  135. ---
  136. ===============================================================================
  137. John Cullen                     || JANET : jjsc@uk.ac.rl.inf
  138. System Support Group            || ARPA  : jjsc%inf.rl.ac.uk@nsfnet-relay.ac.uk
  139. Informatics Department          || BITNET: jjsc%uk.ac.rl.inf@ukacrl
  140. Rutherford Appleton Laboratory  || UUCP  : {...!mcvax}!ukc!rlinf!jjsc
  141. Chilton, Didcot, Oxon. OX11 0QX || VOICE : +44 (0)235 821900 ext 6555  
  142. ===============================================================================
  143. Your fortune cookie says:
  144. I'd give my right arm to be ambidextrous.
  145.  
  146.