c'est bien d'utiliser flex and yacc sous unix pour faire une analyse lexicale
une programe qui fait l'analyse lexicale SQL
كود:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
char *mc[10]={"create","select","insert","into","from","all","table","values","where","sum"};
char *sep[10]={" ","(",")",",",":",";","."};
char *op[20]={"=","<",">","!=","==",">=","<=","+","-","/","*"};
char *id[30];
char *num[30];
char f[40];
int ismc(char* a)//recherche des mots clé
{
int i=0;
while (mc[i]!=NULL)
{
if(strcmp(mc[i],a)) i++;
else return 1;
}
return 0;
}
int issep(char* a)//recherche des separateurs
{
int i=0;
while (sep[i]!=NULL)
{
if(strcmp(sep[i],a)) i++;
else return 1;
}
return 0;
}
int isop(char *a)//recherche des operateurs
{
int i=0;
while (op[i]!=NULL)
{
if(strcmp(op[i],a)) i++;
else return 1;
}
return 0;
}
int main(void)
{
int jj=0;
printf("entrer une chaine:");
char p[100];
gets(p);
char l[2];char m[2];
int i=0;int k=0;
int j=0,cpt=0,d,a,b;
while (p[i]!='\0')
{
l[0]=p[i];l[1]='\0';
if(isalnum(p[i])) d=1;
else d=0;
a=issep(l);b=isop(l);
switch(d)
{
case 0:
if(issep(l)) {i++;}
else if(isop(l)){
m[0]=p[i+1];
m[1]='\0';
if (issep(m)|| isalnum(p[i+1])) {printf("\noperateur:'%s'\n",l); i++; }
else {if(isop(strncat(l,m,1))&&(m[0]!='\0')) {printf("\noperateur:'%s'\n",l);i++;i++;}
else {l[1]='\0';printf("\noperateur:'%s'\n",l);i++;}}}
else {
m[0]=p[i+1];
m[1]='\0';
if(isop(strncat(l,m,1))&&(m[0]!='\0')) {printf("\noperateur:'%s'\n",l);i++;i++;}
else {l[1]='\0';printf("\nerror lexiqal: operateur inconnu '%s' \n",l);i++;}}
break;
case 1:
jj=0;
while(isalnum(p[i]))
{
while(isdigit(p[i]))
{
f[jj]=p[i];
f[jj+1]='\0';
i++;
jj++;
}
if(jj) { num[k]=f;
printf("\nnombre[%d]:%s\n",k,num[k]);
k=k+1;
break;
}
while(isalnum(p[i]))
{
f[jj]=p[i];
f[jj+1]='\0';
jj++;
i++;
}
if(ismc(f)==0){
id[j]=f;
printf("\nidentificateur[%d]:%s\n",j,id[j]);
j=j++;}
else printf("\nmot cle:%s\n",f);
}
}
}
getch();
}