1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
struct registre
{
char *unite ;
char *attribut;
};
char mc[][20]= {"program","debut","fin","si","alors","tantque","faire","var","tableau","entier","et","non","ou","pour","de","réel"};
FILE *f;
char *chemin=NULL;
struct registre regis_mc,regis_id,regis_op,regis_nb,regis_nbr;
char *symbole=NULL;
FILE *f;
char elem;int res;
int is_sep(char x) //verifie si c'est un separateur;
{
int r=0;
if ( (x==' ')||(x=='\n') )
r=1 ;
return(r);
};
char *anal_lex(char elem,int *res, struct registre *regis_mc,struct registre *regis_op,struct registre *regis_id,struct registre *regis_nb,struct registre *regis_nbr,FILE *f)
{
int i,j,nb;
float nbr;char *s=NULL;
if(is_sep(elem)==0)
{
switch (elem) {
case ';' :strcpy(regis_op->unite,"pv");
strcpy(regis_op->attribut,"0");
return("pv");
break;
case '=' : strcpy(regis_op->unite,"oprel");
strcpy(regis_op->attribut,"EGA");
return("oprel");
break;
case ':': elem=fgetc(f);
if(elem=='='){
strcpy(regis_op->unite,"opaff");
strcpy(regis_op->attribut,"0");
}
else
break;
case '<' : strcpy(regis_op->unite,"oprel");
elem=fgetc(f);
if (elem=='='){
strcpy(regis_op->attribut,"PPE");
}
else if(elem=='>')
strcpy(regis_op->attribut,"DIF");
else
strcpy(regis_op->attribut,"PPQ");
break;
case '>' : strcpy(regis_op->unite,"oprel");
elem=fgetc(f);
if (elem=='='){
strcpy(regis_op->attribut,"PGE");
}
else
strcpy(regis_op->attribut,"PGQ");
break;
case '(' :strcpy(regis_op->unite,"oprel");
break;
case ')' :strcpy(regis_op->unite,"oprel");
break;
case '+' :strcpy(regis_op->unite,"oprel");
break;
case '*' :strcpy(regis_op->unite,"oprel");
default :
i=0;
do
{
s[i]=elem;
elem=fgetc(f);
i++;
}
while(is_sep(elem)==0);
s[i]='\0';
j=0;
while((*res==0)&&(j<15))
{
if (strcmp(s,mc[j])==0)
{ *res=1 ;
strcpy(regis_mc->unite,s);
strcpy(regis_mc->attribut,"0");
return(s);
}
else
j++;
}
if (*res==0) {
if((isdigit(s[0]))&&(!(nb=atoi(s)))&&(!(nbr=atof(s))))
printf("%s --->ID INCORRECTE (debut avec un chiffre)\n",s);
else if (nb=atoi(s)) {
strcpy(regis_bn->unite,"nb");
strcpy(regis_id->attribut,s);
return("nb");
*res=2;
}
else if (nbr=atof(s)){
strcpy(regis_nbr->unite,"nbr");
strcpy(regis_nb->attribut,s);
return("nbr");
*res=3;
}
else {
strcpy(regis_id->unite,"id");
strcpy(regis_id->attribut,s);
return("id");
*res=4;
}
}
}//fermeture du switch
}//fermeture du if
}//fermeture du fonction
int main()
{
printf("\nENTRER LE CHEMIN DU FICHIER A ANALYSER:");
scanf("%s",&chemin);
f=fopen(chemin,"r");
if (f==NULL)
{printf("LE FICHIER EST INTROUVABLE !\n");}
else
{
printf("la fichier s'est ouvert");
res=0;
while ((elem=fgetc(f))!=EOF){
symbole=anal_lex(elem,&res,®is_mc,®is_op,®is_id,®is_nb,f);
if(res==0) {
printf("%s /n %s",regis_op.unite,regis_op.attribut);
}
else if(res==1){
printf("%s /n %s",regis_mc.unite,regis_mc.attribut);
}
else if (res==2) {
printf("%s /n %s",regis_nb.unite,regis_nb.attribut);
}
else if(res==3){
printf("%s /n %s",regis_nbr.unite,regis_nbr.attribut);}
else {
printf("%s /n %s",regis_id.unite,regis_id.attribut);
}
}//fermeture du while
}//fermeture du else
fclose(f);
system("pause");
return 0;
} |
Partager