Afficher arbre ?

Fermé
whitelight Messages postés 2 Date d'inscription vendredi 16 mai 2008 Statut Membre Dernière intervention 2 juin 2008 - 16 mai 2008 à 12:55
whitelight Messages postés 2 Date d'inscription vendredi 16 mai 2008 Statut Membre Dernière intervention 2 juin 2008 - 2 juin 2008 à 13:32
Bonjour,
j'ai une structure d'arbre suivante et je veut l'afficher

***************************************************************************************
#ifndef _ARBRE__H_
#define _ARBRE__H_
#pragma once

struct arbre;
typedef struct arbre* Arbre;

struct program_str{
Arbre declar,corps;
};

struct declar_str{
Arbre declar,type,listvar;
};

struct listvar_str{
Arbre listvar,id;
};

struct corps_str{
Arbre corps,instr;
};

struct expr_str{
int type; // 1:affectation 2:somme 3:produit 4:id 5:cst
union data_expr{
struct affec_str{ Arbre id,expr;}affec;
struct op_str{Arbre expr1,expr2;}op;
Arbre cst_ou_id;
}data;
int type_exp; // 1:entier 2:reel 3:bool
};

struct fonct_str{
int type; // 1:id 2:expr
Arbre id_ou_expr;
};

struct arbre{
int type; //voir compil.y : partie définition.
union data_arbre{
struct program_str program;
struct declar_str declar;
struct listvar_str listvar;
struct corps_str corps;
struct expr_str expr;
struct fonct_str fonct;
int indice; // l'indice des IDs et des CSTs dans la table des symboles.
int type; // pour les types 1:entier 2:reel 3:bool
}data;
};



void afficher_arbre_syntaxique();
#endif

1 réponse

whitelight Messages postés 2 Date d'inscription vendredi 16 mai 2008 Statut Membre Dernière intervention 2 juin 2008
2 juin 2008 à 13:32
slt, ça vous aide
char* lettre[]={
"Non Utilise",
"program",
"ID",
"declar",
"corps",
"TYPE",
"listvar",
"instr (voir la grammaire!)",
"expr",
"fonct",
"CST"
};

char* type_label[]={
"Non Utilise",
"Entier",
"Reel",
"Booleen"
};

void init_arb_syn()
{
Arb_Syn=NULL;
out_a=fopen("out.arb","w");
}

void liberer_arb_syn()
{
if(Arb_Syn!=NULL)
{
//c a vous de voir comment libere tout l'espace occupé par cet arbre.
Arb_Syn=NULL;
}
fclose(out_a);
}

Arbre creer_arbre(int type)
{
Arbre temp;
temp=(Arbre) malloc(sizeof(struct arbre));
temp->type=type;
return temp;
}

void afficher_arbre_rec(Arbre rac,int prof)
{
int prof_i;
if(rac!=NULL)
{
prof_i=prof;
while(prof_i){ fprintf(out_a," "); prof_i--;}
fprintf(out_a,"%s",lettre[rac->type]);
switch(rac->type)
{
case 1: fprintf(out_a,"\n");
afficher_arbre_rec(rac->data.program.declar,prof+1);
afficher_arbre_rec(rac->data.program.corps,prof+1);
break;
case 2: fprintf(out_a,"(%d)\n",rac->data.indice);
break;
case 3: fprintf(out_a,"\n");
afficher_arbre_rec(rac->data.declar.type,prof+1);
afficher_arbre_rec(rac->data.declar.listvar,prof+1);
afficher_arbre_rec(rac->data.declar.declar,prof+1);
break;
case 4: fprintf(out_a,"\n");
afficher_arbre_rec(rac->data.corps.instr,prof+1);
//afficher_arbre_rac(rac->data.corps.corps,prof+1);
break;
case 5: fprintf(out_a,"(%s)\n",type_label[rac->data.type]);
break;
case 6: fprintf(out_a,"\n");
afficher_arbre_rec(rac->data.listvar.id,prof+1);
afficher_arbre_rec(rac->data.listvar.listvar,prof+1);
break;
case 7: fprintf(out_a,"\n");
break;
case 8: fprintf(out_a,"(%s)\n",type_label[rac->data.expr.type_exp]);
switch(rac->data.expr.type)
{

}
break;
default: fprintf(err,"Erreur lors de l'affichage de l'arbre (completer l'arbre!)\n");
}
}
}

void afficher_arbre_syntaxique()
{
fprintf(out_a,"l'Arbre Syntaxique :\n");
afficher_arbre_rec(Arb_Syn,0);
}
0