IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

problème avec une structure en C


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 17
    Par défaut problème avec une structure en C
    Bonjour ,
    J'ai un problème concernant ma structure en C (infoIndent) , je n'arrive pas à la parcourir (dans la fonction chercher) . Mon but est de reconnaître les différents lexèmes pour simuler une petite analyse lexical d'un compilateur avec un fichier flex.

    embelisseur.fl
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    %{
    #include <string.h>
    #include "codes.h"
     
    int max=20;
    int indentation=0;
    int nbIndent = 0;
    typedef struct InfoIdent {
      const char* texte; /* le mot-cle ou l’ident proprement dit */
      int code_mot_cle; /* code du mot-cle, ou code IDENT */
    } InfoIdent;
     
    struct InfoIdent table[] = 
      { {"faire",FAIRE},{"entier", ENTIER}, {"reel",REEL},{"chaine",CHAINE},{"<-",AFFECTER},
       {"tableau",TABLEAU},{"si",SI},{"alors",ALORS},{"finsi",FINSI},
       {"sinon",SINON},{"tantque",TANTQUE},{"fintantque",FINTANTQUE},{"ou",OU},{"et",ET},{"non",NON},
       {"lire",LIRE},{"afficher",AFFICHER},{"chaine",NEWCHAINE },{"=",EGAL},
       {"!=",DIFFERENT},{ "<", INFERIEUR},{"<=",INFEGAL},{">",SUPERIEUR},{">=",SUPEGAL}};
     
     
    InfoIdent* chercher (const char* s);
    /* Retourne l’entree de la table correspondant a l’identificateur
    ou au mot-cle qui s’ecrit comme s.
    AJOUTE l’identificateur s `a la table S’IL N’Y EST PAS D´EJ`A.
    Initialement, la table ne contient que les mots-cl´es du
    langage (et ´eventuellement des identificateurs predefinis)
    */
    void indent(); 
    /* declaration en C ex #include <stdio.h
    http://www.comp.sci.sitew.com/fs/Root/5ybea-Outil_ */
    %}
     
    Ident ([a-zA-Z])([a-zA-Z0-9]*)
    Alors alors
    Faire faire
    MotClefSautLigneIndent {Alors}|{Faire}
    Finsi finsi
    Fintantque fintantque
    MotClefLessIndent {Finsi}|{Fintantque}
    Tantque tanque
    Tableau tableau
    Entier entier
    NewChaine chaine
    Si si
    Ou ou
    Et et
    Non non
    Afficher afficher
    Lire lire
    MotClef ({Tantque}|{Tableau}|{Entier}|{Chaine}|{Si}|{Ou}|{Et}|{Non}|{Afficher}|{Lire})
    Affecter "<-"
    Chaine \"([^"\\]|\\n|\\\")*\"
    Nombre [0-9]+
    Reel {Nombre}(((\.)({Nombre}))|e{Nombre})?
    Egal "="
    Different "!="
    Inferieur "<"
    InfEgal "<="
    Superieur ">"
    SupEgal ">="
    Comparator ({Egal}|{Different}|{Inferieur}|{InfEgal}|{Superieur}|{SupEgal})
    Plus "+"
    Moins "-"
    Multiplier "*"
    Diviser "/"
    Modulo "%"
    Operateur ({Plus}|{Moins}|{Multiplier}|{Diviser}|{Modulo})
    Commentaire1 ("/*"([^*]|"*"+[^/*])*"*"+"/")|("//"[^\n]*)
    SINON sinon
     
     
    %%
     
     
    "\n"|";"	{
    	 if (!indentation) { indentation=1; printf("<br>");}
    	return ;	
    	}
     
    {Chaine}  {
    		indent();printf("<font color=green>%s</font>",yytext);
    		return CHAINE;
    		}
     
    {Nombre}  { indent();printf("<font color=green>%s</font>",yytext);
    		return NOMBRE;
    		}
     
    {Reel} {
    		indent();printf("<font color=green>%s</font>",yytext);
    		return REEL;
    		}
     
    {Affecter}	{printf("&larr;");
    		return chercher(yytext)->code_mot_cle;}
     
    ">=" printf("&ge;");
     
    "<=" printf("&le;");
     
    {Commentaire1}	{ indent();printf("<cite><font color=blue>%s</font></cite>",yytext); }
     
    {SINON} {
    			if (!indentation){ printf("<br>"); indentation=1;}
    			if(nbIndent>0){nbIndent--;}
    			indent();
    			printf("<strong><font color=red>%s</font></strong><br>",yytext);
    			indentation=1;
    			nbIndent++;
    			return chercher(yytext)->code_mot_cle;
    			}
     
    {MotClefLessIndent}	{
    			if (!indentation){ printf("<br>"); indentation=1;}
    			if(nbIndent>0){nbIndent--;}
    			indent();
    			printf("<strong><font color=red>%s</font></strong><br>",yytext);
    			indentation=1;
    			return chercher(yytext)->code_mot_cle; 			
    			}
     
    {MotClefSautLigneIndent} {
    			  indent();
    			  nbIndent++;
    			  printf("<strong><font color=red>%s</font></strong>",yytext);
    			  if(!indentation){
    				indentation=1;
    				printf("<br>");}
    			return chercher(yytext)->code_mot_cle;
    			 }
     
     
     
     
    {MotClef}	{
    		indent();
    		printf("<strong><font color=red>%s</font></strong>",yytext);
    		return chercher(yytext)->code_mot_cle;}
     
    {Ident}		{
    		indent();
    		printf("%s",yytext);
    		return chercher(yytext)->code_mot_cle;}
     
    {Comparator}    { return chercher(yytext)->code_mot_cle;}
     
    {Operateur}     {  return yytext[0];}
     
     
    [ \t]	return ;
     
    .|\n	return; 
     
     
    %%
     
     
    void indent(){
      int i=0;
      if(indentation){
      for(i;i<nbIndent;i++){
      printf(" &nbsp;&nbsp;&nbsp;");
        }
    indentation=0;
    }
    }
     
    InfoIdent* chercher (const char* s){
       int i=0;
    	printf("%s",s);
       while(!strcmp(s,table[i].texte)){
    	printf("%s",table[i].texte);
        return table+i;
        i++;
       }
    }
    fichier main.c
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <stdio.h>
    #include "codes.h"
     
    extern int yylex();
    int main() {
    int lex;
    while (lex = yylex()) { // c'est bien un =, pas ==
    if (lex < 256)
    printf ("lexeme \'%d\'\n", lex);
    else
    printf ("lexeme \'%d\'\n", lex);
    }
    }
    le fichier code.h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    #include <stdio.h>
    #include "codes.h"
     
    extern int yylex();
    int main() {
    int lex;
    while (lex = yylex()) { // c'est bien un =, pas ==
    if (lex < 256)
    printf ("lexeme \'%d\'\n", lex);
    else
    printf ("lexeme \'%d\'\n", lex);
    }
    }
    Merci d'avance

  2. #2
    Membre chevronné
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    329
    Détails du profil
    Informations personnelles :
    Localisation : Canada

    Informations forums :
    Inscription : Octobre 2004
    Messages : 329
    Par défaut
    Je poste juste la partie intéressant de ce que je comprends :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    InfoIdent* chercher (const char* s){
       int i=0;
    	printf("%s",s);
       while(!strcmp(s,table[i].texte)){
    	printf("%s",table[i].texte);
        return table+i;
        i++;
       }
    }
    La fonction strcmp() renvoie 0 si les chaines sont identiques, et donc tu boucles tant que les chaines sont identiques.
    Ensuite le return au milieu me semble curieux puisque tu renverras finalement toujours la première entrée si elle match, et sinon tu ne renvoies rien (j'espère que le compilo se plaint ???).

  3. #3
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    17
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Alpes Maritimes (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2013
    Messages : 17
    Par défaut
    exact ,
    Merci beaucoup j'ai corrigé la condition du while et sorti le return table+i et tout marche
    Merci pour ta réponse rapide

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème std::set avec une structure
    Par DeathMixer dans le forum C++
    Réponses: 4
    Dernier message: 03/10/2009, 01h05
  2. Problème avec une structure
    Par pegase.90 dans le forum C
    Réponses: 2
    Dernier message: 04/12/2007, 17h34
  3. Problème avec une structure
    Par titux dans le forum C
    Réponses: 5
    Dernier message: 22/07/2007, 16h26
  4. Problème avec une structure
    Par Pierre.g dans le forum C
    Réponses: 4
    Dernier message: 30/12/2006, 12h22
  5. Probléme avec une structure
    Par astragoth dans le forum C++
    Réponses: 3
    Dernier message: 25/04/2006, 20h31

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo