kenny@uiucdcsb.cs.uiuc.edu (11/07/86)
I recently had occasion to write a C++ program using lex(1) as its front end; I discovered that lex produces an output file that generates many, many warnings when fed into CC. I quickly hacked up the following awk script that makes lex's output into legal C++, and also changes it to use stream I/O rather than stdio. NOTE: Most of the warnings were for unreachable code generated in the yylex() function; this in turn was caused by the fact that the actions I supplied were all of the form, ``return (value);''. My fix simply removes all the lex-generated ``break'' statements; if you have action procedures that do not break or return, you *must* put your own break statements in them: e.g., /[ \t\n]/ break; and not /[ \t\n]/ ; If this behavior is undesirable, remove the three lines /^break;/ { delete = 1; } from the script. ---------- cut here ----- File: lexfix.awk ----- Awk script for lex in C++ BEGIN { delete = 0; } /^#[ \t]*include[ \t][ \t]*"stdio\.h"/ { print "# include <stream.h>"; delete = 1; } /^#[ \t]*define[ \t][ \t]*input/ { print "# define input(c) input_(c)" delete = 1; } /^#[ \t]*define[ \t][ \t]*output/ { print "# define output(c) output_(c)" delete = 1; } /^*[ \t]*define[ \t][ \t]*ECHO/ { print "# define ECHO ((*yyout) << yytext)"; delete = 1; } /^FILE[ \t]*\*yyin/ { print "istream *yyin = &cin; ostream *yyout = &cout;"; delete = 1; } /^yylex\(\)/ { print "int input_ () {"; print " char c;" print " if (yysptr > yysbuf) c = U(*--yysptr);"; print " else if (!((*yyin).get(c))) c = '\\0';"; print " if (c == '\\n') ++yylineno;"; print " return c;"; print " }"; print "inline void output_ (char c) {"; print " (*yyout) << chr(c);"; print " }"; print "extern int yylook ();"; print "extern int yywrap ();"; print "static int yyback (int*, int);"; } /^yyback\(p, m\)/ { print "static int yyback (int *p, int m)\n"; delete = 2; } /^yyoutput\(c\)/ { print "void yyoutput (int c) {"; delete = 2; } /^yyunput\(c\)/ { print "void yyunput (int c) {"; delete = 2; } /^break;/ { delete = 1; } /fprintf\(yyout,"bad switch yylook %d",nstr\);/ { print "cerr << \"bad switch yylook %d\" << nstr;"; delete = 1; } { if (!delete) print $0; else --delete; }