Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лабы / Рекурс. спуск / REC_SP~1
.CPP//#include <iostream.h>
//#include <string.h>
//#include <conio.h>
//#include <ctype.h>
#include <stdio.h>
#include "E:\122.cpp"
int err= 0;
int spec = 0;
int next= 0;
int flag=0;
//бЁ¬ў®«л ¤®ЇгбЄ Ґ¬лҐ ў Ё¬ҐЁ д ©«
char file_sym[] = {'.',':','\\','~','#','!','@','$'};
char lent[120];
int fname_sym(char c);
int exp_sym(char c);
int iden_sym(char c);
void is_iden(int i)
{
if(!iden_sym(lent[i]))
{
err = i+1;
if((lent[i] == ' ') || (lent[i] == '\n') || (lent[i] == 0) || (lent[i] == 9))
err = 0;
next = i+1;
return;
}
is_iden(i+1);
}
void is_expr(int i)
{
if(!exp_sym(lent[i]))
{
err = i+1;
if((lent[i]== ' ') || (lent[i] == '\n') || (lent[i] == 0) || (lent[i] == 9))
err = 0;
next = i+1;
return;
}
is_expr(i+1);
}
void is_fname(int i)
{
if(!fname_sym(lent[i]))
{
err = i+1;
if((lent[i] == '"') || (lent[i] == '>'))
err = 0;
next = i+1;
return;
}
is_fname(i+1);
}
void is_specw(int i)
{
char st[10];
int j=0;
while (isalpha(lent[i]))
{
st[j] = lent[i];
i++;
j++;
}
st[j] = 0;
next = i+1;
if (strcmp(st,"include") == 0) {spec = 0; return;}
if (strcmp(st,"define") == 0) {spec = 1; return;}
if (strcmp(st,"ifdef") == 0) {spec = 2; return;}
if (strcmp(st,"ifndef") == 0) {spec = 3; return;}
if (strcmp(st,"else") == 0) {spec = 4; return;}
err = i;
}
int fname_sym(char c)
{
if((c >= '0') && (c <= '9')) return 1;
if((c >= 'A') && (c <= 'Z')) return 1;
if((c >= 'a') && (c <= 'z')) return 1;
for (int i=0; i<strlen(file_sym)-1; i++)
if (c == file_sym[i]) return 1;
return 0;
}
int exp_sym(char c)
{
if((c >' ') && (c <= '~')) return 1;
return 0;
}
int iden_sym(char c)
{
if((c >= '0') && (c <= '9')) return 1;
if((c >= 'A') && (c <= 'Z')) return 1;
if((c >= 'a') && (c <= 'z')) return 1;
if(c == '_') return 1;
return 0;
}
int razbor_str(char * str)
{
if(str[0] != '#') {err = 1; return 1;}
is_specw(1);
if (err) return 1;
while (str[next] == ' ') next++;
if ((spec != 4) && ((lent[next] == '\n') || (lent[next] == 0))) {err = next; return 1;}
switch (spec) {
case 0: if ((str[next] != '"') && (str[next] != '<')) {err = next; return 1;};
is_fname(next+1);
while (str[next] == ' ') next++;
if ((str[next] != 0) && (str[next] != '\n'))
{err = next+1; return 1;};
break;
case 1: is_iden(next);
if (err) return 1;
while (str[next] == ' ') next++;
if ((str[next] == 0) || (str[next] == '\n')) {err = next; return 1;};
is_expr(next);
while (str[next] == ' ') next++;
if ((str[next] != 0) && (str[next] != '\n'))
{err = next+1; return 1;};
break;
case 2:
is_iden(next);
while (str[next] == ' ') next++;
if ((str[next] == 0) || (str[next] =='\n')) err=0;
else err=next;
break;
case 3: is_iden(next);
while (str[next] == ' ') next++;
if ((str[next] == 0) || (str[next] =='\n')) err=0;
else err=next;
break;
case 4:
is_iden(next);
while (str[next] == ' ') next++;
if ((str[next] == 0) || (str[next] =='\n')) err=0;
else err=next;
break;
}
if (err) return 1;
return 0;
}
main()
{
clrscr();
FILE* fin;
fin = fopen("res.cc","r");
if(!fin)
{
cout<<"Ґ г¤ Ґвбп ®вЄалвм д ©«";
getch();
return 1;
};
while(!feof(fin))
{
fgets(lent,80,fin);
if (feof(fin)) break;
err = 0;
razbor_str((char*)lent);
cout<<endl<<lent<<" Error: "<<err<<" symbol";
};
fclose(fin);
getch();
return 0;
}
Соседние файлы в папке Рекурс. спуск