%{ #include "symtab.h" int texmode= 0; /* if (texmode) replace identifiers by defined TeX replacements */ FILE * outfile=NULL; #define MAXPATH 521 char outpath[MAXPATH]; int ignore = 0; #define PRINT(str) if (!ignore) fprintf(outfile?outfile:stdout,"%s",str) #define PRINTESC(c) if (!ignore) fprintf(outfile?outfile:stdout,"'\\%c'",c); #define PRINTIT PRINT(yytext) #define TAB PRINT("\t&") #define CR PRINT("\\cr\n"); #define PRINTLABEL print_label(yytext) void print_label(char *s) { char c; if (ignore) return; if (texmode) { node *n = lookup(s); if (n!=NULL && n->name!=NULL) { fprintf(outfile?outfile:stdout,"%s",n->name); return; } } while ((c=*s++)!=0) { if (c=='_') putchar('\\'); fputc(c,outfile?outfile:stdout); } } node * get(char *s) { node *n; n = lookup(s); if (n==NULL) n = insert(s); return n; } /* next we deal with tex replacement text */ #define MAXTEX 256 char texbuffer[MAXTEX]={0}; int texlen=0; int texlevel=0; void add_tex(char t) { if (t=='{') texlevel++; else if (t=='}' && texlevel>0) texlevel--; if (texlen0?BEGIN(state[--statesp]):fprintf(stderr,"Error Stack underflow\n")) %} %s OP ARG STRING EXTEND EXOP COMMENT DEF TEX TEXSKIP VERBATIM LONG IGNORE FILENAME SPACES [[:blank:]]+ ESCCHAR [_&$^{}\\~#%] LABEL [a-zA-Z:_][a-zA-Z0-9:_]*|[0-9]H NOLABEL [^a-zA-Z0-9:_\n] NOOP [^A-Z0-9\n] NOARG [^a-zA-Z0-9:_$#,&|()@~<>+*/'"\-\n] NOTAB [^&\n] %% /* Remember: PRINTIT is the default action */ .*\n BEGIN(INITIAL); /* lines enclosed in %%off %%on are for mmixal only*/ "%%off" ignore=1; "%%on"{SPACES}*\n ignore=0; "%%on"{SPACES} BEGIN(FILENAME); [^[:space:]]+ { if (outfile!=NULL) { if (strncmp(outpath,yytext,MAXPATH)!=0) { fclose(outfile); outfile=NULL; } } if (outfile==NULL) { outfile=fopen(yytext,"w"); if (outfile==NULL) fprintf(stderr,"Unable to open file %s\n",yytext); else { strncpy(outpath,yytext,MAXPATH); ignore=0; } } BEGIN(IGNORE); } /* lines ending in %%hide are for mmixal only */ .*\%\%hide{SPACES}?\n ; /* special tex commands starting with %%tex */ "%%tex"{SPACES} BEGIN(IGNORE); PUSH; BEGIN(DEF); {LABEL} def=get(yytext); BEGIN(TEXSKIP); [[:blank:]]* BEGIN(TEX); \n yyless(0); if (def!=NULL) set_content(def,get_tex(),NULL,0); POP; [[:space:]] { if (texlevel==0) { if (def!=NULL) set_content(def,get_tex(),NULL,0); POP;} else add_tex(yytext[0]); } . add_tex(yytext[0]); {LABEL} def=get(yytext); TAB; PRINTLABEL; {SPACES}"%%tex" BEGIN(COMMENT); PUSH; BEGIN(TEXSKIP); /* starting lines with %% makes them verbatim tex */ \%\% BEGIN(VERBATIM); {SPACES}?\n PRINTIT;BEGIN(INITIAL); /* starting lines with % makes them comment */ \% PRINT("\\omit\\mmsstrut&\\rm "); BEGIN(LONG); {SPACES}\% TAB;TAB; PRINT("\\rm "); BEGIN(LONG); \n PRINT("\\hidewidth\\cr\n");BEGIN(INITIAL); /* formating regular code */ ^{SPACES} TAB;TAB; BEGIN(OP); {SPACES} TAB; BEGIN(OP); {NOLABEL} TAB;TAB;TAB;TAB; BEGIN(COMMENT); {SPACES} TAB; BEGIN(ARG); {NOOP} yyless(0);TAB;TAB; BEGIN(COMMENT); {SPACES} TAB; BEGIN(COMMENT); {SPACES}?\% TAB; BEGIN(COMMENT); /* ignore % starting a comment */ {SPACES}?"\\hidewidth" PRINTIT; TAB; BEGIN(COMMENT); /*allow \hidewidth after (long) arguments */ {SPACES}?\; PRINTIT; BEGIN(EXTEND); \'{ESCCHAR}\' PRINTESC(yytext[1]); \'.\' PRINTIT; \" PRINTIT; BEGIN(STRING); {NOARG} yyless(0); TAB; BEGIN(COMMENT); [0-9]F PRINTIT; [0-9]B PRINTIT; {LABEL} PRINTLABEL; \" PRINTIT; BEGIN(ARG); {SPACES} PRINTIT; BEGIN(EXOP); {SPACES} PRINTIT; BEGIN(ARG); {ESCCHAR} PRINT("\\"); PRINTIT; {NOTAB}*\&{NOTAB}* PRINTIT; {NOTAB}* TAB; PRINTIT; \n CR;BEGIN(INITIAL); /* default rules: echo if not ignored */ <> CR; yyterminate(); <*>.|\n PRINTIT; %% int main(int argc, char * argv[]) { if (argc>1 && argv[1][0]=='-') { if (argv[1][1]=='t') texmode=1; } yylex(); if (outfile!=NULL) fclose(outfile); return 0; }