-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.c
208 lines (190 loc) · 4.77 KB
/
scanner.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "scanner.h"
#define TKNS 11
#define NEWFRM 250
#define HSTGRM_LINES 50
#define HSTGRM_SIZE 10
#define DEBUG 0
extern int yylex();
extern int yylineno;
extern char* yytext;
int yylinecounter = NEWFRM;
int yylinelastcount = 1;
int yylinecounter_H = HSTGRM_LINES;
int yylinelastcount_H = 1;
char* names[TKNS] = {"KEYWORD","IDENTIFIER","CONSTANTLITERAL","OPERATOR","PUNCTUATOR","COMMENT","PREPROCESSOR","CONSTANTCHAR","CONSTANTSTRING","ERROR","BLANK"};
char* colors[TKNS] = {"BurntOrange","Aquamarine","ForestGreen","Goldenrod","Fuchsia","Rhodamine","Gray","GreenYellow","Emerald","Red","White"};
char * commands[]={"gnuplot \"histogram_script.gnu\"","pdflatex beamer.tex","okular beamer.pdf --presentation"};
typedef struct table_row
{
int token;
char* text;
struct table_row * next;
}
Row;
Row getToken(void);
void writetoken(Row rowtoken, FILE* file);
void beginwritting(FILE* file);
void finishwritting(FILE* file);
void writetokens_dat(void);
Row* table[TKNS]; //symbol table 4 later
typedef struct
{
int token_count[TKNS];
}
fragment_info;
fragment_info* histogram;
int current_Hist_i; //index
int current_Hist_s; //size
FILE* file;
char* texsource = "source.tex";
int main(void)
{
histogram = malloc (HSTGRM_SIZE * sizeof(*histogram));
current_Hist_s = HSTGRM_SIZE;
#if DEBUG
for (int i=0;i<TKNS;i++)
printf(" n:%d ",histogram[0].token_count[i]);
#endif
file = fopen(texsource,"w");
Row rowtoken;
rowtoken = getToken();
beginwritting(file);
while(rowtoken.token)
{
writetoken(rowtoken,file);
rowtoken = getToken();
#if DEBUG
for (int i=0;i<TKNS;i++)
printf(" n:%d ",histogram[current_Hist_i].token_count[i]);
printf(" end line:%d\n", current_Hist_i);
#endif
}
finishwritting(file);
fclose(file);
writetokens_dat();
system(commands[0]); //gnuplot
system(commands[1]); //pdflatex
system(commands[2]); //okular
return 0;
}
void writetokens_dat(void)
{
file = fopen("datafile.dat","w");
fprintf(file, "x ");
for(int i=0;i<TKNS-1;i++)
fprintf(file,"%s ",names[i]);
fprintf(file, "\n");
for(int i=0;i<=current_Hist_i;i++)
{
fprintf(file,"%d-%d ",i*HSTGRM_LINES,i*HSTGRM_LINES+HSTGRM_LINES);
for(int a=0;a<=TKNS-2;a++)
{
fprintf(file,"%d ",histogram[i].token_count[a]);
}
fprintf(file,"\n");
}
fclose(file);
}
void extend_histogram()
{
#if DEBUG
for (int i=0;i<TKNS;i++)
printf(" n:%d ",histogram[current_Hist_i-1].token_count[i]);
#endif
fragment_info *tmp;
current_Hist_s=current_Hist_s+HSTGRM_SIZE;
tmp = realloc(histogram, current_Hist_s*sizeof(*histogram));
if (!tmp)
{
printf("failed to realloc");
exit(9); //failed to realloc
}
else
{
#if DEBUG
printf("extended hist to %d\n",current_Hist_s);
for (int i=0;i<TKNS;i++)
printf(" n:%d ",histogram[current_Hist_i].token_count[i]);
printf("^ new line of extended hist\n");
#endif
histogram = tmp;
//zero out the rows
for(int a=0;a<HSTGRM_SIZE;a++)
for(int i=0;i<TKNS;i++)
histogram[a+current_Hist_i].token_count[i]=0;
#if DEBUG
printf("extended hist to %d\n",current_Hist_s);
for (int i=0;i<TKNS;i++)
printf(" n:%d ",histogram[current_Hist_i].token_count[i]);
printf("^ new line of extended hist\n");
#endif
}
}
void beginwritting(FILE* file)
{
fprintf(file,"\\begin{frame}[fragile,allowframebreaks]{Syntax Highlighting}~");
}
void finishwritting(FILE* file)
{
fprintf(file,"\n\\end{frame}\n");
}
void writetoken(Row rowtoken,FILE* file)
{
if(yylineno>=(yylinelastcount+yylinecounter)) //break beamer frames cuz LaTeX sucks
{
fprintf
(
file,
"\n\\end{frame}\n\\begin{frame}[fragile,allowframebreaks]{Syntax Highlighting}~"
);
yylinelastcount=yylinelastcount+yylinecounter;
}
if(yylineno>=(yylinelastcount_H+yylinecounter_H)) //Histogram array index and realloc
{
current_Hist_i++;
#if DEBUG
printf("hist index ++:%d, yylineno:%d\n",current_Hist_i,yylineno);
#endif
if(current_Hist_i%HSTGRM_SIZE==0)
{
extend_histogram();
}
yylinelastcount_H=yylinelastcount_H+yylinecounter_H;
}
if(rowtoken.token==BLANK)
{
if(rowtoken.text[0]==0x9)
fprintf(file,"\\tab");
if(rowtoken.text[0]==0xa)
fprintf(file,"\\newline");
if(rowtoken.text[0]==' ')
fprintf(file," ");
}
if(rowtoken.token==COMMENT)
{
fprintf
(
file,
"\\color{%s}\\begin{verbatim}%s\\end{verbatim}\\leavevmode",
colors[rowtoken.token-1],
rowtoken.text
);
}
if(rowtoken.token!=COMMENT&&rowtoken.token!=BLANK)
{
fprintf(file,"\\color{%s}\\verb$%s$", colors[rowtoken.token-1],rowtoken.text);
}
/* important: increase the token count in respective histogram index */
histogram[current_Hist_i].token_count[rowtoken.token-1]++;
}
Row getToken(void)
{
Row myRow;
int token = yylex();
myRow.token = token;
myRow.text = yytext;
return myRow;
}