Skip to content

Instantly share code, notes, and snippets.

@spellancer
Created May 1, 2012 09:35
Show Gist options
  • Save spellancer/2566792 to your computer and use it in GitHub Desktop.
Save spellancer/2566792 to your computer and use it in GitHub Desktop.
lexem.h -- homework2
#ifndef lexem_h
#define lexem_h
#include <iostream>
using namespace std;
// ----------
char* del (char *str)
{
int i,j,n;
for (char *p = str; *p; p++) *p = tolower(*p);
for (i=0;i<strlen(str);i++)
{
if (str[i]==' ')
{
for (j=i;j<strlen(str);j++)
{
str[j]=str[j+1];
}
i=i-1;
}
}
return str;
}
// -----------
// -----------
int number (char *str)
{
int i,n=0,m=0;
for (i=0;i<strlen(str);i++)
{
if (str[i]==':')
n++;
else if (str[i]==';') m++;
}
if (n==m && n!=0)
return n;
else
if (n!=m)
{
cout << "Ошибка! Проверьте количество переменных и символов ':' и ';' " << endl;
n = -1;
return n;
}
}
// -----------
// -----------
bool checktypear (char *type)
{
bool k;
char ar[23] = "array[char,byte]ofreal";
char ar2[23] = "array[byte,char]ofreal";
char ar3[23] = "array[char,byte]ofchar";
char ar4[23] = "array[byte,char]ofchar";
char ar5[26] = "array[char,byte]ofinteger";
char ar6[26] = "array[byte,char]ofinteger";
char ar7[23] = "array[char,byte]ofbyte";
char ar8[23] = "array[char,byte]ofbyte";
k=true;
for (int i=0;i<strlen(type);i++)
{
if (type[i]!=ar[i])
if (type[i]!=ar2[i])
if (type[i]!=ar3[i])
if (type[i]!=ar4[i])
if (type[i]!=ar5[i])
if (type[i]!=ar6[i])
if (type[i]!=ar7[i])
if (type[i]!=ar8[i])
k=false;
}
return k;
}
// -----------
// -----------
bool checkid (char *id)
{
int i,j,m,n;
char res;
bool key=false,key2=false;
bool r;
char num[11] = "0123456789";
char sym[16] = "!@#$%^&*()-=+/\|";
for (i=0; i <10;i++)
{
if (id[0]==num[i])
key2=true;
}
for (i=0; i <strlen(id);i++)
{
for (j=0;j<16;j++)
{
if (id[i]==sym[j])
key=true; //есть ошибка
}
}
if (key==true || key2==true)
r = false;
else r = true;
return r;
}
// -----------
bool checktypeset (char *type)
{
int i,j,m,n;
char *res;
char st1[10]="setofchar";
char st2[10]="setofreal";
char st3[10]="setofbyte";
char st4[13]="setofinteger";
bool k;
k=true;
for (int i=0;i<strlen(type);i++)
{
if (type[i]!=st1[i])
if (type[i]!=st2[i])
if (type[i]!=st3[i])
if (type[i]!=st4[i])
k=false;
}
return k;
}
bool checktypestr (char *type)
{
int i,j;
char st[7]="string";
char st1[11]="0123456789";
bool k1;
k1=true;
for (i=0; i<6;i++)
{
if (type[i]!=st[i])
k1 = false;
}
int n = strlen(type);
int n1 = n-1;
int n2 = n-2;
if (n!=6)
{
if (k1==true)
{ cout <<"Ищем скобку: " << type[6] << " Ищем закр. скобку: " << type[n1]<<endl;
if (type[6]!='[' || type[n1]!=']')
k1 = false;
}
if (k1==true)
{
k1=false;
i=7;
while (type[i]!=']')
{
for (j=0;j<strlen(st1);j++)
{
if (type[i]==st1[j])
k1=true;
}
i++;
}
}
}
return k1;
}
// -----------
char* check ( char *ch)
{
int i,j,m,n;
char *res, id[40], type[40],*idd;
idd = new char;
res = new char;
bool k,ar,set,str,rec;
i=0;
j=0;
while (ch[i]!=':')
{
id[i]=ch[i];
i++;
}
id[i]='\0';
k = checkid (id);
if (k==true)
idd = "i";
else
idd = "E";
//cout << "результат проверки id " << k <<endl;
if (ch[i]==':')
{
i++;
for (i;i<strlen(ch);i++)
{
type[j]=ch[i];
j++;
}
type[j]='\0';
}
ar = checktypear (type);
set = checktypeset (type);
str = checktypestr (type);
i=0;
if (ar==true)
{
res = "a[n,m]ot";
strcat (idd , res);
}
else
if (set==true)
{
res = "sot";
strcat (idd , res);
}
else
if (str==true)
{
if (strlen(type)!=6)
{
res = "s[k]";
strcat (idd , res);
}
else
{
res = "s";
strcat (idd , res);
}
}
else
{
res = "ET";
strcat (idd , res);
}
//cout << "результат проверки на массив:" << ar <<endl;
return idd;
}
// -----------
char* lex (char *str)
{
int i,j,n,k,a,in=0;
char *res, id[40] , type[40], *ch ,*res1;
bool key,kid,ktype;
//res = str;
res = new char ;
ch = str;
kid=false;
ktype=false;
key = false;
if (str[0]=='v' && str[1]=='a' && str[2]=='r')
{
res[in] = 'v';
in++;
key = true;
}
else cout << "Ошибка! Отсутствует ключевое слово var!" <<endl;
res[in] = '\0';
j=0;
i=3;
n=0;
n = number (str);
if (key==true && n>0)
{
while (n!=0)
{
j=0;
while (str[i]!=';')
{
ch[j] = str[i];
j++;
i++;
}
ch[j] = '\0';
res1 = check (ch);
//cout <<"до --"<< res <<endl;
strcat (res, res1);
if (str[i]==';')
i++;
//cout <<"проверка -- " << res << endl;
n=n-1;
}
}
//res[in] = '\0';
return res;
}
#endif lexem_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment