-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_lexer.l
170 lines (151 loc) · 4.14 KB
/
code_lexer.l
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
%{
// c code
#include <string.h>
#include <ctype.h>
#include "code_executor.h"
char* code;
int size = 0;
int tags [BUFFER_SIZE];
char* function[BUFFER_SIZE];
int current_function;
int
strsplit (const char *str, char *parts[], const char *delimiter) {
char *pch;
int i = 0;
char *copy = NULL, *tmp = NULL;
copy = strdup(str);
if (! copy)
goto bad;
pch = strtok(copy, delimiter);
tmp = strdup(pch);
if (! tmp)
goto bad;
parts[i++] = tmp;
while (pch) {
pch = strtok(NULL, delimiter);
if (NULL == pch) break;
tmp = strdup(pch);
if (! tmp)
goto bad;
parts[i++] = tmp;
}
free(copy);
return i;
bad:
free(copy);
for (int j = 0; j < i; j++)
free(parts[j]);
return -1;
}
void concat_code(){
int len = strlen(yytext);
for(int i = 0; i < len; i++, size++){
if(size % BUFFER_SIZE == 0)
code = realloc(code, (size + BUFFER_SIZE) * sizeof code[0]);
code[size] = toupper(yytext[i]);
}
}
void concat_function(){
int len = strlen(yytext);
for(int i = 0; i < len; i++, size++){
if(size % BUFFER_SIZE == 0){
function[current_function]= realloc(function[current_function], (size + BUFFER_SIZE) * sizeof function[current_function][0]);
}
function[current_function][size] = toupper(yytext[i]);
}
}
void add_tag(){
char*num[2];
strsplit(yytext,num,"}");
tags[atoi(num[0]+1)-1] = size;
}
void add_goto(){
int len = strlen(yytext);
char*num[2];
strsplit(yytext,num,"}");
if(size % BUFFER_SIZE == 0)
code = realloc(code, (size + BUFFER_SIZE) * sizeof code[0]);
code[size++]='{';
for(int i = 1; i < len; i++, size++){
if(size % BUFFER_SIZE == 0)
code = realloc(code, (size + BUFFER_SIZE) * sizeof code[0]);
code[size] = yytext[i+4];
}
}
void begin_function(){
char*num[2];
strsplit(yytext,num,"]");
current_function = atoi(yytext+1)-1;
size = 0;
}
%}
%option noyywrap
%x codigo
%x comentario
%x funcion
%x funcion_comentario
digit [1-9][0-9]*
code [uldrjULDRJ]
%%
<comentario>. ;
<comentario>"\n" BEGIN(codigo);
<codigo>{code} concat_code();
<codigo>"\n" ;
<codigo>"\t" ;
<codigo>" " ;
<codigo>"{"{digit}"}" add_tag();
<codigo>"{goto"{digit}"}" add_goto();
<codigo>"//" BEGIN(comentario);
<codigo>"["{digit}"]:" begin_function();BEGIN(funcion);
<funcion>{code} concat_function();
<funcion>"\n" ;
<funcion>"\t" ;
<funcion>" " ;
<funcion>"["{digit}"]:" begin_function();
<funcion>"//" BEGIN(funcion_comentario);
<funcion_comentario>. ;
<funcion_comentario>"\n" BEGIN(funcion);
"//" BEGIN(comentario);
{code} BEGIN(codigo);concat_code();
"\n" ;
"\t" ;
" " ;
"{"{digit}"}" add_tag();BEGIN(codigo);
.|\n { fprintf(stderr, "Error al principio\n"); exit(1); }
<codigo>.|\n { fprintf(stderr, "Error en el código principal\n"); exit(2); }
<funcion>.|\n { fprintf(stderr, "Error en una función\n"); exit(3); }
%%
int main(void) {
current_function = -1;
size = 0;
code = malloc(BUFFER_SIZE);
yylex();
printf("#include <stdlib.h>\n"
"#define BUFFER_SIZE 1024\n"
"char* code= \"%s\";\n"
"char* function[BUFFER_SIZE];\n"
"int tags [BUFFER_SIZE];\n"
"void fill_functions(){\n", code);
for(int i = 0; i < BUFFER_SIZE; i++){
if(function[i] != NULL){
int fun_len = strlen(function[i]);
printf("function[%d]= malloc(%d * sizeof function[0]);\n",i, fun_len);
for(int j = 0; j <= fun_len;j++){
if(j < fun_len){
printf("function[%d][%d] = '%c';\n",i, j, function[i][j]);
} else {
printf("function[%d][%d] = %d;\n",i, j, 0);
}
}
}
}
printf("}\n");
printf("void fill_tags(){\n");
for(int i = 0; i < BUFFER_SIZE; i++){
if(tags[i] != NULL) {
printf("tags[%d] = %d;\n",i, tags[i]);
}
}
printf("}\n");
return 0;
}