Table de symbole

Un article de Wikipédia, l'encyclopédie libre.

Voici l'algorithme donnant la table des symboles.

[modifier] Algorithme

#include <stdio.h>
#include <string.h>
char c,t[20] ;

int test(char ch[20])
{
   int j,k;
   char  MC[35][30]={"include","auto","default","float","register","struct","volatile","break","do",
   "for","return","switch","while","case","double","goto","short","typedef","char","else","if","signed", 
   "union","const","enum","int","sizeof","unsigned","continue","extern","long","static",
   "void","include"};

   k=1;
   for (j=0;j<34;j++)
   { 
      if (strcmp(ch,MC[j])==0) {
         k=0;
         break;
      }
   }
   if (k) return 0 ;
   else return 1 ;
}
void main(void) 
{
   int i,n,ok;
   int coment=0;
   int comento=0;
   char a,nom_fich[30] ;
   char sep[25]={'     ','&','/','<','>','#',';',',','\"','\,'\n','\a','(',')','\t','\\','\0', 
   '+','=','-','*','{','|','}'};
   FILE * fich ;
   do
   {
      printf("Donner le nom de la source c :");
      scanf("%30s",nom_fich);
   } while (!(fich= fopen(nom_fich,"r")));

   while (!feof(fich))
   { 
      c=fgetc(fich);
      if ((c=='/')&&(a=='/')) comento=1;
      if (c=='\n') comento=0;
      if ((c=='*')&&(a=='/')) coment=1;
      if ((c=='/')&&(a=='*')) coment=0;
      if ((coment==0)&&(comento==0))
      { 
         ok=1;
         for (i=0;i<24;i++)
         {
            if (c==sep[i])
            {
               ok=0 ;
               break;
            }
         }
         if (ok==1) {
            n=strlen(t);
            t[n]=c;
            t[n+1]='\0';
         }
         else
         {
            if ((test(t)==0)&&(strlen(t))) printf("%s ",t);
            strcpy(t,"");
         }
      } 
      a=c;
   }
   fclose(fich);
   getch();
}