/* Here is a lex program, that tries to find mismatched quotes in shellscripts. * It assumes you are NOT trying to do sneaky things like deliberately * make a quoted string span multiple lines, to get the newline * in the string * * Yes, this is awfully similar to the way an AWK prog is structured. * The main difference being that AWK works on a line-by-line basis. * Whereas lex works on a char-by-char basis. * See http://www.gnu.org/manual/flex-2.5.4/html_chapter/flex_toc.html * for more info on lex, and the GNU version called flex. * * Name this file "quotecheck.y" * Compile with "lex quotecheck.y; cc -o quotecheck lex.yy.c -ll" * It looks at stdin, obviously */ DOUBLEQUOTE ["] SINGLEQUOTE ['] BACKSLASH \\ %{ #define YY_MAIN 1 int dqcount=0,sqcount=0; %} %% {BACKSLASH}{BACKSLASH} ECHO; {BACKSLASH}{DOUBLEQUOTE} ECHO; {BACKSLASH}{SINGLEQUOTE} ECHO; if(sqcount==1) sqcount=0; {DOUBLEQUOTE} { ECHO; if(sqcount==0){ /*printf("[toggling DQ]");*/ dqcount=1-dqcount; } } {SINGLEQUOTE} { if(dqcount==0) { /*printf("[toggling SQ]");*/ sqcount=1-sqcount; } ECHO; } \n { ECHO; if(sqcount>0){ printf("***ERROR: unbalanced singlequote above.\n"); } if(dqcount>0){ printf("***ERROR: unbalanced doublequote above\n"); } dqcount=0;sqcount=0; } . ECHO; %% /* main() already defined by YY_MAIN */