Accueil > > > ANALYSEUR SYNTAXIQUE DU LANGUAGE PASCAL (ANALYSE DESCENDANTE RECURSIVE)
ANALYSEUR SYNTAXIQUE DU LANGUAGE PASCAL (ANALYSE DESCENDANTE RECURSIVE)
Information sur la source
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
- /************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;
- }
/************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;
}
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
analyseur lexical et syntaxique pour un langage pascal programmer en c++ [ par talzou ]
j'ai besoins d'un analyseur lexical et syntaxique de langage pascal programmer en devc++merci
Analyse lexicale / syntaxique [ par Cartman13 ]
Bonjour a tous.Je bosse actuellement sur un analyseur sémantique en C++.Concrètement le fichier est le avec ifstream (flux d'entrée) et stocké dans un
Analyse syntaxique et generation de code intermediaire [ par mohyiddine18 ]
bonjour a tous, Je veux obtenir de l'aide à représentées dans le: écriture d'un analyseur syntaxique et generateur de code intermediaire pour des s
triangle de Pascal [ par philouxy ]
Bonsoir à tous, voilà je me remets gentillement à la programmation, et je fais mes premiers pas en C++. En voulant reprendre un ancien exercice sur l
analyse syntaxique [ par fadiam ]
Bonjour à tous. je cherche à réaliser une fonction qui modifie une chaine de caractères.elle n'accepte que les "chiffres" de 0 à 9 et quelques autres
Algorithme et analyse structurée d'un probleme [ par djibril57100 ]
Bonjour je souhaiterais savoir qui pourrais m'apporter de l'aide pour la réalisation de mon [url=http://www.facebook.com/photo.php?pid=714235&l=4fc674
stack overflow [ par ssana83 ]
Bonjour, j'ai réalisé un programme de création d'un arbre donc j'ai utilisé une fonction récursive. J'ai essayé le programme ça marche sauf avec certa
arbre syntaxique [ par bahrounitarek ]
comment je peut faire un programme qui fait un arbre syntaxique à partir d'une raquette sql par exemple : select * from etudiant where nom="Mohamed
flex & bison [ par roubalist21 ]
bonsoir tous le monde, je voudrais déclarer un tableau dans flex quand il finit son analyse lexical et le remplir dans bison durant l'analyse syntaxiq
analyse du protocole de routage SPEED [ par baghdadmoussa ]
bonjour, salut, je veut réaliser une application qui a le rôle d'un analyse du protocole de routage SPEED (appliquée aux résaux de capteurs sans fil
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
MATRICE TEMPLATEMATRICE TEMPLATE par hjr2610
Cliquez pour lire la suite par hjr2610 RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|