Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Курсовая работа. Вариант 16 / kurs / LEXANLS
.CPP#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
char* tkw[14] = { // table key words
"void",
"main",
"print",
"scan",
"if",
"else",
"for",
"goto",
"const",
"int",
"float",
"fmatr",
"det",
"transpose"
};
char* tss1[17] = {"(", ")", "[", "]", "{", "}", "*", "/", "+", "-", ";", ",",
"=", "!", ">", "<", ":"};
char* tss2[4] = {">=", "<=", "==", "!="};
struct t_fun {
int q;
char* out;
};
struct el_t_id {
char id[11];
};
List<el_t_id> t_id;
List<int> t_i_const;
List<float> t_f_const;
t_fun delta[15][12] = // дгЄжЁп ЇҐаҐе®¤ Delta Є®Ґз®Ј® ЇаҐ®Ўа §®ў ⥫п
{
// 0 1 2 3 4 5 6 7 8 9 10 11
{{15, ""}, {1, "%c"}, {1, "%c"}, {2, "%c"}, {0, ""}, {0, "%c\n"}, {13, "%c\n"}, {3, "%c"}, {3, "%c"}, {5, "%c"}, {10, "%c"}, {4, "%c"}},
{{15, ""}, {1, "%c"}, {1, "%c"}, {1, "%c"}, {13, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {3, "\n%c"}, {11, "\n%c"}, {12, "\n%c"}, {15, ""}},
{{15, ""}, {15, ""}, {6, "%c"}, {2, "%c"}, {13, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {3, "\n%c"}, {11, "\n%c"}, {12, "\n%c"}, {14, "%c"}},
{{15, ""}, {1, "\n%c"}, {1, "\n%c"}, {2, "\n%c"}, {0, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {0, "%c\n"}, {5, "\n%c"}, {10, "\n%c"}, {15, ""}},
{{15, ""}, {15, ""}, {15, ""}, {8, "%c"}, {15, ""}, {15, ""}, {15, ""}, {15, ""}, {15, ""}, {15, ""}, {15, ""}, {15, ""}},
{{15, ""}, {1, "\n%c"}, {1, "\n%c"}, {2, "%c"}, {0, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {3, "\n%c"}, {15, ""}, {10, "\n%c"}, {4, "%c"}},
{{15, ""}, {15, ""}, {15, ""}, {7, "%c"}, {15, ""}, {15, ""}, {15, ""}, {15, ""}, {15, ""}, {9, "%c"}, {9, "%c"}, {15, ""}},
{{15, ""}, {15, ""}, {15, ""}, {7, "%c"}, {13, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {3, "\n%c"}, {11, "\n%c"}, {12, "\n%c"}, {15, ""}},
{{15, ""}, {15, ""}, {6, "%c"}, {8, "%c"}, {13, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {3, "\n%c"}, {11, "\n%c"}, {12, "\n%c"}, {15, ""}},
{{15, ""}, {15, ""}, {15, ""}, {7, "%c"}, {15, ""}, {15, ""}, {15, ""}, {15, ""}, {15, ""}, {15, "%c"}, {15, "%c"}, {15, ""}},
{{15, ""}, {1, "\n%c"}, {1, "\n%c"}, {2, "%c"}, {0, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {3, "\n%c"}, {5, "\n%c"}, {15, ""}, {4, "%c"}},
{{15, ""}, {1, "\n%c"}, {1, "\n%c"}, {2, "\n%c"}, {0, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {3, "\n%c"}, {15, ""}, {10, "\n%c"}, {4, "\n%c"}},
{{15, ""}, {1, "\n%c"}, {1, "\n%c"}, {2, "\n%c"}, {0, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {3, "\n%c"}, {5, "\n%c"}, {15, ""}, {4, "\n%c"}},
{{15, ""}, {1, "%c"}, {1, "%c"}, {2, "%c"}, {13, ""}, {0, "%c\n"}, {13, "%c\n"}, {3, "%c"}, {3, "%c"}, {11, "%c"}, {12, "%c"}, {4, "%c"}},
{{15, ""}, {15, ""}, {15, "%c"}, {8, "%c"}, {13, "\n"}, {0, "\n%c\n"}, {13, "\n%c\n"}, {3, "\n%c"}, {3, "\n%c"}, {11, "\n%c"}, {12, "\n%c"}, {15, ""}},
};
void out_tok(FILE* f, int n1, int n2)
{
fprintf(f, "%i %i \n", n1, n2);
}
int det_class(char ch)
{
if (ch > 64 && ch < 91 && (ch != 'E'))
return 1;
if (ch > 96 && ch < 123 && (ch != 'e'))
return 1;
if (ch == '_')
return 1;
if (ch == 'e' || ch == 'E')
return 2;
if (ch > 47 && ch < 58)
return 3;
if (ch == 32 || ch == 13 || ch == 10 || ch == EOF || ch == 9)
return 4;
if (ch == '(' || ch == '[' || ch == '{' || ch == '*' || ch == '/' ||
ch == ';' || ch == ',' || ch == ':')
return 5;
if (ch == ')' || ch == ']' || ch == '}')
return 6;
if (ch == '>' || ch == '<' || ch == '!')
return 7;
if (ch == '=')
return 8;
if (ch == '+')
return 9;
if (ch == '-')
return 10;
if (ch == '.')
return 11;
return 0;
}
void out_tables_bin()
{
FILE* out = fopen("tables.bin", "wb");
int sz = t_id.get_qntf();
fprintf(out, "%c%c", ((char*)&sz)[0], ((char*)&sz)[1]);
sz = t_i_const.get_qntf();
fprintf(out, "%c%c", ((char*)&sz)[0], ((char*)&sz)[1]);
int i, j;
for (i = 0; i < sz; i++)
for (j = 0; j < 2; j++)
fprintf(out, "%c", ((char*)&t_i_const[i])[j]);
sz = t_f_const.get_qntf();
fprintf(out, "%c%c", ((char*)&sz)[0], ((char*)&sz)[1]);
for (i = 0; i < sz; i++)
for (j = 0; j < 4; j++)
fprintf(out, "%c", ((char*)&t_f_const[i])[j]);
fclose(out);
}
void out_tables()
{
FILE* out = fopen("t_id_.txt", "w");
fprintf(out, "*** Identifiers table(3):\n\n");
fprintf(out, "N_ID: SYMBOL_VALUE:\n");
int i;
for (i = 0; i < t_id.get_qntf(); i++) {
fprintf(out, "%-7i %s\n", i, t_id[i].id);
}
fclose(out);
out = fopen("t_i_cons_.txt", "w");
fprintf(out, "*** Numeric integer constant table(4):\n\n");
fprintf(out, "N_CONST: VALUE:\n");
for (i = 0; i < t_i_const.get_qntf(); i++) {
fprintf(out, "%-9i %i\n", i, t_i_const[i]);
}
fclose(out);
out = fopen("t_f_cons_.txt", "w");
fprintf(out, "*** Numeric floating constant table(5):\n\n");
fprintf(out, "N_CONST: VALUE:\n");
for (i = 0; i < t_f_const.get_qntf(); i++) {
fprintf(out, "%-9i %f\n", i, t_f_const[i]);
}
fclose(out);
}
void coding_tokens()
{
char buf[64];
FILE* inp = fopen("token1.txt", "r");
FILE* out = fopen("token2.txt", "w");
FILE* out_b = fopen("token.bin", "wb");
int i;
int lex_n1, lex_n2;
while (fgets(buf, 21, inp) != NULL) {
buf[strlen(buf) -1] = 0;
lex_n1 = 0; // tkw
lex_n2 = -1;
for (i = 0; i < 14; i++) {
int g = strcmp(buf, tkw[i]);
if (g == 0) lex_n2 = i;
}
if (lex_n2 == -1) {
lex_n1 = 2; //tss2
lex_n2 = -1;
for (i = 0; i < 4; i++)
if (!strcmp(buf, tss2[i])) lex_n2 = i;
if (lex_n2 == -1) {
lex_n1 = 1; //tss1
lex_n2 = -1;
for (i = 0; i < 17; i++)
if (!strcmp(buf, tss1[i])) lex_n2 = i;
if (lex_n2 == -1) {
if (det_class(buf[0]) == 1 || det_class(buf[0]) == 2) {
lex_n1 = 3;
int id_exist = 0;
i = 0;
while (i < t_id.get_qntf() && (!id_exist))
if (!strcmp(buf, t_id[i++].id))
id_exist = 1;
if (id_exist)
lex_n2 = --i;
else {
el_t_id temp;
strcpy(temp.id, buf);
t_id.add(temp);
lex_n2 = i;
}
}
else {
int is_float = 0;
for (i = 0; i < strlen(buf); i++) {
if (buf[i] == '.' || buf[i] == 'e') is_float = 1;
}
int ti;
float tf;
if (!is_float) {
lex_n1 = 4;
ti = atoi(buf);
int const_exist = 0;
i = 0;
while (i < t_i_const.get_qntf() && (!const_exist))
const_exist = (t_i_const[i++] == ti);
if (const_exist)
lex_n2 = --i;
else {
t_i_const.add(ti);
lex_n2 = i;
}
}
else {
lex_n1 = 5;
tf = (float)atof(buf);
int const_exist = 0;
i = 0;
while (i < t_f_const.get_qntf() && (!const_exist))
const_exist = (t_f_const[i++] == tf);
if (const_exist)
lex_n2 = --i;
else {
t_f_const.add(tf);
lex_n2 = i;
}
}
}
}
}
}
out_tok(out, lex_n1, lex_n2);
fprintf(out_b, "%c%c%c%c", ((char*)&lex_n1)[0], ((char*)&lex_n1)[1],
((char*)&lex_n2)[0], ((char*)&lex_n2)[1]);
}
fclose(inp);
fclose(out);
fclose(out_b);
}
int main(int argc, char* argv[])
{
printf(":::‹ҐЄбЁзҐбЄЁ© «Ё§ в®а\n");
printf("(c) Copyright 2001, MK-Soft & S-Labs\n\n");
if (argc != 2) {
printf("ЏаЁ¬Ґп©вҐ д®а¬ в: lexanls.exe inp_file\n");
return 1;
}
FILE* inp = fopen(argv[1], "r");
if (inp == NULL) {
printf("Warning! FILE: %s NOT FOUND\n", argv[1]);
return 1;
}
FILE* out = fopen("token1.txt", "w");
int F[2] = {0, 13};
int n_f = 2;
int q_err = 15;
char cs; // Љ®Ґзл© ЇаҐ®Ўа §®ў ⥫м
char class_s;
int c_q = 0; // ’ҐЄг饥 б®бв®пЁҐ
int st_end = 0;
char e;
int err; // ЏаЁ§ Є ®иЁЎЄЁ
unsigned int cur_line = 1, cur_pos = 1;
do {
e = fscanf(inp, "%c", &cs);
if ((cs != 13) && (cs != 10))
cur_pos++;
else {
cur_pos = 1;
if (cs == 10) cur_line++;
}
if (e == EOF)
cs = EOF;
class_s = det_class(cs);
fprintf(out, delta[c_q][class_s].out, cs);
c_q = delta[c_q][class_s].q;
int k = 0;
st_end = 0;
err = (c_q == q_err);
while ((k != n_f) && (!st_end)) {
st_end = (c_q == F[k++]);
}
} while ((cs != EOF) && (!err));
fclose(inp);
fclose(out);
if (err) {
printf("ЋиЁЎЄ ў Їа®Ја ¬¬Ґ!\nline:%u pos:%u\n", cur_line, --cur_pos);
return 1;
}
else {
printf("OK!!!\nѓҐҐаЁагҐ¬лҐ д ©«л:\n");
printf("> token.bin - Ї®б«Ґ¤®ў ⥫м®бвм «ҐЄбҐ¬ ў ЎЁ ஬ д®а¬ вҐ\n");
printf("> token1.txt - Ї®б«Ґ¤®ў ⥫м®бвм «ҐЄбҐ¬\n");
printf("> token2.txt - Ї®б«Ґ¤®ў ⥫м®бвм § Є®¤Ёа®ў ле «ҐЄбҐ¬\n");
printf("> t_id_.txt - в Ў«Ёж Ё¤ҐвЁдЁЄ в®а®ў\n");
printf("> t_i_const_.txt - в Ў«Ёж 楫ле Є®бв в\n");
printf("> t_f_const_.txt - в Ў«Ёж ўҐйҐб⢥ле Є®бв в\n");
}
coding_tokens();
out_tables();
out_tables_bin();
return 0;
}
Соседние файлы в папке kurs