Blob


1 /* Make this with: lex delatex.lex; cc lex.yy.c -ll -o delatex */
2 L [A-Za-z]
3 %Start Display Math Normal Tag
4 %%
5 <Normal>\' {yyleng--; yymore(); /* ignore apostrophes */}
6 <Normal>{L}+\\- {yyleng-=2; yymore(); /* ignore hyphens */}
7 <Normal>[a-z]/[^A-Za-z] ; /* ignore single letter "words" */
8 <Normal>[A-Z]+ ; /* ignore words all in uppercase */
9 <Normal>{L}+('{L}*)*{L} {printf("%s\n",yytext); /* any other letter seq is a word */}
10 <Normal>"%".* ; /* ignore comments */
11 <Normal>\\{L}+ ; /* ignore other control sequences */
12 <Normal>"\\begin{" BEGIN Tag; /* ignore this and up to next "}" */
13 <Normal>"\\bibitem{" BEGIN Tag;
14 <Normal>"\\bibliography{" BEGIN Tag;
15 <Normal>"\\bibstyle{" BEGIN Tag;
16 <Normal>"\\cite{" BEGIN Tag;
17 <Normal>"\\end{" BEGIN Tag;
18 <Normal>"\\include{" BEGIN Tag;
19 <Normal>"\\includeonly{" BEGIN Tag;
20 <Normal>"\\input{" BEGIN Tag;
21 <Normal>"\\label{" BEGIN Tag;
22 <Normal>"\\pageref{" BEGIN Tag;
23 <Normal>"\\ref{" BEGIN Tag;
24 <Tag>[^}] ; /* ignore things up to next "}" */
25 <Tag>"}" BEGIN Normal;
26 <Normal>[0-9]+ ; /* ignore numbers */
27 <Normal>"\\(" BEGIN Math; /* begin latex math mode */
28 <Math>"\\)" BEGIN Normal; /* end latex math mode */
29 <Math>.|\\[^)]|\n ; /* ignore anything else in latex math mode */
30 <Normal>"\\[" BEGIN Display; /* now in Latex display mode */
31 <Display>[^$]|\\[^\]] ; /* ignore most things in display math mode */
32 <Display>"\\]" BEGIN Normal; /* get out of Display math mode */
33 <Normal>\\. ; /* ignore other single character control sequences */
34 <Normal>\\\n ; /* more of the same */
35 <Normal>\n|. ; /* ignore anything else, a character at a time */
36 %%
37 #include <stdio.h>
38 #include <stdlib.h>
40 int
41 main(int argc, char **argv)
42 {
43 int i;
45 BEGIN Normal; /* Starts yylex off in the right state */
46 if (argc==1) {
47 yyin = stdin;
48 yylex();
49 }
50 else for (i=1; i<argc; i++) {
51 yyin = fopen(argv[i],"r");
52 if (yyin==NULL) {
53 fprintf(stderr,"can't open %s\n",argv[i]);
54 exit(1);
55 }
56 yylex();
57 }
58 exit(0);
59 }
60 int
61 yywrap(void)
62 {
63 return 1;
64 }