Analyseur syntaxique du language pascal (analyse descendante recursive)

Description

en utilisant l'analyse descendante récursive, on réalisera un analyseur syntaxique du langage pascal.
les contraintes a respecter: il faut que les unités lexicales soient séparées par des espaces.

Source / Exemple :


/************analyseur syntaxique**************/
/************de la grammaire du Language Pascal **************/
/************en utilisant l analyse descendante recursive**************/
/************realisé par : BENKADJA ABDELLAH**************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
  int fct_chiffre(char c);
  int fct_lettre(char c);
  int fct_operateur(char c);
  int fct_Identificateur(char *tab,int d,int f);
  int fct_entier(char *tab,int d,int f);
  void affiche(char *tab,int d,int f);
  void fct_test(char  tab[]);//pour determiner les lettres,les chiffres et les operateur
  void bloc();
  void corps();
  void declaration();
  void exp();
  void exp_simple();
  void facteur();
  void inst();
  void list_id();
  void list_inst();
  void list_type();
  void pgauche();
  void prgm();
  void relop();
  void terme();
  void type();
  int is_addop();
  int is_mulop();
  int is_cste(char tab[]);
  int is_id(char tab[])
  {

      return(fct_lettre(tab[0]));
  }
  int is_cste(char entier[])
  {
      int r=1;
      for(int i=0;i<strlen(entier);i++)
        {
            if(!fct_chiffre(entier[i])) r=0;
        }
        return r;
  }
  int d=0,f;

  char sym[20];
  char phrase[200];
int compar(char s1[], char s2[])
{
    int i=0;
    while(i<strlen(s1))
    {
        if(strlen(s1)!=strlen(s2)) return 0 ;
        else if (s1[i]!=s2[i]) {return 0; break ;}
        else i++;
    }
    if (i==strlen(s1)) return 1;
}

void cut(char phrase1[],int a, int b)
{
    for(int k=0;k<20;k++) sym[k]=NULL;
    int i,j;
    j=a;i=0;
    while(i<=b-a)
    {
        sym[i]=phrase1[j];
        j++;i++;
    }
}
void next()
{
    for(int k=0;k<20;k++) sym[k]=NULL;
    int i= d;
    while(phrase[i]!=' '&& i<strlen(phrase)+1) i++;
    if(phrase[i]==' '|| i==strlen(phrase)+1)
         {
         f=i-1;
          if(fct_Identificateur(phrase,d,f))
          {
              cut(phrase,d,f);

          }
          else if (fct_entier(phrase,d,f))
          {
              cut(phrase,d,f);
          }
             else
             {
             cut(phrase,d,f);

             }
             d=f+2;
             i=f+1;
        }
        i++;
}
void type(){
if(compar(sym,"integer")) {printf("\n%s : type accepted ",sym) ; next();}
else if (compar(sym,"array"))
{
    printf("\n%s :accepted ",sym) ;
    next();
    if(compar(sym,"["))
    {
        printf("\n%s :accepted ",sym) ;
        next();
        if(is_cste(sym))
        {
            printf("\n%s :accepted ",sym) ;
            next();
            if (compar(sym,".."))
            {
                printf("\n%s :accepted ",sym) ;
                next();
                if(is_cste(sym))
                {
                    printf("\n%s :accepted ",sym) ;
                    next();
                    if(compar(sym,"]"))
                    {
                        printf("\n%s :accepted ",sym) ;
                        next();
                        if(compar(sym,"of"))
                        {
                            printf("\n%s :accepted ",sym) ;
                            next();
                            if(compar(sym,"integer")) {printf("\ndeclaration du tableau reussi ");next();}else printf("\nerreur->%s type non valide",sym);

                        }else printf("\nerreur->%s non valide pour la declaration d un tableau",sym);
                    }else printf("\nerreur->%s non valide pour la declaration d un tableau",sym);
                }else printf("\nerreur->%s non valide pour la declaration d un tableau",sym);
            }else printf("\nerreur->%s non valide pour la declaration d un tableau",sym);
        } else printf("\nerreur->%s non valide pour la declaration d un tableau",sym);
    }else printf("\nerreur->%s non valide pour la declaration d un tableau",sym);
} else printf("\nerreur->%s type non valide",sym);
}
void list_id()
{
    if(is_id(sym)&& !compar(sym,"begin"))
    {
    printf("\n%s->ID valide",sym);
    next() ;
    } else if(compar(sym,"begin")&& !is_id(sym))printf("\nerreur->ID %s non valide",sym);
    while(compar(sym,",")) { next(); list_id();}
}

void list_type()
{
list_id();
if(compar(sym,":")){
    next(); type();
    if(compar(sym,";")){
    next(); list_type();
    }

    } else if(!compar(sym,"begin")) printf("\nerreur: declaration des variables");
}
void declaration()
{
    if (compar(sym,"var")){
    next(); list_type();
    } else printf("\naucune declalation validée");
}

void exp_simple()
{
    terme();
    if(is_addop())
    {
        printf("\n addop reussi");
        next();
        exp_simple();
    }

}

void exp()
{
    exp_simple();
    relop();
    exp_simple();
    printf("\n expression verifie");
}

void facteur()
{
    if(compar(sym,"not")) { next(); exp();}
    else if(is_cste(sym)) {printf("\n%s->constante ",sym); next();}
    else if(compar(sym,"("))
    {
        next();
        exp();
        if(compar(sym,")")) {printf("\nparentheses verifiees");next();}
        else printf("\nerreur parenthese manquante");
    }
    else pgauche();
}

void terme()
{
    facteur();
    if (is_mulop())
    {
        printf("\nmulop reussi ");
        next();
        terme();
    }

}

void inst()
{
    if(compar(sym,"if"))
    {
        printf("\n if verifie");
        next();
        exp();
        if (compar(sym,"then")){ printf("\nparours then ");next(); corps();} else printf("\nerror: syntaxe if non verifie ");

    }
    else if(compar(sym,"while"))
    {
        printf("\n while verifie");
        next();
        exp();
        if (compar(sym,"do")){ printf("\nparours do ");next(); corps();} else printf("\nerror: syntaxe if non verifie ");

    }
    else
    {
        pgauche();
        if (compar(sym,":="))
        {
            printf("\n%s->accepte ");
            next();
            exp_simple();
        } else printf("\n erreur syntaxt d instruction - id := terme");
    }
}
void list_inst()
{
    inst();
    while(compar(sym,";")){
    next();
    inst();
    }
    printf("\ngrpe d instruction verifie ");

}
void corps(){
if(compar(sym,"begin"))
{
    next();
    list_inst();
    if(compar(sym,"end"))
    {
        printf("\nsyntax verifie ");
        next();
        printf("\nL ANALYSE SYNTAXIQUE TERMINE");
    } else printf("\nerreur: syntaxe non valide pour le grpe d instructions");

} else printf("\nerreur: begin ");
}
void bloc()
{
    declaration();
    corps();
}
void prgm()
{

    if (compar(sym,"program")){
        printf("\n%s->accepted\n",sym);
        next();
        if(is_id(sym))
            {
            printf("\n%s->accepted\n",sym);
            next();
            if (compar(sym,";")){ next(); bloc();}
            else printf("\nerreur: ; oubliee");
            } else printf("\nerreur: ident prog est faut ") ;
        } else printf("\nerreur: prgm");
}

void relop()
{
    if (compar(sym,"=")||compar(sym,"<")||compar(sym,"<=")) {printf("\n%s->operateur",sym); next();} else
    if (compar(sym,"<>")||compar(sym,">=")||compar(sym,">")) {printf("\n%s->operateur",sym); next();} else
    printf("\nerreur : %s operateur non valide  ", sym );
}

void pgauche()
{
    if(is_id(sym)) {printf("\npgauche reussi "); next();} ;
}

int is_mulop()
{
    if(compar(sym,"*")||compar(sym,"/")||compar(sym,"and")) return 1;
    else return 0;
}
int is_addop()
{
    if(compar(sym,"+")||compar(sym,"-")||compar(sym,"or")) return 1;
    else return 0;
}
int main() {

printf("\n\n");
printf("******************>>>>>>>>>>>>ANALYSEUR SYNTAXIQUE<<<<<<<<<<<<<<<**************** ");
printf("\nrealisé par : BENKADJA ABDELLAH******* ");
printf("\n\n \t ****||enter une pharse\n" );
printf("\n\n \t \t\t ***||" );

gets(phrase);
    next();
    prgm();

    getchar();
    return 0;
}
int fct_chiffre(char c){
    if (c>='1' && c<='9')return 1;
    return 0;
}
int fct_lettre(char c){
    if( (c>='a' && c<='z') ||(c>='A' && c<='Z'))return 1;
    return 0;
}
int fct_Identificateur(char *tab,int d,int f){
 if(!fct_lettre(tab[d])) return 0;
  while(d+1<=f+1){
      if(!fct_lettre(tab[d]) && !fct_chiffre(tab[d]) &&  tab[d]!='_') return 0;
      else d++;
  }
    return 1;
}
int fct_entier(char *tab,int d,int f){
 while(d<=f){
     if(!fct_chiffre(tab[d])) return 0;
        else d++;
            }
    return 1;
}
void affiche(char *tab,int d,int f){
     printf("\n\t");
     while(d<=f){
     printf("%c",tab[d]);
     d++;
               }
}
int fct_operateur(char c){
char operateur[10]="<>=+-*/!%";
int i=0;
for(i;operateur[i];i++){
    if(c==operateur[i])return 1;
    }
return 0;
}

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.