Kevin P. Kleinfelter kpk at gitpyr.UUCP
Mon Sep 22 08:16:32 AEST 1986

A little while ago, I posted a request for a program to check for
possible confusion of "=" and "==".  Several people requested that
I post/mail such a program when found.

I received a very nice program which performed the required check, however
it also checked for consistent indentation (in a manner inconsistent with
my preferred style).

After a little thought, I wrote the following program.  It isn't very
smart, but it is about as simple a program as I can imagine to solve the

Cut the remainder of this message into the 3 files indicated.
Run "lex" on the ".l" file, and run "cc" on the ".c" file (and of course,
on lex's output).

Note: The version of lex that I use is public domain, and a little different
from Unix lex.  You may have to fiddle with "yyline" or other lex details to
get it to work properly.  If you have any problems, mail them to me, and I'll
see if I can help.

================= equalch.c =================================================
/* main program to check for use of "=" in a C program when "==" intended */
#include "equalchk.h"
extern int yyline;

main ()
   int token;
   int comment_in_progress = 0;
   int paren_nesting_level = 0;

   yyline = 1;
   while(token=yylex()) {
      switch(token) {
         case StartComment:
            if (comment_in_progress) {
               printf("%d WARNING: nested comment\n",yyline);
            comment_in_progress = 1;
         case EndComment:
            comment_in_progress = 0;
         case LeftParen:
            if (comment_in_progress) {
         case RightParen:
            if (comment_in_progress) {
            if (paren_nesting_level <= 0) {
               printf("%d ERROR: unmatched right parenthesis\n",yyline);
         case Equal:
            if (paren_nesting_level > 0) {
               printf("%d WARNING: possible misuse of \"==\"\n",yyline);
================ equalchk.h ================================================

/* defines for including by equalch.lxi for C ==/= checker */
#define StartComment 300
#define EndComment 301
#define LeftParen 302
#define RightParen 303
#define Equal 304

================ equalchk.l =================================================

#include "equalchk.h"
extern int yylval;
extern int yyline;
"/*"                    {yylval=StartComment;return(StartComment);}
"*/"                    {yylval=EndComment;return(EndComment);}
"("                     {yylval=LeftParen;return(LeftParen);}
")"                     {yylval=RightParen;return(RightParen);}
"=="                    {;}
"="                     {yylval=Equal;return(Equal);}
[\012]                  {yyline++;}
[\001-\177]             {;}

More information about the Comp.lang.c mailing list