URL: https://linuxfr.org/forums/programmation-c--2/posts/probleme-analyseur-lexical-flex Title: Problème analyseur lexical flex Authors: NotsosmartChicken Date: 2017年02月23日T09:50:43+01:00 License: CC By-SA Tags: c et flex Score: 0 Bonjour, Je viens vers vous car je débute avec flex et j'aurais bien besoin de conseils pour me débloquer ... Je cherche à créer un parser avec flex qui va récupérer les noms de fonctions dans un header afin de les réutiliser plus tard. Je désire stocker ces noms de fonctions dans un tableau que j'alloue dynamiquement car je souhaite pouvoir parser n'importe quel header avec (donc n'importe quel nombre de fonctions). Dans les règles de production flex afin d'identifier les mots j'ai indiqué : ```C [a-zA-Z0-9]+ {if(yyin==hfile){ if(strcmp(yytext,"extern")==0){ fctON=1; cptWords=0;} else if(fctON==1){ allocationfonctions(yytext);} else{ECHO;} } else if(yyin==cfile){ ECHO; } } ``` qui lorsque je suis en train de parser le header hfile va identifier "extern" et faire appel à la fonction allocationsfonctions ci dessous pour les mots suivants jusqu'à rencontrer le caractère '(' qui repassera fctON à 0 (code non indiqué ici). La fonction allocationfonctions : ```C int allocationfonctions(char* data){ int i=0; char* word = strcat(data,""); printf("mot a stocker : %s\n",word); if(cptWords==0){ if(n>=5){ fonctions = realloc(fonctions, (n+2)*sizeof(chaine)); if(fonctions==NULL){ printf("erreur lors de la reallocation\n"); return -1;} fonctions[n]="fctname"; fonctions[n+1]="fctname"; } fonctions[n+1]=word; printf("result : fonctions[%d]=%s\n",n+1,fonctions[n+1]); cptWords++; n+=2; return 1;} else{ if(n>=5){ fonctions = realloc(fonctions, (n+1)*sizeof(chaine)); if(fonctions==NULL){ printf("erreur lors de la reallocation\n"); return -1;} fonctions[n]="fctname";} fonctions[n]=word; printf("result : fonctions[%d]=%s\n",n,fonctions[n]); cptWords++; n+=1; return 1;} } ``` Le passage par un paramètre word avec strcat était une tentative d'éviter le problème que je pointe plus bas avec l'ajout d'un caractère nul, mais ça n'a rien changé Et voici le main qui initialise tout ça : ```C int main(void) { int i; hfile = fopen("HelloWorld.h", "r"); if (!hfile) { printf("I don't know where to read\n"); return -1; } cfile = fopen("HelloWorld.c", "r"); if (!cfile) { printf("I don't know where to read\n"); return -1; } thatfile = fopen("result.c", "w"); if (!thatfile) { printf("I don't know where to write\n"); return -1; } chaine=malloc(8*sizeof(char)); if(chaine==NULL){ printf("erreur de memoire - ligne\n"); return 0; } fonctions=malloc(5*sizeof(chaine)); if(fonctions==NULL){ printf("erreur de memoire - tableau\n"); return 0; } for(i=0;i<5;i++){ fonctions[i]="fctname"; printf("valeur defaut fonction[%d]:%s\n",i,fonctions[i]); } yyin = hfile; yylex(); printf("end of first parsing\n\n\n"); for(i=0;i

AltStyle によって変換されたページ (->オリジナル) /