-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlatex2html.y
248 lines (196 loc) · 7.57 KB
/
latex2html.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
%{ /* ISI@CAL */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int yydebug = 1;
FILE *newHtml ;
int mathflag = 0;
int tablewidth = 0;
int inhtml=0,inbody=0;
%}
%union
{
char* arr;
int val;
}
%start latexstatement
%token DBLBS BACKSL LCURLYB RCURLYB END PIPE HLINE AMPERSAND
%token SPECCHAR LSQRB RSQRB LBEGIN SECTION TABLE SUPERSCRIPT LANGLE RANGLE
%token TABULAR VSPACE B C H L BEGINTABULAR ENDTABULAR
%token T R DOCUMENTCLASS SUBSCRIPT DOLLARMATH BEGINTABLE ENDTABLE
%token ARTICLE PROC LETTER TITLE LBEGINDOCU LENDDOCU STARTBIB ENDBIB
%token SQRT FRAC BOLDFACE ITALICS WS HSPACE SWORD STARTLIST ENDLIST ITEM BIBITEM
%token <arr> DATE
%token <arr> AUTHOR
%type <arr> operand
%token <arr> WORD
%token <arr> GREEK
%token <arr> LETTERS
%token <arr> OPERATOR
%token <val> INTEGER
%%
latexstatement : documenttype{fprintf(newHtml,"<html><head> <title> \n"); inhtml=1;} titletype author date LBEGINDOCU{fprintf(newHtml,"<body>\n");inbody=1; } mainbody {fprintf(newHtml,"</body>\n"); } LENDDOCU {fprintf(newHtml,"</html> \n"); }
;
author : AUTHOR LCURLYB{fprintf(newHtml,"<i> Author:: "); } formattedtextoption RCURLYB{fprintf(newHtml,"</i></br> "); }
|
;
date : DATE LCURLYB{fprintf(newHtml,"<i> Date:: "); } formattedtextoption RCURLYB{fprintf(newHtml,"</i></br><hr> "); }
|
;
ignore :SWORD LCURLYB bword RCURLYB LSQRB bword RSQRB ignore
| SWORD LSQRB bword RSQRB LCURLYB bword RCURLYB ignore
|SWORD LCURLYB bword RCURLYB ignore
| LCURLYB bword RCURLYB ignore
| SWORD LCURLYB bword RCURLYB
| LCURLYB bword RCURLYB
| SWORD ignore
|
;
listoption : STARTLIST {fprintf(newHtml,"<ol> "); } listitem ENDLIST {fprintf(newHtml,"</ol> "); }
|
;
listitem : ITEM{fprintf(newHtml,"<li> "); } listoption formattedtextoption {fprintf(newHtml,"</li> "); } listitem
|
;
bword : bword WORD
| bword INTEGER
| bword ignore
| WORD
| INTEGER
| LETTERS
| bword mathoption
|
;
bibliography: STARTBIB LCURLYB textoption RCURLYB{fprintf(newHtml,"<hr> <b>The References</b></br><ol> "); } biblist ENDBIB {fprintf(newHtml,"</ol> "); }
;
biblist: BIBITEM LSQRB bword RSQRB LCURLYB bword RCURLYB {fprintf(newHtml,"<li> "); } formattedtextoption {fprintf(newHtml,"</li> "); } biblist
|
;
documenttype : DOCUMENTCLASS LCURLYB type RCURLYB ignore
| DOCUMENTCLASS LSQRB bword RSQRB LCURLYB type RCURLYB ignore
;
type : ARTICLE
|PROC
|LETTER
;
titletype : TITLE LCURLYB formattedtextoption RCURLYB {fprintf(newHtml,"</title>\n"); }
|{fprintf(newHtml,"</title>\n");}
;
mainbody : mainbody mainoption
| mainoption
;
mainoption : formattedtextoption
| tableoption
| mathoption
| listoption
|bibliography
|section
;
formattedtextoption: BOLDFACE LCURLYB {fprintf(newHtml,"<b>\n");} formattedtextoption RCURLYB {fprintf(newHtml,"</b>\n");} formattedtextoption
| ITALICS LCURLYB {fprintf(newHtml,"<i>\n");} formattedtextoption RCURLYB {fprintf(newHtml,"</i>\n");} formattedtextoption
| ignore textoption ignore
;
section: SECTION LCURLYB{fprintf(newHtml,"<hr><b>\n");} formattedtextoption RCURLYB {fprintf(newHtml,"</b></br>\n");}
;
mathoption : DOLLARMATH {fprintf(newHtml," <math>\n");} mathstatement {fprintf(newHtml,"</mrow>\n"); } DOLLARMATH {fprintf(newHtml,"</math>\n");}
;
set : BACKSL LCURLYB {fprintf(newHtml,"<mi>{</mi>"); } setmath BACKSL RCURLYB {fprintf(newHtml,"<mi>}</mi>"); }
| LANGLE {fprintf(newHtml,"<mi> [ </mi>"); } setmath RANGLE {fprintf(newHtml,"<mi> ] </mi>"); }
;
mathstatement : operand OPERATOR {fprintf(newHtml,"\n<mo>%s</mo>\n",$2); } mathstatement
| SWORD mathstatement
|LSQRB mathstatement RSQRB
| squareroot
| fractional
| operand mathstatement
| set
| operand OPERATOR {fprintf(newHtml,"\n<mo>%s</mo>\n",$2); } set
| operand
| SWORD
;
setmath : operand OPERATOR {fprintf(newHtml,"\n<mo>%s</mo>\n",$2); } setmath
| SWORD setmath
| operand setmath
| operand
| SWORD
;
fractional : FRAC {fprintf(newHtml,"<mrow><mfrac>\n");} fracbody {fprintf(newHtml,"</mfrac></mrow>\n");}
;
fracbody : fracpart fracpart
;
fracpart : LCURLYB {fprintf(newHtml,"<mrow>\n"); } mathstatement {fprintf(newHtml,"</mrow>\n"); } RCURLYB
;
squareroot : SQRT {fprintf(newHtml,"<msqrt>\n"); } LCURLYB mathstatement RCURLYB {fprintf(newHtml,"</msqrt>\n"); }
;
operand : LETTERS SUPERSCRIPT {fprintf(newHtml,"<mrow><msup>\n<mi>%s</mi>",$1); } operand {fprintf(newHtml,"</msup></mrow>");}
| INTEGER SUPERSCRIPT {fprintf(newHtml,"<mrow><msup>\n<mn>%d</mn>",$1); } operand {fprintf(newHtml,"</msup></mrow>");}
| LETTERS SUBSCRIPT {fprintf(newHtml,"<mrow><msub>\n<mi>%s</mi>",$1); } operand {fprintf(newHtml,"</msub></mrow>");}
| INTEGER SUBSCRIPT {fprintf(newHtml,"<mrow><msub>\n<mn>%d</mn>",$1); } operand {fprintf(newHtml,"</msub></mrow>");}
| GREEK SUPERSCRIPT {fprintf(newHtml,"<mrow><msup>\n<mi>%s;</mi>",$1); } operand {fprintf(newHtml,"</msup></mrow>");}
| GREEK SUBSCRIPT {fprintf(newHtml,"<mrow><msub>\n<mi>%s;</mi>",$1); } operand {fprintf(newHtml,"</msub></mrow>");}
| LETTERS {fprintf(newHtml,"<mi>%s</mi>",$1); }
| GREEK {fprintf(newHtml,"<mi>%s;</mi>",$1); }
|INTEGER {fprintf(newHtml,"<mn>%d</mn>",$1); }
;
tableoption : starttable tablebody endtable
| tablebody
;
starttable : BEGINTABLE LSQRB position RSQRB
;
position : T
| H
| B
;
tablebody : starttabular tabularbody endtabular tablebody
| starttabular tabularbody endtabular
;
starttabular : BEGINTABULAR LCURLYB tablespec RCURLYB {fprintf(newHtml,"<table border=\"%d\">\n",tablewidth); }
;
endtabular : ENDTABULAR {fprintf(newHtml,"</table>\n"); tablewidth = 0; }
;
endtable : ENDTABLE
;
tabularbody : hline {fprintf(newHtml,"<tr>\n<td> "); } row tabularbody
| hline
;
hline : HLINE hline
|
;
row : tabletextoption AMPERSAND {fprintf(newHtml," </td> <td>"); } row
| tabletextoption DBLBS {fprintf(newHtml," </td>\n</tr>\n"); }
;
tablespec : tablespec colspec
| colspec
;
colspec : L
| C
| R
| PIPE {tablewidth = 1;}
;
tabletextoption : tabletextoption WORD {fprintf(newHtml," %s",$2);}
| tabletextoption INTEGER {fprintf(newHtml," %d",$2);}
| WORD {fprintf(newHtml,"%s",$1); }
| INTEGER {fprintf(newHtml,"%d",$1); }
|mathoption;
;
textoption : textoption WORD {fprintf(newHtml," %s",$2);}
| textoption INTEGER {fprintf(newHtml," %d",$2);}
| WORD {fprintf(newHtml,"%s",$1); }
| INTEGER {fprintf(newHtml,"%d",$1); }
| textoption DBLBS {fprintf(newHtml,"<br/>"); }textoption
| textoption HSPACE {fprintf(newHtml," "); }
|
;
%%
int main(int argc, char *argv[]){
char fname[100];
strcpy(fname,argv[1]);
newHtml = fopen(fname,"w+");
return yyparse();
}
int yyerror (char *msg) {
if((inhtml==1)&&(inbody==0))
fprintf(newHtml,"ERROR in TITLE</title></html>");
if((inbody==1)&&(inbody==1))fprintf(newHtml," ERROR in BODY</body></html>");
return fprintf (stderr, "YACC: %s\n ", msg);
}