Skip to content

Instantly share code, notes, and snippets.

@libbkmz
Last active December 17, 2015 23:39
Show Gist options
  • Select an option

  • Save libbkmz/5690711 to your computer and use it in GitHub Desktop.

Select an option

Save libbkmz/5690711 to your computer and use it in GitHub Desktop.
#include "header.h"
int main(int argc, char *argv[])
{
char ch;
int i=0,j=0,razm=0,ind=0,fl=0,fl2=0;
int l=0;
fp=fopen(argv[1],"r");
fp2=fopen(argv[2],"w");
fp3=fopen(argv[3],"w");
//fp=fopen("f://test.txt","r");
//fp2=fopen("f://test2.txt","w");
//if((fp=fopen("f://test.txt","r"))==NULL) {
// printf("Error:params: File not found.\n");
// exit(1);
//}
//if((fp2=fopen("f://test2.txt","w"))==NULL) {
// printf("Error:params: File not found.\n");
// exit(1);
//}
if((fp=fopen(argv[1],"r"))==NULL) {
printf("Error:params: File not found.\n");
exit(1);
}
if((fp2=fopen(argv[2],"w"))==NULL) {
printf("Error:params: File not found.\n");
exit(1);
}
if((fp3=fopen(argv[3],"w"))==NULL) {
printf("Error:params: File not found.\n");
exit(1);
}
// if((fp=fopen("c://test.txt","r"))==NULL) {
// printf("Error:params:Fp.\n");
// exit(1);
// }
// if((fp2=fopen("c://test2.txt","w"))==NULL) {
// printf("Error:params:Fp2.\n");
// exit(1);
// }
//if((fp3=fopen("c://test3.txt","w"))==NULL) {
// printf("Error:params:Fp3.\n");
// exit(1);
// }
do{
ch=fgetc(fp);
ch2[i] = ch;
i++;
razm++;
}while(!feof(fp));
ind=0;
i=0;
fl=0;
while(ind < razm)
{
for(j=0;j<11;j++)
{
if((ch2[ind] == cifra[j] || ch2[ind] == '.') && fl == 0)
{
i=fun1(razm,ind);
ind=i;
fl=1;
masId++;
superfl = 1;
break;
}
}
if(ch2[ind] == '/' && ch2[ind + 1] == '/')
{
i=fun6(razm,ind);
ind=i;
fl=1;
superfl = 1;
}
for(j=0;j<53;j++)
{
if(ch2[ind] == bukva[j] && fl == 0 && l == 0)
{
i=fun2(razm,ind);
ind=i;
fl=1;
/////fun7
i=fun5(razm,ind);
ind=i;
superfl = 0;
break;
}
}
if(ch2[ind] == ' ')
{
ind++;
fl=1;
}
for(j=0;j<20;j++)
{
if(ch2[ind] == ']'){fl=0;}
if((ch2[ind] == '!' || ch2[ind] == '+' || ch2[ind] == '-' ||
ch2[ind] == '*' || ch2[ind] == '/' || ch2[ind] == '%' ||
ch2[ind] == '=' || ch2[ind] == '<' || ch2[ind] == '>' ||
ch2[ind] == ':' || ch2[ind] == ';' || ch2[ind] == ',' ||
ch2[ind] == '(' || ch2[ind] == ')' || ch2[ind] == '[' ||
ch2[ind] == ']' || ch2[ind] == ' ') && fl == 0)
{
superfl = 1;
i=fun3(razm,ind);
ind=i;
fl=1;
break;
}
}
if((ch2[ind] == '\n' || ch2[ind] == '\t') && fl == 0)
{
i=fun4(razm,ind);
ind=i;
fl=1;
}
if(fl == 0)
{
if(ind != razm-1)
{
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[ind];
printf("\n");
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[ind], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
fle=1;
}
for(;;)
{
ind++;
if(ch2[ind] == '\n' || ch2[ind] == '\0')
{
break;
}
}
l=0;
}
fl=0;
}
for(i=0;i<masId+1;i++)
{
std::cout << mas[i] << " ";
std::cout << mas2[i] << std::endl;
}
masId2 = 0;
fprintf(fp3, "<program>");
fprintf(fp3, "\n");
for(i=0;i<masId+1;i++)
{
funOpredelenie();
}
fprintf(fp3, "</program>");
if(fle == 0)
{
cout << "ok";
}
cout << endl;
system("pause");
fclose(fp);
fclose(fp2);
fclose(fp3);
return 0;
}
int fun1 (int razm, int ind)
{
int i=ind,j=0,fl=0,buf[90],rbuf=0,tochka=0,real=-1,e=0,fl2=0,q=0;
///////////////////////двоичное///////////////////
if((ind+2 < razm) || (ind+2 == razm))
{
if((ch2[i] == '2' && ch2[i+1] == '#') && (ch2[i+2] == '0' || ch2[i+2] == '1'))
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:int");
fprintf(fp2, "\t");fprintf(fp2, "val:2");
fwrite(&ch2[i+1], sizeof( char ), 1, fp2);
fwrite(&ch2[i+2], sizeof( char ), 1, fp2);
mas[masId] = "int";
mas2[masId] = "2";
bufstr = ch2[i+1];
mas2[masId].append(bufstr);
bufstr = ch2[i+2];
mas2[masId].append(bufstr);
i=i+3;
for(;;)
{
if(ch2[i] != '0' && ch2[i] != '1')
{
if(ch2[i] == ' ' || ch2[i] == '\t' || ch2[i] == '\n' || ch2[i+1] == '\0'
|| ch2[i] == '[' || ch2[i] == ']' || ch2[i] == ':' || ch2[i] == ';'
|| ch2[i] == '(' || ch2[i] == ')')
{
fprintf(fp2, "\n");
return i;
}
else
{
fprintf(fp2, "\n");
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[i];
printf("\n");
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[i], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
fle=1;
for(;;)
{
if(ch2[i] == '\n' || ch2[i] == '\0')
{
//i++;
return i;
}
i++;
}
}
}
bufstr = ch2[i];
mas2[masId].append(bufstr);
fwrite(&ch2[i], sizeof( char ), 1, fp2);
i++;
}
}
}
/////////////////шестнадцетиричное//////////////
q=0;
memset(strN, 0, sizeof(int)*9);
if(ch2[i] == '1' && ch2[i+1] == '6' && ch2[i+2] == '#')
{
fl2=1;
i=i+3;
for(j=0;j<12;j++)
{
if(ch2[i] == cifra[j] || ch2[i] == bukva[j])
{
fl2=2;
j=-1;
if(fl == 0)
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:int");
fprintf(fp2, "\t");fprintf(fp2, "val:16");
fwrite(&ch2[i-1], sizeof( char ), 1, fp2);
mas[masId] = "int";
mas2[masId] = "1";
bufstr = ch2[i-2];
mas2[masId].append(bufstr);
bufstr = ch2[i-1];
mas2[masId].append(bufstr);
fl=1;
}
fwrite(&ch2[i], sizeof( char ), 1, fp2);
bufstr = ch2[i];
mas2[masId].append(bufstr);
i++;
}
}
if(ch2[i] == '\n' || ch2[i] == ' ' || ch2[i] == '\t' || ch2[i] == '\0' || ch2[i] == ';' || ch2[i] == ','|| ch2[i] == ']')
{
if(fl == 1)
{
fprintf(fp2, "\n");
}
if(fl2==1)
{
fprintf(fp2, "\n");
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[i-1], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[i-1];
printf("\n");
fle=1;
}
return i;
}
else
{
if(fl == 1)
{
fprintf(fp2, "\n");
}
printf("Error:");
printf("%d", nstr);
cout << ch2[i];
printf("\n");
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[i], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
fle=1;
for(;;)
{
if(ch2[i] == '\n' || ch2[i] == '\0')
{
//i++;
return i;
}
i++;
}
}
return i;
}
///////////////////десятеричное//////////////////
q=0;
memset(strN, 0, sizeof(int)*9);
fl2=0;
rbuf=0;
real=-1;
tochka=0;
e=0;
int fl4 = 0;
int fl3=0;
int t=11,k=0;
int fl5 = 1;
int flee = 0;
for(k=ind;k<razm;k++)
{
if(ch2[k] == '.')
{
fl3=1;
break;
}
}
if(ch2[i] == '.' && (ch2[i+1] == 'e' || ch2[i+1] == 'E'))
flee=1;
if(ch2[i] == '.' && flee == 0)
{ real = 0; fl5 = 1; }
if(ch2[i] == '1' && ch2[i+1] == '0' && ch2[i+2] == '#' && fl3 == 0)
i = i + 3;
if(ch2[i] == '8' && ch2[i+1] == '#' && fl3 == 0)
{
t=8;
fl4 = 1;
i = i + 2;
}
for(j=0;j<t;j++)
{
if(ch2[i] == cifra[j] && fl2 == 0 && flee == 0)
{
buf[rbuf] = ch2[i];
rbuf++;
j=-1;
i++;
fl5 = 0;
if (real != 1)
real = 0;
}
if(ch2[i] == '.' && fl2 == 0 && tochka == 0 && real == 0 && fl4 == 0 && flee == 0)
{
real = 1;
tochka = 1;
buf[rbuf] = ch2[i];
rbuf++;
i++;
j=-1;
}
if((ch2[i] == 'e' || ch2[i] == 'E') && fl4 == 0 && fl5 == 0 && flee == 0&& e == 0 && fl2 == 0 && (real == 0 || real == 1)
&& (((ch2[i+1] == '+' || ch2[i+1] == '-')
&& ch2[i+2] == cifra[j]) || ch2[i+1] == cifra[j]) )
{
real=1;
e=1;
j=-1;
buf[rbuf] = ch2[i];
rbuf++;
buf[rbuf] = ch2[i+1];
rbuf++;
i=i+2;
}
}
if(real == 0 || real == 1)
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");
if(real == 0)
{
fprintf(fp2, "lex:int");
mas[masId] = "int";
}
if(real == 1)
{
fprintf(fp2, "lex:real");
mas[masId] = "real";
}
fprintf(fp2, "\t");fprintf(fp2, "val:");
for(j=0;j<rbuf;j++)
{
fwrite(&buf[j], sizeof( char ), 1, fp2);
bufstr = buf[j];
mas2[masId].append(bufstr);
}
///////////////////////////////
if(t == 8)
{
li1 = strtol (strN,&pEnd,8);
//cout << li1;
fprintf(fp2, " Dec: %d",li1);
}
///////////////////////////////
fprintf(fp2, "\n");
if(ch2[i] != ' ' && ch2[i] != '\n' && ch2[i] != '\t' && ch2[i+1] != '\0' && ch2[i] != ':' && ch2[i] != ']' &&
ch2[i] != '[' && ch2[i] != '(' && ch2[i] != ')' && ch2[i] != ';')
{
///////
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[i];
printf("\n");
fle=1;
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[i], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
for(;;)
{
if(ch2[i] == '\n' || ch2[i] == '\0')
{
return i;
}
i++;
}
}
}
if(fl2 == 1)
{
fprintf(fp2, "\n");
}
if(real == -1)
{
printf("Error:");
printf("%d", nstr);
cout << ch2[i];
printf("\n");
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[i], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
fle=1;
for(;;)
{
if(ch2[i] == '\n' || ch2[i] == '\0')
{
//i++;
return i;
}
i++;
}
}
return i;
}
int fun2(int razm, int ind)
{
int j=0,i=0,t=0, fl=0;
if(ind == 0)
{
fl=1;
}
if(fl == 0)
{
if(ch2[ind-1] == '\t' || ch2[ind-1] == '\n' || ch2[ind-1] == ' ' || ch2[ind-1] == ':' || ch2[ind-1] == ';' || ch2[ind-1] == ',')
{
fl=2;
}
}
if(fl == 1 || fl == 2)
{
for(j=0;j<18;j++)
{
for(i=ind;i<rstr[j]+ind;i++)
{
if(ch2[i] == str[j][t])
{
t++;
if(t == rstr[j])
{
if(ch2[i+1] == '\t' || ch2[i+1] == '\n' || ch2[i+1] == ' ' || ch2[i+2] == '\0' || ch2[i+1] == ';' || ch2[i+1] == ',')
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:");
fprintf(fp2, "%s", str[j].c_str());
fprintf(fp2, "\n");
mas[masId] = str[j];//tut lex a ne znachenie!!!!!!!!!!!
mas2[masId] = str[j];
masId++;
i++;
return i;
}
}
}
}
t=0;
}
}
return ind;
}
int fun3 (int razm, int ind)
{
int i=0,j=0,t=0;
for(j=0;j<20;j++)
{
for(i=ind;i<rs2[j]+ind;i++)
{
if(ch2[i] == s2[j][t])
{
t++;
if(t == rs2[j])
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:");
fprintf(fp2, "%s", s[j].c_str());
fprintf(fp2, "\t");fprintf(fp2, "val:");
fprintf(fp2, "%s", s2[j].c_str());
fprintf(fp2, "\n");
mas[masId] = s[j];
mas2[masId] = s2[j];
masId++;
i++;
return i;
}
}
}
t=0;
}
if(ch2[ind] == '!')
{
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[ind];
printf("\n");
fle=1;
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[ind], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
ind++;
}
if( ch2[ind] == '!' || ch2[ind] == '+' || ch2[ind] == '-' ||
ch2[ind] == '*' || ch2[ind] == '/' || ch2[ind] == '%' ||
ch2[ind] == '=' || ch2[ind] == '<' || ch2[ind] == '>')
{
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[ind];
printf("\n");
fle=1;
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[ind], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
for(;;)
{
if(ch2[ind] == '\n' || ch2[ind] == '\0')
{
return ind;
}
ind++;
}
}
return ind;
}
int fun4 (int razm, int ind)
{
if(ch2[ind] == '\n')
{
nstr++;
ind++;
return ind;
}
if(ch2[ind] == '\t')
{
ind++;
return ind;
}
return ind;
}
int fun5 (int razm, int ind)
{
int i=0,j=ind,fl=0,fl2=0, t=0;
for(j=0;j<20;j++)
{
for(i=ind;i<rs2[j]+ind;i++)
{
if(ch2[i] == s2[j][t])
{
t++;
if(t == rs2[j])
{
if(ch2[i+1] == ' ' || ch2[i+1] == '\n' || ch2[i+1] == '\t' || ch2[i+2] == '\0' || ch2[i+1] == ';' || ch2[i+1] == ',')
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:");
fprintf(fp2, "%s", s[j].c_str());
fprintf(fp2, "\t");fprintf(fp2, "val:");
fprintf(fp2, "%s", s2[j].c_str());
fprintf(fp2, "\n");
mas[masId] = s[j];
mas2[masId] = s2[j];
masId++;
i++;
return i;
}
}
}
}
t=0;
}
for(j=ind;j<razm;j++)
{
fl2=0;
for(i=0;i<53;i++)
{
if(ch2[j] == bukva[i])
{
if(fl == 0)
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:id");
fprintf(fp2, "\t");fprintf(fp2, "val:");
mas[masId] = "id";
}
fwrite(&ch2[j], sizeof( char ), 1, fp2);
bufstr = ch2[j];
mas2[masId].append(bufstr);
fl=1;//pervi raz v fun5 s ukazatelem ind
fl2=1;
}
}
for(i=0;i<11;i++)
{
if(ch2[j] == cifra[i] && fl == 1)
{
fwrite(&ch2[j], sizeof( char ), 1, fp2);
bufstr = ch2[j];
mas2[masId].append(bufstr);
fl2=1;
}
}
if(fl2 == 0)
{
ind = j;
if(fl == 1)
{
fprintf(fp2, "\n");
masId++;
}
return ind;
}
}
return ind;
}
int fun6 (int razm, int ind)
{
int i=ind,j=0;
for(i=ind;i<razm;i++)
{
if(ch2[i] == '\n')
{
nstr++;
i++;
return i;
}
}
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[ind];
printf("\n");
fle=1;
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[ind], sizeof( char ), 1, fp2);
ind++;
return ind;
}
int funOpredelenie()
{
int i=0,j=0;
if((mas[masId2] == str[3] || mas[masId2] == str[4])
&& (mas2[masId2] == str[3] || mas2[masId2] == str[4]))
{
masId2=funOpisanie(masId2+1);j=1;
}
if(mas[masId2] == str[17])
{
masId2=funProcedure(masId2+1);j=1;
}
for(i=0;i<9;i++)
{
if(mas[masId2] == prv[i] || (mas[masId2] == "int" && mas2[masId2] != "int" && mas[masId2+1] == "Colon"))
{
masId2=funOperator(masId2);j=1;
if(mas[masId2] != "Semicolon"){
funError(masId2);}
masId2++;
}
}
if(mas2[masId2] == ";")//pustoi
{
masId2++;j=1;
}
if(j == 0 && masId2 != masId)
{
funError(masId2);
}
////////proverka esli ne kude ne popali
return 0;
}
int funOpisanie(int vp)
{
int fl = -1;//нужен для того что бы понять после инд. есть ли [ celoe ]
do{
if(mas[vp] == "id")
{
fl = 0;//vstretili id
if(mas2[vp+1] == "[")
{
fl = -1;//na4ali
if((mas[vp+2] == "int" && mas2[vp+2] != "int") && mas2[vp+2] != "0")
{
fl = -1;
if(mas2[vp+3] == "]")
{
fl = 1;//zakrili konstrukc
}
}
}
}
if(fl == 0)
{
fprintf(fp3, "<dfn name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, " type=");
fprintf(fp3, "\"%s\"", mas[masId2].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp = vp+1;
}
if(fl == -1)
{
funError(vp);
}
if(fl == 1)
{
fprintf(fp3, "<dfn name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, " length=");
fprintf(fp3, "\"%s\"", mas2[vp+2].c_str());
fprintf(fp3, " type=");
fprintf(fp3, "\"%s\"", mas[masId2].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp=vp+4;
}
fl = 0;
if(mas2[vp] != "," && mas2[vp] != ";")
{
funError(vp);
}
if(mas2[vp] == ",")
{
vp++;
if(mas[vp] != "id")
{
funError(vp);
}
}
}while(mas2[vp] != ";");
vp=vp+1;
return vp;
}
int funProcedure(int vp)
{
int i=0,j=0;//,vp = masId2+1;
fprintf(fp3, "<procedure ");
if(mas[vp] == "id")
{
fprintf(fp3, "name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, ">");
fprintf(fp3, "\n");
if(mas[vp + 1] != "start")
{
vp++;
do{
if((mas[vp] == "int" && mas2[vp] == "int") || (mas[vp] == "real" && mas2[vp] == "real"))
{
masId2 = vp;
vp++;
vp=funOpisanie(vp);
}
else
{
funError(vp);
}
/*if(mas2[vp] != ";")
{
funError(vp);
}*/
}while(mas[vp] != "start");//do dumat uslovie esli voobhe net
vp=funSostavnoi(vp);
fprintf(fp3, "</procedure>");
fprintf(fp3, "\n");
return vp;
}
else
{
vp++;
vp=funSostavnoi(vp);
fprintf(fp3, "</procedure>");
fprintf(fp3, "\n");
return vp;
}
}
else
{
funError(vp);
}
return vp;
}
int funOperator(int vp)
{
int i=0,j=0;
if(mas[vp] == "int" && mas[vp + 1] == "Colon")
{
fprintf(fp3, "<label name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp=vp+2;
}
for(i=0;i<kprv;i++)
{
if(mas[vp] == prv[i])//mas2[vp]
{
switch(i)
{
case 0: vp=funSostavnoi(vp); return vp;
case 1: vp=funPrisvaivanie(vp); return vp;
case 2: vp=funPerehod(vp); return vp;
case 3: vp=funUslovnii(vp); return vp;
case 4: vp=funCikl(vp); return vp;
case 5: vp=funVvod(vp); return vp;
case 6: vp=funVivod(vp); return vp;
case 7: vp=funVizov(vp); return vp;
case 8: vp=funBreak(vp); return vp;
if(mas2[vp] == ";")
{
vp++;
return vp;
}
}
}
}
return vp;
}
int funSostavnoi(int vp)
{
//vp == "begin" -> vp+1
fprintf(fp3, "<compound>");
fprintf(fp3, "\n");
vp=vp+1;
do{
vp=funOperator(vp);
}while(mas2[vp] != ";" && mas2[vp] != "stop");//dopisat'
vp++;
if(mas[vp] == "stop")
{
vp++;
if(mas2[vp] == ";")
{
fprintf(fp3, "</compound>");
fprintf(fp3, "\n");//!!!
return vp;
}
else
{
funError(vp);
}
}
else
{
funError(vp);
}
return 0;
}
int funPrisvaivanie(int vp)
{
int t=0;
vp=vp+1;
t=vp;
fprintf(fp3, "<assign>");
fprintf(fp3, "\n");
vp=funPeremennaya(vp);
if(vp == t)//возвращаем указатель с которым надо работать t-esli ne izmenilos to net peremennoi
{
funError(vp);
}
if(mas[vp] == "Comma")
{
vp++;
vp=funVirojenie(vp);
}
else
{
funError(vp);
}
fprintf(fp3, "</assign>");
fprintf(fp3, "\n");
return vp;
}
int funPeremennaya(int vp)
{
int j=0;
if(mas[vp] == "id")
{
vp++;
if(mas2[vp]=="[")
{
j=-1;
if(mas[vp+1]=="id" || (mas[vp+1]=="int" && mas2[vp+1]!="int"))
{
j=-1;
if(mas2[vp+2]=="]")
{
j=1;
fprintf(fp3, "<var name=");
fprintf(fp3, "\"%s\"", mas2[vp-1].c_str());
fprintf(fp3, " index=");
fprintf(fp3, "\"%s\"", mas2[vp+1].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp=vp+3;
return vp;
}
}
}
if (j == -1)
{
funError(vp);
}
fprintf(fp3, "<var name=");
fprintf(fp3, "\"%s\"", mas2[vp-1].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
return vp;
}
return vp;
}
int funVirojenie(int vp)
{
int t;
if(flexpr == 0){
fprintf(fp3, "<expr>");
flexpr = 1;}
t=funOperaciya(vp);
if(t != -1)
{
vp=funOperand(t);
vp=funOperand(vp);
if(flexpr == 1){
fprintf(fp3, "</expr>");
flexpr = 0;
fprintf(fp3, "\n");}
return vp;
//operaciya vp ukazivaet na slovo posle znaka
}
if(mas2[vp] == "(")
{
if(mas[vp+1] == s[4])
{
vp=vp+2;
t=vp;
vp=funOperand(vp);
if(vp != t)
{
if(mas2[vp] == ")")
{
if(flexpr == 1){
fprintf(fp3, "</expr>");
flexpr = 0;
fprintf(fp3, "\n");}
vp++;
return vp;
}
else
{
funError(vp);
}
}
else
{
funError(vp);
}
}
else
{
funError(vp);
}
}
vp=funOperand(vp);
if(flexpr == 1)
{
fprintf(fp3, "</expr>");
flexpr = 0;
fprintf(fp3, "\n");
}
//if(i != vp)
//{
// i=vp;
// vp=funOperand(vp);
//if(vp != i)
//{
// if(flexpr == 1){
// fprintf(fp3, "</expr>");
// flexpr = 0;
// fprintf(fp3, "\n");}
//}
//}
//fprintf(fp3, "</expr>");
//fprintf(fp3, "\n");
//vp++;//???
return vp;
}
int funOperaciya(int vp)
{
int i=0;
for(i=0;i<20;i++)
{
if(s2[i] == mas2[vp])
{
if((i > 2 && i < 9) || (i>14 && i<18) || (i == 19))
{
fprintf(fp3, "<op kind=");
fprintf(fp3, "\"%s\"", mas[vp].c_str());
fprintf(fp3, ">");
vp++;
return vp;//!!!
}
}
}
return -1;
}
int funOperand(int vp)
{
int i=vp;
vp=funPeremennaya(vp);
if(vp != i){return vp;}
if((mas[vp] == "int" && mas2[vp] != "int") || (mas[vp] == "real" && mas2[vp] != "real"))
{
fprintf(fp3, "<");
fprintf(fp3, "\"%s\"", mas[vp].c_str());
fprintf(fp3, " val=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
vp++;
return vp;
}
i=vp;
vp=funVirojenie(vp);
if(vp != i){return vp;}
return vp;
}
int funPerehod(int vp)
{
vp++;
if(mas[vp] == "id")
{
fprintf(fp3, "<goto");
fprintf(fp3, " label=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
}
else
{
funError(vp);
}
vp++;
return vp;
}
int funUslovnii(int vp)
{
int i;
fprintf(fp3, "<if>");
fprintf(fp3, "\n");
vp++;
i=vp;
vp=funVirojenie(vp);
if(i == vp)
{
funError(vp);
}
if(mas2[vp] != "then")
{
funError(vp);
}
vp++;
do{
i=vp;
vp=funOperator(vp);
int y=0;//dlya proverki ';'
if(mas2[vp] == ";")
{
//vp++;
y=1;//';' vstretili -> pustoi
}
if(i == vp && y == 0)
{
funError(vp);
}
if(mas2[vp] != ";" && y == 0)
{
funError(vp);
}
vp++;
}while(mas2[vp] != "else" && mas2[vp] != "stop");
if(mas2[vp] == "else")
{
vp++;
do{
i=vp;
vp=funOperator(vp);
int y=0;//dlya proverki ';'
if(mas2[vp] == ";")
{
//vp++;
y=1;//';' vstretili -> pustoi
}
if(i == vp && y == 0)
{
funError(vp);
}
if(mas2[vp] != ";" && y == 0)
{
funError(vp);
}
vp++;
}while(mas2[vp] != "stop");
fprintf(fp3, "</if>");
fprintf(fp3, "\n");
vp++;
return vp;
}
else
{
if(mas2[vp] == "stop")
{
fprintf(fp3, "</if>");
fprintf(fp3, "\n");
vp++;
return vp;
}
else
{
funError(vp);
}
}
return vp;
}
int funCikl(int vp)
{
int i=0;
fprintf(fp3, "<for>");
fprintf(fp3, "\n");
vp++;
i=vp;
vp=funOperator(vp);
//if(mas2[vp] == "end")
//{
// vp++;
// fprintf(fp3, "</for>");
// fprintf(fp3, "\n");
// return vp;
//}
//if(mas2[vp] == ";" && mas2[vp+1] == "end")
//{
// vp=vp+2;
// fprintf(fp3, "</for>");
// fprintf(fp3, "\n");
// return vp;
//}
//if(i == vp)
//{
// cout << "Error: net operatora v Cikle";
// exit(1);
//}
do{
i=vp;
vp=funOperator(vp);
if(mas2[vp] == "stop")
{
vp++;
fprintf(fp3, "</for>");
fprintf(fp3, "\n");
return vp;
}
if(i == vp)
{
vp++;
}
}while(mas2[vp] != "stop");
//if(mas2[vp] == "stop")
//{
// vp++;
// fprintf(fp3, "</for>");
// fprintf(fp3, "\n");
// return vp;
//}
if(mas2[vp] != ";" || mas2[vp] != "stop")
{
funError(vp);
}
return 0;
}
int funVvod(int vp)
{
vp++;
int i;
fprintf(fp3, "<read>");
fprintf(fp3, "\n");
i=vp;
vp=funPeremennaya(vp);
if(i == vp)
{
funError(vp);
}
do{
if(mas2[vp] == ",")
{
vp++;
i=vp;
vp=funPeremennaya(vp);
if(i == vp)
{
funError(vp);
}
}
if(mas2[vp] != ";" && mas2[vp] != "," && mas2[vp] != "stop")
{
funError(vp);
}
}while(mas2[vp] != ";" && mas2[vp] != "stop");
fprintf(fp3, "</read>");
fprintf(fp3, "\n");
return vp;
}
int funVivod(int vp)
{
vp++;
fprintf(fp3, "<write>");
fprintf(fp3, "\n");
int i;
i=vp;
vp=funSpecifikator(vp);
if(i == vp)
{
vp=funVirojenie(vp);
if(i == vp)
{
funError(vp);
}
}
do
{
int j=0;
if(mas2[vp] == ",")
{
j=1;//vstretili ','
vp++;
if(mas2[vp] == "stop")//kostil
{
funError(vp);
}
if(mas2[vp] == ";")//kostil
{
funError(vp);
}
i=vp;
vp=funSpecifikator(vp);
if(i == vp)
{
vp=funVirojenie(vp);
if(i == vp)
{
funError(vp);
}
}
}
if(j == 0 && mas2[vp] != ";" && mas2[vp] != "stop")
{
funError(vp);
}
}while(mas2[vp] != ";" && mas2[vp] != "stop");//
fprintf(fp3, "</write>");
fprintf(fp3, "\n");
return vp;
}
int funSpecifikator(int vp)
{
if(mas2[vp] == "skip" || mas2[vp] == "space" || mas2[vp] == "tab")
{
fprintf(fp3, "<qualifier kind=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp++;
return vp;
}
return vp;
}
int funVizov(int vp)
{
int i;
vp++;
if(mas[vp] == "id" && mas2[vp+1] == "stop")
{
fprintf(fp3, "<call name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
fprintf(fp3, "</call>");
fprintf(fp3, "\n");
vp=vp+2;
return vp;
}
else if(mas[vp] != "id")
{
funError(vp);
}
else
{
fprintf(fp3, "<call name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp++;
i=vp;
vp=funPeremennaya(vp);
if(i == vp)
{
funError(vp);
}
do
{
if(mas2[vp] == ",")
{
vp++;
i=vp;
vp=funPeremennaya(vp);
if(i == vp)
{
funError(vp);
}
}
}while(mas2[vp] != "stop");
fprintf(fp3, "</call>");
fprintf(fp3, "\n");
vp++;
return vp;
}
}
int funBreak(int vp)
{
fprintf(fp3, "</break>");
fprintf(fp3, "\n");
vp++;
return vp;
}
int funError(int e)
{
fclose(fp2);
fclose(fp3);
cout << "Error: " << mas2[e] << ": " << e;
exit(1);
return 0;
}
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <conio.h>
using namespace std;
char ch2[700];
char strN[90];
char cifra[11] = "0123456789";
char bukva[53] = "ABCDEFabcdefGHIJKLMNOPQRSTUVWXYZghijklmnopqrstuvwxyz";
string str[19] = {"start", "stop","end","int","real","goto","read","write","skip","space","tab","ppp","if","then","else","loop","call","procedure","ppp"};
int rstr[19] = { 5, 4, 3, 3, 4, 4, 4, 5, 4, 5, 3, 3, 2, 4, 4, 4, 4, 9, 3};
string s[20]={"let","Semicolon","Space","Add","Min","Div", "Mul", "LE", "GE", "LRB","RRB","LSB","RSB","Comma","Mod","LT","GT", "NE","Colon","EQ"};
string s2[20]={"let", ";", " ", "plus","minus", "div","mult", "le", "ge", "(", ")", "[", "]", ",", "mod", "lt","gt","ne", ":", "eq"};
int rs2[20] = { 3, 1, 1, 4, 5, 3, 4, 2, 2, 1, 1, 1, 1, 1, 3, 2, 2, 2, 1, 2};
int nstr=1;
int fle=0, superfl = 0;
long int li1;
char * pEnd;
FILE *fp;
FILE *fp2;
FILE *fp3;
int fun1 (int razm, int ind);
int fun2 (int razm, int ind);
int fun3 (int razm, int ind);
int fun4 (int razm, int ind);
int fun5 (int razm, int ind);
int fun6 (int razm, int ind);
int funOpredelenie();
int funOpisanie(int vp);
int funProcedure(int vp);
int funOperator(int vp);
int funSostavnoi(int vp);
int funPrisvaivanie(int vp);
int funPerehod(int vp);
int funUslovnii(int vp);
int funCikl(int vp);
int funVvod(int vp);
int funVivod(int vp);
int funVizov(int vp);
int funError(int vp);
int funPeremennaya(int vp);
int funVirojenie(int vp);
int funOperaciya(int vp);
int funOperand(int vp);
int funSpecifikator(int vp);
int funBreak(int vp);
int funError(int e);
string mas[900];
string mas2[900];
string bufstr;//perevod iz Char v String
int masId=0;//количество элементов которое будет в массивах стринг после их заполнения
int masId2=0;//рабочий указатель в массивах стринг
string prv[9]={"start","let","goto","if","loop","read","write","call","break"};//!!!;!!!
int kprv = 9;
int flexpr=0;
#include "1.h"
int fun1 (int razm, int ind)
{
int i=ind,j=0,fl=0,buf[90],rbuf=0,tochka=0,real=-1,e=0,fl2=0;
if(ch2[i] == '0')
{
///////////////////////двоичное///////////////////
if((ind+2 < razm) || (ind+2 == razm))
{
if((ch2[i+1] == 'b' || ch2[i+1] == 'B') && (ch2[i+2] == '0' || ch2[i+2] == '1'))
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:int");
fprintf(fp2, "\t");fprintf(fp2, "val:0");
fwrite(&ch2[i+1], sizeof( char ), 1, fp2);
fwrite(&ch2[i+2], sizeof( char ), 1, fp2);
mas[masId] = "int";
mas2[masId] = "0";
bufstr = ch2[i+1];
mas2[masId].append(bufstr);
bufstr = ch2[i+2];
mas2[masId].append(bufstr);
i=i+3;
for(;;)
{
if(ch2[i] != '0' && ch2[i] != '1')
{
if(ch2[i] == ' ' || ch2[i] == '\t' || ch2[i] == '\n' || ch2[i+1] == '\0'
|| ch2[i] == '[' || ch2[i] == ']' || ch2[i] == ':' || ch2[i] == ';'
|| ch2[i] == '(' || ch2[i] == ')' || ch2[i] == ',')
{
fprintf(fp2, "\n");
return i;
}
else
{
fprintf(fp2, "\n");
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[i];
printf("\n");
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[i], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
fle=1;
for(;;)
{
if(ch2[i] == '\n' || ch2[i] == '\0')
{
return i;
}
i++;
}
}
}
bufstr = ch2[i];
mas2[masId].append(bufstr);
fwrite(&ch2[i], sizeof( char ), 1, fp2);
i++;
}
}
}
/////////////////шестнадцетиричное//////////////
if(ch2[i+1] == 'x' || ch2[i+1] == 'X')
{
fl2=1;
i=i+2;
for(j=0;j<12;j++)
{
if(ch2[i] == cifra[j] || ch2[i] == bukva[j])
{
fl2=2;
j=-1;
if(fl == 0)
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:int");
fprintf(fp2, "\t");fprintf(fp2, "val:0");
fwrite(&ch2[i-1], sizeof( char ), 1, fp2);
mas[masId] = "int";
mas2[masId] = "0";
bufstr = ch2[i-1];
mas2[masId].append(bufstr);
fl=1;
}
fwrite(&ch2[i], sizeof( char ), 1, fp2);
bufstr = ch2[i];
mas2[masId].append(bufstr);
i++;
}
}
if(ch2[i] == '\n' || ch2[i] == ' ' || ch2[i] == '\t' || ch2[i] == '\0' || ch2[i] == ']')
{
if(fl == 1)
{
fprintf(fp2, "\n");
}
if(fl2==1)
{
fprintf(fp2, "\n");
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[i-1], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[i-1];
printf("\n");
fle=1;
}
return i;
}
else
{
if(fl == 1)
{
fprintf(fp2, "\n");
}
printf("Error:");
printf("%d", nstr);
cout << ch2[i];
printf("\n");
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[i], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
fle=1;
for(;;)
{
if(ch2[i] == '\n' || ch2[i] == '\0')
{
return i;
}
i++;
}
}
return i;
}
}
///////////////////десятеричное//////////////////
fl2=0;
rbuf=0;
real=-1;
tochka=0;
e=0;
int fl3=0;
int t=11,k=0;
for(k=ind;k<razm;k++)
{
if(ch2[k] == '.')
{
fl3=1;
break;
}
}
if(ch2[i] == '0' && fl3 == 0)
{
t=8;
}
for(j=0;j<t;j++) //если первая цифра 1-9, то следующая либо 0-9, либо её нет
{
if(ch2[i] == cifra[j] && fl2 == 0)
{
buf[rbuf] = ch2[i];
rbuf++;
j=-1;
i++;
if(real != 1)
{
real = 0;
}
}
if(ch2[i] == '.' && fl2 == 0 && ch2[i+1] == cifra[j] && tochka == 0 && real == 0)
{
real = 1;
tochka = 1;
buf[rbuf] = ch2[i];
rbuf++;
i++;
j=-1;
}
if((ch2[i] == 'e' || ch2[i] == 'E') && e == 0 && fl2 == 0 && (real == 0 || real == 1)
&& (((ch2[i+1] == '+' || ch2[i+1] == '-')
&& ch2[i+2] == cifra[j]) || ch2[i+1] == cifra[j]) )
{
real=1;
e=1;
j=-1;
buf[rbuf] = ch2[i];
rbuf++;
buf[rbuf] = ch2[i+1];
rbuf++;
i=i+2;
}
}
if(real == 0 || real == 1)
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");
if(real == 0)
{
fprintf(fp2, "lex:int");
mas[masId] = "int";
}
if(real == 1)
{
fprintf(fp2, "lex:real");
mas[masId] = "real";
}
fprintf(fp2, "\t");fprintf(fp2, "val:");
for(j=0;j<rbuf;j++)
{
fwrite(&buf[j], sizeof( char ), 1, fp2);
bufstr = buf[j];
mas2[masId].append(bufstr);
}
fprintf(fp2, "\n");
if(ch2[i] != ' ' && ch2[i] != '\n' && ch2[i] != '\t' && ch2[i+1] != '\0' && ch2[i] != ':' && ch2[i] != ']' &&
ch2[i] != '[' && ch2[i] != '(' && ch2[i] != ')' && ch2[i] != ';' && ch2[i] != ',')
{
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[i];
printf("\n");
fle=1;
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[i], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
for(;;)
{
if(ch2[i] == '\n' || ch2[i] == '\0')
{
return i;
}
i++;
}
}
}
if(fl2 == 1)
{
fprintf(fp2, "\n");
}
return i;//не одно из условий функции не удавлетваренно, храним этот символ
}
int fun2(int razm, int ind)
{
int j=0,i=0,t=0, fl=0;
if(ind == 0)
{
fl=1;
}
if(fl == 0)
{
if(ch2[ind-1] == '\t' || ch2[ind-1] == '\n' || ch2[ind-1] == ' ' || ch2[ind-1] == ':' || ch2[ind-1] == ';' || ch2[ind-1] == ',')
{
fl=2;
}
}
if(fl == 1 || fl == 2)
{
for(j=0;j<19;j++)
{
for(i=ind;i<rstr[j]+ind;i++)
{
if(ch2[i] == str[j][t])
{
t++;
if(t == rstr[j])
{
if(ch2[i+1] == '\t' || ch2[i+1] == '\n' || ch2[i+1] == ' ' || ch2[i+2] == '\0' || ch2[i+1] == ';' || ch2[i+1] == ',')
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:");
fprintf(fp2, "%s", str[j].c_str());
fprintf(fp2, "\n");
mas[masId] = str[j];//tut lex a ne znachenie!!!!!!!!!!!
mas2[masId] = str[j];
masId++;
i++;
return i;
}
}
}
}
t=0;
}
}
return ind;//не соответствует не одному из слов, поэтому не меняем указатель в этой функции
}
int fun3 (int razm, int ind)
{
int i=0,j=0,t=0,fl=0;
for(j=0;j<20;j++)
{
for(i=ind;i<rs2[j]+ind;i++)
{
if(ch2[i] == ' ' && fl == 0)
{
i++;
ind++;
}
if(ch2[i] == s2[j][t])
{
t++;
if(t == rs2[j])
{
if(s[j] != "Space")
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:");
fprintf(fp2, "%s", s[j].c_str());
fprintf(fp2, "\t");fprintf(fp2, "val:");
fprintf(fp2, "%s", s2[j].c_str());
fprintf(fp2, "\n");
mas[masId] = s[j];
mas2[masId] = s2[j];
masId++;
i++;
return i;
}
else
{
i++;
ind++;
}
}
}
fl=1;//posle proverki znaka uderaet probel kak osibku
}
t=0;
fl=0;
}
if(ch2[ind] == '!')
{
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[ind];
printf("\n");
fle=1;
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[ind], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
ind++;
}
return ind;//вернули указатель неизменным
}
int fun4 (int razm, int ind)
{
if(ch2[ind] == '\n')
{
nstr++;
ind++;
return ind;
}
if(ch2[ind] == '\t')
{
ind++;
return ind;
}
return ind;
}
int fun5 (int razm, int ind)
{
int i=0,j=ind,fl=0,fl2=0;
for(j=ind;j<razm;j++)
{
fl2=0;
for(i=0;i<53;i++)
{
if(ch2[j] == bukva[i])
{
if(fl == 0)
{
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:id");
fprintf(fp2, "\t");fprintf(fp2, "val:");
mas[masId] = "id";
}
fwrite(&ch2[j], sizeof( char ), 1, fp2);
bufstr = ch2[j];
mas2[masId].append(bufstr);
fl=1;//pervi raz v fun5 s ukazatelem ind
fl2=1;
}
}
for(i=0;i<11;i++)
{
if(ch2[j] == cifra[i] && fl == 1)
{
fwrite(&ch2[j], sizeof( char ), 1, fp2);
bufstr = ch2[j];
mas2[masId].append(bufstr);
fl2=1;
}
}
if(fl2 == 0)
{
ind = j;
if(fl == 1)
{
fprintf(fp2, "\n");
masId++;
}
return ind;
}
}
return ind;
}
int fun6 (int razm, int ind)
{
int i=ind,j=0;
for(i=ind;i<razm;i++)
{
if(ch2[i] == '\n')
{
nstr++;
}
if(ch2[i] == '}')
{
i++;
return i;
}
}
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[ind];
printf("\n");
fle=1;
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[ind], sizeof( char ), 1, fp2);
ind++;
return ind;
}
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
int i=0,j=0,razm=0,ind=0,fl=0,fl2=0;
int l=0;
/*fp=fopen(argv[1],"r");
fp2=fopen(argv[2],"w");
fp3=fopen(argv[3],"w");*/
if((fp=fopen("c://test.txt","r"))==NULL) {
printf("Error:params:Fp.\n");
exit(1);
}
if((fp2=fopen("c://test2.txt","w"))==NULL) {
printf("Error:params:Fp2.\n");
exit(1);
}
if((fp3=fopen("c://test3.txt","w"))==NULL) {
printf("Error:params:Fp3.\n");
exit(1);
}
do{
ch=fgetc(fp);
ch2[i] = ch;
i++;
razm++;
}while(!feof(fp));
ind=0;
i=0;
fl=0;
while(ind < razm)
{
for(j=0;j<11;j++)
{
if(ch2[ind] == cifra[j] && fl == 0)
{
i=fun1(razm,ind);
ind=i;
fl=1;
masId++;
break;
}
}
for(j=0;j<53;j++)
{
if(ch2[ind] == bukva[j] && fl == 0 && l == 0)
{
i=fun2(razm,ind);
ind=i;
fl=1;
i=fun5(razm,ind);
ind=i;
break;
}
}
for(j=0;j<20;j++)
{
if((ch2[ind] == '!' || ch2[ind] == '+' || ch2[ind] == '-' ||
ch2[ind] == '*' || ch2[ind] == '/' || ch2[ind] == '%' ||
ch2[ind] == '=' || ch2[ind] == '<' || ch2[ind] == '>' ||
ch2[ind] == ':' || ch2[ind] == ';' || ch2[ind] == ',' ||
ch2[ind] == '(' || ch2[ind] == ')' || ch2[ind] == '[' ||
ch2[ind] == ']' || ch2[ind] == ' ') && fl == 0)
{
i=fun3(razm,ind);
ind=i;
fl=1;
break;
}
}
if((ch2[ind] == '\n' || ch2[ind] == '\t') && fl == 0)
{
i=fun4(razm,ind);
ind=i;
fl=1;
}
if(ch2[ind] == '{' && fl == 0)
{
i=fun6(razm,ind);
ind=i;
fl=1;
}
if(fl == 0)
{
if(ind != razm-1)
{
printf("Error:");
printf("%d", nstr);
printf(":");
cout << ch2[ind];
printf("\n");
fprintf(fp2, "%d", nstr);
fprintf(fp2, "\t");fprintf(fp2, "lex:Error");
fprintf(fp2, "\t");fprintf(fp2, "val:");
fwrite(&ch2[ind], sizeof( char ), 1, fp2);
fprintf(fp2, "\n");
fle=1;
}
for(;;)
{
ind++;
if(ch2[ind] == '\n' || ch2[ind] == '\0')
{
break;
}
}
l=0;
}
fl=0;
}
for(i=0;i<masId+1;i++)
{
std::cout << mas[i] << " ";
std::cout << mas2[i] << std::endl;
}
masId2 = 0;
fprintf(fp3, "<?xml version=\"1.0\"?>");
fprintf(fp3, "\n");
fprintf(fp3, "<program>");
fprintf(fp3, "\n");
for(i=0;i<masId+1;i++)
{
funOpredelenie();
if(masId2 >= masId)
{
break;
}
}
fprintf(fp3, "</program>");
if(fle == 0)
{
cout << "ok";
}
cout << endl;
system("pause");
fclose(fp);
fclose(fp2);
fclose(fp3);
return 0;
}
int funOpredelenie()
{
int i=0,j=0;
if((mas[masId2] == str[2] || mas[masId2] == str[3])
&& (mas2[masId2] == str[2] || mas2[masId2] == str[3]))
{
masId2=funOpisanie(masId2+1);j=1;
}
if(mas[masId2] == str[16])
{
masId2=funProcedure(masId2+1);j=1;
}
for(i=0;i<9;i++)
{
if(mas[masId2] == prv[i] || (mas[masId2] == "id" && mas[masId2+1] == "Colon"))
{
masId2=funOperator(masId2);j=1;
if(mas[masId2] != "Semicolon"){
funError(masId2);}
masId2++;
}
}
if(mas2[masId2] == ";")//pustoi
{
masId2++;j=1;
}
if(j == 0)
{
funError(masId2);
}
////////proverka esli ne kude ne popali
return 0;
}
int funOpisanie(int vp)
{
int fl = -1;//нужен для того что бы понять после инд. есть ли [ celoe ]
do{
if(mas[vp] == "id")
{
fl = 0;//vstretili id
if(mas2[vp+1] == "[")
{
fl = -1;//na4ali
if((mas[vp+2] == "int" && mas2[vp+2] != "int") && mas2[vp+2] != "0")
{
fl = -1;
if(mas2[vp+3] == "]")
{
fl = 1;//zakrili konstrukc
}
}
}
}
if(fl == 0)
{
fprintf(fp3, "<dfn name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, " type=");
fprintf(fp3, "\"%s\"", mas[masId2].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp = vp+1;
}
if(fl == -1)
{
funError(vp);
}
if(fl == 1)
{
fprintf(fp3, "<dfn name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, " length=");
fprintf(fp3, "\"%s\"", mas2[vp+2].c_str());
fprintf(fp3, " type=");
fprintf(fp3, "\"%s\"", mas[masId2].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp=vp+4;
}
fl = 0;
if(mas2[vp] != "," && mas2[vp] != ";")
{
funError(vp);
}
if(mas2[vp] == ",")
{
vp++;
if(mas[vp] != "id")
{
funError(vp);
}
}
}while(mas2[vp] != ";");
vp=vp+1;
return vp;
}
int funProcedure(int vp)
{
int i=0,j=0;//,vp = masId2+1;
fprintf(fp3, "<procedure ");
if(mas[vp] == "id")
{
fprintf(fp3, "name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, ">");
fprintf(fp3, "\n");
if(mas[vp + 1] != "begin")
{
vp++;
do{
if((mas[vp] == "int" && mas2[vp] == "int") || (mas[vp] == "real" && mas2[vp] == "real"))
{
masId2 = vp;
vp++;
vp=funOpisanie(vp);
}
else
{
funError(vp);
}
/*if(mas2[vp] != ";")
{
funError(vp);
}*/
}while(mas[vp] != "begin");//do dumat uslovie esli voobhe net
vp=funSostavnoi(vp);
fprintf(fp3, "</procedure>");
fprintf(fp3, "\n");
return vp;
}
else
{
vp++;
vp=funSostavnoi(vp);
fprintf(fp3, "</procedure>");
fprintf(fp3, "\n");
return vp;
}
}
else
{
funError(vp);
}
return vp;
}
int funOperator(int vp)
{
int i=0,j=0;
fprintf(fp3, "<clause>\n");
if(mas[vp] == "id" && mas[vp + 1] == "Colon")
{
fprintf(fp3, "<label name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp=vp+2;
}
for(i=0;i<kprv;i++)
{
if(mas[vp] == prv[i])//mas2[vp]
{
switch(i)
{
case 0: vp=funSostavnoi(vp); fprintf(fp3, "</clause>\n"); return vp;
case 1: vp=funPrisvaivanie(vp); fprintf(fp3, "</clause>\n"); return vp;
case 2: vp=funPerehod(vp); fprintf(fp3, "</clause>\n"); return vp;
case 3: vp=funUslovnii(vp); fprintf(fp3, "</clause>\n"); return vp;
case 4: vp=funCikl(vp); fprintf(fp3, "</clause>\n"); return vp;
case 5: vp=funVvod(vp); fprintf(fp3, "</clause>\n"); return vp;
case 6: vp=funVivod(vp); fprintf(fp3, "</clause>\n"); return vp;
case 7: vp=funVizov(vp); fprintf(fp3, "</clause>\n"); return vp;
case 8: vp=funBreak(vp); fprintf(fp3, "</clause>\n"); return vp;
if(mas2[vp] == ";")
{
vp++;
fprintf(fp3, "</clause>\n");
return vp;
}
}
}
}
fprintf(fp3, "</clause>\n");
return vp;
}
int funSostavnoi(int vp)
{
//vp == "begin" -> vp+1
fprintf(fp3, "<compound>");
fprintf(fp3, "\n");
vp=vp+1;
do{
vp=funOperator(vp);
}while(mas2[vp] != ";" && mas2[vp] != "end");//dopisat'
vp++;
if(mas[vp] == "end")
{
vp++;
if(mas2[vp] == ";")
{
fprintf(fp3, "</compound>");
fprintf(fp3, "\n");//!!!
return vp;
}
else
{
funError(vp);
}
}
else
{
funError(vp);
}
return 0;
}
int funPrisvaivanie(int vp)
{
int t=0;
vp=vp+1;
t=vp;
fprintf(fp3, "<assign>");
fprintf(fp3, "\n");
vp=funPeremennaya(vp);
if(vp == t)//возвращаем указатель с которым надо работать t-esli ne izmenilos to net peremennoi
{
funError(vp);
}
if(mas[vp] == "Comma")
{
vp++;
vp=funVirojenie(vp);
}
else
{
funError(vp);
}
fprintf(fp3, "</assign>");
fprintf(fp3, "\n");
return vp;
}
int funPeremennaya(int vp)
{
int j=0;
if(mas[vp] == "id")
{
vp++;
if(mas2[vp]=="[")
{
j=-1;
if(mas[vp+1]=="id" || (mas[vp+1]=="int" && mas2[vp+1]!="int"))
{
j=-1;
if(mas2[vp+2]=="]")
{
j=1;
fprintf(fp3, "<var name=");
fprintf(fp3, "\"%s\"", mas2[vp-1].c_str());
fprintf(fp3, " index=");
fprintf(fp3, "\"%s\"", mas2[vp+1].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp=vp+3;
return vp;
}
}
}
if (j == -1)
{
funError(vp);
}
fprintf(fp3, "<var name=");
fprintf(fp3, "\"%s\"", mas2[vp-1].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
return vp;
}
return vp;
}
int funVirojenie(int vp)
{
int t;
if(flexpr == 0){
fprintf(fp3, "<expr>");
flexpr = 1;}
t=funOperaciya(vp);
if(t != -1)
{
vp=funOperand(t);
vp=funOperand(vp);
if(flexpr == 1){
fprintf(fp3, "</expr>");
flexpr = 0;
fprintf(fp3, "\n");}
return vp;
//operaciya vp ukazivaet na slovo posle znaka
}
if(mas2[vp] == "(")
{
if(mas[vp+1] == s[4])
{
vp=vp+2;
t=vp;
vp=funOperand(vp);
if(vp != t)
{
if(mas2[vp] == ")")
{
if(flexpr == 1){
fprintf(fp3, "</expr>");
flexpr = 0;
fprintf(fp3, "\n");}
vp++;
return vp;
}
else
{
funError(vp);
}
}
else
{
funError(vp);
}
}
else
{
funError(vp);
}
}
vp=funOperand(vp);
if(flexpr == 1)
{
fprintf(fp3, "</expr>");
flexpr = 0;
fprintf(fp3, "\n");
}
//if(i != vp)
//{
// i=vp;
// vp=funOperand(vp);
//if(vp != i)
//{
// if(flexpr == 1){
// fprintf(fp3, "</expr>");
// flexpr = 0;
// fprintf(fp3, "\n");}
//}
//}
//fprintf(fp3, "</expr>");
//fprintf(fp3, "\n");
//vp++;//???
return vp;
}
int funOperaciya(int vp)
{
int i=0;
for(i=0;i<20;i++)
{
if(s2[i] == mas2[vp])
{
if((i > 2 && i < 9) || (i>14 && i<18) || (i == 19))
{
fprintf(fp3, "<op kind=");
fprintf(fp3, "\"%s\"", mas[vp].c_str());
fprintf(fp3, ">");
vp++;
return vp;//!!!
}
}
if(str[10] == mas2[vp])
{
fprintf(fp3, "<op kind=");
fprintf(fp3, "\"%s\"", mas[vp].c_str());
fprintf(fp3, ">");
vp++;
return vp;//!!!
}
}
return -1;
}
int funOperand(int vp)
{
int i=vp;
vp=funPeremennaya(vp);
if(vp != i){return vp;}
if((mas[vp] == "int" && mas2[vp] != "int") || (mas[vp] == "real" && mas2[vp] != "real"))
{
fprintf(fp3, "<");
fprintf(fp3, "\"%s\"", mas[vp].c_str());
fprintf(fp3, " val=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
vp++;
return vp;
}
i=vp;
vp=funVirojenie(vp);
if(vp != i){return vp;}
return vp;
}
int funPerehod(int vp)
{
vp++;
if(mas[vp] == "id")
{
fprintf(fp3, "<goto");
fprintf(fp3, " label=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
}
else
{
funError(vp);
}
vp++;
return vp;
}
int funUslovnii(int vp)
{
int i;
fprintf(fp3, "<if>");
fprintf(fp3, "\n");
vp++;
i=vp;
vp=funVirojenie(vp);
if(i == vp)
{
funError(vp);
}
if(mas2[vp] != "then")
{
funError(vp);
}
vp++;
do{
i=vp;
vp=funOperator(vp);
int y=0;//dlya proverki ';'
if(mas2[vp] == ";")
{
//vp++;
y=1;//';' vstretili -> pustoi
}
if(i == vp && y == 0)
{
funError(vp);
}
if(mas2[vp] != ";" && y == 0)
{
funError(vp);
}
vp++;
}while(mas2[vp] != "else" && mas2[vp] != "end");
if(mas2[vp] == "else")
{
vp++;
do{
i=vp;
vp=funOperator(vp);
int y=0;//dlya proverki ';'
if(mas2[vp] == ";")
{
//vp++;
y=1;//';' vstretili -> pustoi
}
if(i == vp && y == 0)
{
funError(vp);
}
if(mas2[vp] != ";" && y == 0)
{
funError(vp);
}
vp++;
}while(mas2[vp] != "end");
fprintf(fp3, "</if>");
fprintf(fp3, "\n");
vp++;
return vp;
}
else
{
if(mas2[vp] == "end")
{
fprintf(fp3, "</if>");
fprintf(fp3, "\n");
vp++;
return vp;
}
else
{
funError(vp);
}
}
return vp;
}
int funCikl(int vp)
{
int i=0;
fprintf(fp3, "<for>");
fprintf(fp3, "\n");
vp++;
i=vp;
vp=funOperator(vp);
//if(mas2[vp] == "end")
//{
// vp++;
// fprintf(fp3, "</for>");
// fprintf(fp3, "\n");
// return vp;
//}
//if(mas2[vp] == ";" && mas2[vp+1] == "end")
//{
// vp=vp+2;
// fprintf(fp3, "</for>");
// fprintf(fp3, "\n");
// return vp;
//}
//if(i == vp)
//{
// cout << "Error: net operatora v Cikle";
// exit(1);
//}
if(mas2[vp] == ";")
{
vp++;
do{
i=vp;
vp=funOperator(vp);
if(mas2[vp] == "end")
{
vp++;
fprintf(fp3, "</for>");
fprintf(fp3, "\n");
return vp;
}
if(i == vp)
{
vp++;
}
}while(mas2[vp] != "end");
}
if(mas2[vp] == "end")
{
vp++;
fprintf(fp3, "</for>");
fprintf(fp3, "\n");
return vp;
}
if(mas2[vp] != ";" || mas2[vp] != "end")
{
funError(vp);
}
return 0;
}
int funVvod(int vp)
{
vp++;
int i;
fprintf(fp3, "<read>");
fprintf(fp3, "\n");
i=vp;
vp=funPeremennaya(vp);
if(i == vp)
{
funError(vp);
}
do{
if(mas2[vp] == ",")
{
vp++;
i=vp;
vp=funPeremennaya(vp);
if(i == vp)
{
funError(vp);
}
}
if(mas2[vp] != ";" && mas2[vp] != "," && mas2[vp] != "end")
{
funError(vp);
}
}while(mas2[vp] != ";" && mas2[vp] != "end");
fprintf(fp3, "</read>");
fprintf(fp3, "\n");
return vp;
}
int funVivod(int vp)
{
vp++;
fprintf(fp3, "<write>");
fprintf(fp3, "\n");
int i;
i=vp;
vp=funSpecifikator(vp);
if(i == vp)
{
vp=funVirojenie(vp);
if(i == vp)
{
funError(vp);
}
}
do
{
int j=0;
if(mas2[vp] == ",")
{
j=1;//vstretili ','
vp++;
if(mas2[vp] == "end")//kostil
{
funError(vp);
}
if(mas2[vp] == ";")//kostil
{
funError(vp);
}
i=vp;
vp=funSpecifikator(vp);
if(i == vp)
{
vp=funVirojenie(vp);
if(i == vp)
{
funError(vp);
}
}
}
if(j == 0 && mas2[vp] != ";" && mas2[vp] != "end")
{
funError(vp);
}
}while(mas2[vp] != ";" && mas2[vp] != "end");//
fprintf(fp3, "</write>");
fprintf(fp3, "\n");
return vp;
}
int funSpecifikator(int vp)
{
if(mas2[vp] == "skip" || mas2[vp] == "space" || mas2[vp] == "tab")
{
fprintf(fp3, "<qualifier kind=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp++;
return vp;
}
return vp;
}
int funVizov(int vp)
{
int i;
vp++;
if(mas[vp] == "id" && mas2[vp+1] == "end")
{
fprintf(fp3, "<call name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
fprintf(fp3, "</call>");
fprintf(fp3, "\n");
vp=vp+2;
return vp;
}
else if(mas[vp] != "id")
{
funError(vp);
}
else
{
fprintf(fp3, "<call name=");
fprintf(fp3, "\"%s\"", mas2[vp].c_str());
fprintf(fp3, "/>");
fprintf(fp3, "\n");
vp++;
i=vp;
vp=funPeremennaya(vp);
if(i == vp)
{
funError(vp);
}
do
{
if(mas2[vp] == ",")
{
vp++;
i=vp;
vp=funPeremennaya(vp);
if(i == vp)
{
funError(vp);
}
}
}while(mas2[vp] != "end");
fprintf(fp3, "</call>");
fprintf(fp3, "\n");
vp++;
return vp;
}
}
int funBreak(int vp)
{
fprintf(fp3, "</break>");
fprintf(fp3, "\n");
vp++;
return vp;
}
int funError(int e)
{
fclose(fp2);
fclose(fp3);
cout << "Error: " << mas2[e] << ": " << e;
exit(1);
return 0;
}
@Stat1cV01D
Copy link

Le fucking Peace-death, mon ami %)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment