Recent Changes - Search:

My Projects

Courses

Writings

Source Code

Social Networks

Live Traffic !

Évolution 6 : Les chaînes de caractères (Partie 1 - Variable et déclaration)

Créer son propre langage de programmation de A à Z

<< Évolution 5 : Les boucles | Évolution 6 : Les chaînes de caractères (Partie 1 - Variable et déclaration) | Évolution 7 : Les chaînes de caractères (Partie 2.1 - Concaténation et ré-allocation dynamique de la mémoire) >>

Grosse évolution : les chaînes de caractères. Le langage Simple ne sait gérer pour le moment que les entiers, les booléens et c'est tout ! Il serait chouette de pouvoir travailler sur du texte. C'est ce que nous allons implémenter. On va créer un nouveau type natif de variable dans Simple, le type texte. Puis nous allons nous dire que les variables de type texte doivent comment par un "t". Ensuite les chaînes de texte écrites en brutes devront être délimitées par des guillemets : "Ceci est un exemple de chaîne en Simple.". On mettra en place une instruction pour supprimer une variable de texte. Ceci dans l'objectif de libérer la mémoire allouée par la variable.

⚠ (:youtube T9O91u7CaPI?a fs=1 scale=2:)

Évolution 6 du langage :

⚠ (:source lang=bnf linenum:) <affectation> ::= <affectation> | variable_texte "=" <expression_texte> ";" <:vspace> <suppression> ::= "supprimer" variable_texte ";" <:vspace> <expression_texte> ::= variable_texte | texte (:sourcend:) variable_texte est un terminal au même titre que variable_booleenne et variable_arithmetique. C'est un nom qui doit commencer par un "t" minuscule. texte est aussi un terminal qui est la chaîne entre guillemets. A l'instar des commentaires, la reconnaissance de ce lexème se fera par une condition de démarrage. Mais le contenu ne devra cette fois-ci pas être ignoré par l'analyseur syntaxique. Le token envoyé à ce dernier devra avoir pour valeur la chaîne de caractères. Nous concaténerons les caractères de la chaînes à la variable yylval.texte : ⚠ (:source lang=c header="lexique_simple.lex" linenum:) %{ <:vspace> #include "simple.h" unsigned int lineno=1; bool error_lexical=false; <:vspace> /* definition de la fonction de concatenation pour les chaines de texte */ void* concat(char*,char*); <:vspace> %} <:vspace> %option noyywrap <:vspace> nombre 0|[1-9][[:digit:]]* variable_booleenne b(_|[[:alnum:]])* variable_arithmetique e(_|[[:alnum:]])* variable_texte t(_|[[:alnum:]])* <:vspace> /* regex de commentaire d'une seule ligne */ commentaire ((\/\/|#).*) <:vspace> /* pour les commentaires de plusieurs lignes, on declare nos deux lexemes en tant que conditions de demarrage exclusives (%x) dans Flex */ %x commentaire_1 %x commentaire_2 <:vspace> /* chaine de caractere */ %x chaine <:vspace> %% <:vspace> "/*" { /* un marqueur de debut de commentaire trouve -> on lui dit que le lexeme commentaire_1 commence */ BEGIN(commentaire_1); printf("Commentaire detecte en ligne %i\n",lineno); } <:vspace> <commentaire_1>"\n" { /* si on trouve des retours chariots et que la condition de demarrage est commentaire_1, alors on incremente la variable lineno. sans cela, on serait en decalage pour la suite de l'analyse */ lineno++; } <:vspace> <commentaire_1>"*"+"/" { /* si on au moins une fois "*" suivi de "/" et que la condition de demarrage est commentaire_1, alors on lui dit que le lexeme commentaire_1 est fini */ BEGIN(INITIAL); printf("Fin du commentaire en ligne %i\n",lineno); return TOK_COMMENT; } <:vspace> <commentaire_1>. {/* les autres caracteres suivants la conditions de demarrage sont absorbes par l'analyse est donc ingores */} <:vspace> "<!--" { BEGIN(commentaire_2); printf("Commentaire detecte en ligne %i\n",lineno); } <commentaire_2>"\n" {lineno++;} <commentaire_2>"-"+"-"+">" { BEGIN(INITIAL); printf("Fin du commentaire en ligne %i\n",lineno); return TOK_COMMENT; } <commentaire_2>. {} <:vspace> "\"" { /* debut de la chaine de texte (premier guillemet) */ BEGIN(chaine); yylval.texte=malloc(sizeof(char)*strlen(yytext)); if(yylval.texte==NULL){ fprintf(stderr,"\tERREUR : Probleme de memoire sur une chaine texte a la ligne %i !\n",lineno); exit(-1); } yylval.texte=strdup(yytext); printf("Chaine de texte detectee en ligne %i\n",lineno); } <:vspace> <chaine>"\n" { /* on prend en compte les sauts de ligne que l'on traduira par "\n" */ lineno++; yylval.texte=(char*)concat(yylval.texte,"\\n"); if(yylval.texte==NULL){ fprintf(stderr,"\tERREUR : Probleme de memoire sur une chaine texte a la ligne %i !\n",lineno); exit(-1); } } <:vspace> <chaine>"\t" { /* on prend en compte les tabulations que l'on traduira par "\t" */ yylval.texte=(char*)concat(yylval.texte,"\\t"); if(yylval.texte==NULL){ fprintf(stderr,"\tERREUR : Probleme de memoire sur une chaine texte a la ligne %i !\n",lineno); exit(-1); } } <:vspace> <chaine>"\\\"" { /* pour echapper le guillemet dans la chaine \" */ yylval.texte=(char*)concat(yylval.texte,yytext); if(yylval.texte==NULL){ fprintf(stderr,"\tERREUR : Probleme de memoire sur une chaine texte a la ligne %i !\n",lineno); exit(-1); } } <:vspace> <chaine>"\"" { /* fin de la chaine (deuxieme guillemet non echappe) */ BEGIN(INITIAL); yylval.texte=(char*)concat(yylval.texte,yytext); if(yylval.texte==NULL){ fprintf(stderr,"\tERREUR : Probleme de memoire sur une chaine texte a la ligne %i !\n",lineno); exit(-1); } printf("Fin de la chaine en ligne %i\n",lineno); return TOK_TEXTE; } <:vspace> <chaine>. { /* les caracteres de la chaine */ yylval.texte=(char*)concat(yylval.texte,yytext); if(yylval.texte==NULL){ fprintf(stderr,"\tERREUR : Probleme de memoire sur une chaine texte a la ligne %i !\n",lineno); exit(-1); } } <:vspace> {nombre} { sscanf(yytext, "%ld", &yylval.nombre); return TOK_NOMBRE; } <:vspace> "si" {return TOK_SI;} <:vspace> "alors" {return TOK_ALORS;} <:vspace> "sinon" {return TOK_SINON;} <:vspace> "++" {return TOK_INCREMENTATION;} <:vspace> "--" {return TOK_DECREMENTATION;} <:vspace> "+=" {return TOK_AFFECT_PLUS;} <:vspace> "-=" {return TOK_AFFECT_MOINS;} <:vspace> "*=" {return TOK_AFFECT_MUL;} <:vspace> "/=" {return TOK_AFFECT_DIV;} <:vspace> "%=" {return TOK_AFFECT_MOD;} <:vspace> "&=" {return TOK_AFFECT_ET;} <:vspace> "|=" {return TOK_AFFECT_OU;} <:vspace> "egal a"|"equivalent a"|"==" {return TOK_EQU;} <:vspace> "different de"|"!="|"<>" {return TOK_DIFF;} <:vspace> "superieur a"|"plus grand que"|">" {return TOK_SUP;} <:vspace> "inferieur a"|"plus petit que"|"<" {return TOK_INF;} <:vspace> "superieur ou egal a"|">=" {return TOK_SUPEQU;} <:vspace> "inferieur ou egal a"|"<=" {return TOK_INFEQU;} <:vspace> "compris dans"|"dans" {return TOK_IN;} <:vspace> "afficher" {return TOK_AFFICHER;} <:vspace> "supprimer" {return TOK_SUPPR;} <:vspace> "faire" {return TOK_FAIRE;} <:vspace> "x" {return TOK_CROIX;} <:vspace> "=" {return TOK_AFFECT;} <:vspace> "+" {return TOK_PLUS;} <:vspace> "-" {return TOK_MOINS;} <:vspace> "*" {return TOK_MUL;} <:vspace> "/" {return TOK_DIV;} <:vspace> "%" {return TOK_MOD;} <:vspace> "^" {return TOK_PUISSANCE;} <:vspace> "(" {return TOK_PARG;} <:vspace> ")" {return TOK_PARD;} <:vspace> "[" {return TOK_CROG;} <:vspace> "]" {return TOK_CROD;} <:vspace> "?" {return TOK_POINT_INTERROGATION;} <:vspace> ":" {return TOK_DOUBLE_POINT;} <:vspace> "et" {return TOK_ET;} <:vspace> "ou" {return TOK_OU;} <:vspace> "non"|"!" {return TOK_NON;} <:vspace> ";" {return TOK_FINSTR;} <:vspace> "vrai" {return TOK_VRAI;} <:vspace> "faux" {return TOK_FAUX;} <:vspace> "\n" {lineno++;} <:vspace> {variable_booleenne} { yylval.texte = yytext; return TOK_VARB; } <:vspace> <:vspace> {variable_arithmetique} { yylval.texte = yytext; return TOK_VARE; } <:vspace> {variable_texte} { yylval.texte = yytext; return TOK_VART; } <:vspace> {commentaire} { printf("Commentaire detecte en ligne %i\n",lineno); printf("Fin du commentaire en ligne %i\n",lineno); return TOK_COMMENT; } <:vspace> " "|"\t" {} <:vspace> . { fprintf(stderr,"\tERREUR : Lexeme inconnu a la ligne %d. Il s'agit de %s et comporte %d lettre(s)\n",lineno,yytext,yyleng); error_lexical=true; return yytext[0]; } <:vspace> %% <:vspace> /* fonction de concatenation - realloue la memoire a la variable texte dimensionne a la taille memoire du tableau de char ajout puis concatene */ <:vspace> void* concat(char* texte, char* ajout){ void* p=NULL; /* realloue la memoire -> taille de texte + taille de ajout + 1 (caractere de fin \0) */ if((p=realloc(texte,sizeof(char)*(strlen(texte)+strlen(ajout)+1)))){ texte=p; return strcat(texte,ajout); }else{ /* appel de la macro FREE pour liberer correctement la memoire */ FREE(texte); return NULL; } } (:sourcend:) Le fichier d'entête avec la macro FREE pour libérer correctement la mémoire : ⚠ (:source lang=c header="simple.h" linenum:) #include <stdlib.h> #include <stdio.h> #include <stdbool.h> #include <string.h> #include <glib.h> #include "syntaxe_simple.tab.h" int yylex(void); void yyerror(char*); extern unsigned int lineno; extern bool error_lexical; <:vspace> /* MACROS */ #define FREE(x) { if (x) free(x); x = NULL; } <:vspace> /* Le flux de notre fichier de sortie final */ FILE* fichier; <:vspace> /* Definition des methodes de generation de code C */ extern void debut_code(void); extern void genere_code(GNode*); extern void fin_code(void); <:vspace> /* Definition des sequences de code possibles pour l'AST (Arbre Syntaxique). Chaque sequence de code est associe a un numerique. Ce sont tout les noeuds possibles de l'AST. */ #define CODE_VIDE 0 #define SEQUENCE 1 #define VARIABLE 2 #define AFFECTATION 3 #define AFFECTATIONE 4 #define AFFECTATIONB 5 #define AFFICHAGEE 6 #define AFFICHAGEB 7 #define ENTIER 8 #define ADDITION 9 #define SOUSTRACTION 10 #define MULTIPLICATION 11 #define DIVISION 12 #define ET 13 #define OU 14 #define NON 15 #define VRAI 16 #define FAUX 17 #define EXPR_PAR 18 #define EGALITE 19 #define DIFFERENT 20 #define SUPERIEUR 21 #define INFERIEUR 22 #define SUPEGAL 23 #define INFEGAL 24 #define DANSII 25 /* inclus-inclus */ #define DANSEI 26 /* exclus-inclus */ #define DANSIE 27 /* inclus-exclus */ #define DANSEE 28 /* exclus-exclus */ #define CONDITION_SI 29 #define CONDITION_SI_SINON 30 #define SI 31 #define SINON 32 #define NEGATIF 33 #define BLOC_CODE 34 #define MODULO 35 #define AFFECTATION_PLUS 36 #define AFFECTATION_MOINS 37 #define AFFECTATION_MUL 38 #define AFFECTATION_DIV 39 #define AFFECTATION_MOD 40 #define AFFECTATION_ET 41 #define AFFECTATION_OU 42 #define INCREMENTATION 43 #define DECREMENTATION 44 #define AFFECTATION_INCR 45 #define AFFECTATION_DECR 46 #define BOUCLE_FOR 47 #define BOUCLE_WHILE 48 #define BOUCLE_DO_WHILE 49 #define AFFECTATIONNT 50 #define AFFECTATIONT 51 #define SUPPRESSIONT 52 #define TEXTE 53 #define AFFICHAGET 54 (:sourcend:) On met à jour l'analyseur syntaxique : ⚠ (:source lang=c header="syntaxe_bison.y" linenum:) %{ <:vspace> #include "simple.h" bool error_syntaxical=false; bool error_semantical=false; /* Notre table de hachage */ GHashTable* table_variable; <:vspace> /* Fonction de suppression des variables declarees a l'interieur d'un arbre syntaxique */ void supprime_variable(GNode*); <:vspace> /* Notre structure Variable qui a comme membre le type et un pointeur generique vers la valeur */ typedef struct Variable Variable; <:vspace> struct Variable{ char* type; GNode* value; }; <:vspace> %} <:vspace> /* L'union dans Bison est utilisee pour typer nos tokens ainsi que nos non terminaux. Ici nous avons declare une union avec trois types : nombre de type int, texte de type pointeur de char (char*) et noeud d'arbre syntaxique (AST) de type (GNode*) */ <:vspace> %union { long nombre; char* texte; GNode* noeud; } <:vspace> /* Nous avons ici les operateurs, ils sont definis par leur ordre de priorite. Si je definis par exemple la multiplication en premier et l'addition apres, le + l'emportera alors sur le * dans le langage. Les parenthese sont prioritaires avec %right */ <:vspace> %left TOK_INCREMENTATION TOK_DECREMENTATION /* ++ -- */ %left TOK_PLUS TOK_MOINS /* +- */ %left TOK_MUL TOK_DIV TOK_MOD /* /*% */ %left TOK_PUISSANCE /* ^ */ %left TOK_ET TOK_OU TOK_NON /* et ou non */ %left TOK_EQU TOK_DIFF TOK_SUP TOK_INF TOK_SUPEQU TOK_INFEQU /* comparaisons */ %right TOK_PARG TOK_PARD /* () */ <:vspace> /* Nous avons la liste de nos expressions (les non terminaux). Nous les typons tous en noeud de l'arbre syntaxique (GNode*) */ <:vspace> %type<noeud> code %type<noeud> bloc_code %type<noeud> commentaire %type<noeud> instruction %type<noeud> condition %type<noeud> condition_si %type<noeud> condition_sinon %type<noeud> boucle_for %type<noeud> boucle_while %type<noeud> boucle_do_while %type<noeud> variable_arithmetique %type<noeud> variable_booleenne %type<noeud> variable_texte %type<noeud> affectation %type<noeud> affichage %type<noeud> suppression %type<noeud> expression_arithmetique %type<noeud> expression_booleenne %type<noeud> expression_texte %type<noeud> addition %type<noeud> soustraction %type<noeud> multiplication %type<noeud> division %type<noeud> modulo <:vspace> /* Nous avons la liste de nos tokens (les terminaux de notre grammaire) */ <:vspace> %token<nombre> TOK_NOMBRE %token TOK_VRAI /* true */ %token TOK_FAUX /* false */ %token TOK_AFFECT /* = */ %token TOK_FINSTR /* ; */ %token TOK_IN /* dans */ %token TOK_CROG TOK_CROD /* [] */ %token TOK_AFFICHER /* afficher */ %token<texte> TOK_VARB /* variable booleenne */ %token<texte> TOK_VARE /* variable arithmetique */ %token<texte> TOK_VART %token TOK_SI /* si */ %token TOK_ALORS /* alors */ %token TOK_SINON /* sinon */ %token TOK_COMMENT /* commentaire */ %token TOK_AFFECT_PLUS TOK_AFFECT_MOINS TOK_AFFECT_MUL TOK_AFFECT_DIV TOK_AFFECT_MOD /* += -= *= /= %= */ %token TOK_AFFECT_ET TOK_AFFECT_OU /* &= |= */ %token TOK_POINT_INTERROGATION /* ? */ %token TOK_DOUBLE_POINT /* : */ %token TOK_FAIRE /* faire */ %token TOK_CROIX /* x */ %token<texte> TOK_TEXTE /* texte libre */ %token TOK_SUPPR /* supprimer */ <:vspace> %% <:vspace> /* Nous definissons toutes les regles grammaticales de chaque non terminal de notre langage. Par defaut on commence a definir l'axiome, c'est a dire ici le non terminal code. Si nous le definissons pas en premier nous devons le specifier en option dans Bison avec %start */ <:vspace> entree: code{ genere_code($1); g_node_destroy($1); }; <:vspace> bloc_code: code{ $$=g_node_new((gpointer)BLOC_CODE); g_node_append($$,$1); supprime_variable($1); } <:vspace> code: %empty{$$=g_node_new((gpointer)CODE_VIDE);} | code commentaire{ $$=g_node_new((gpointer)SEQUENCE); g_node_append($$,$1); g_node_append($$,$2); } | code instruction{ printf("Resultat : C'est une instruction valide !\n\n"); $$=g_node_new((gpointer)SEQUENCE); g_node_append($$,$1); g_node_append($$,$2); } | code error{ fprintf(stderr,"\tERREUR : Erreur de syntaxe a la ligne %d.\n",lineno); error_syntaxical=true; }; <:vspace> commentaire: TOK_COMMENT{ $$=g_node_new((gpointer)CODE_VIDE); }; <:vspace> instruction: affectation{ printf("\tInstruction type Affectation\n"); $$=$1; } | affichage{ printf("\tInstruction type Affichage\n"); $$=$1; } | condition{ printf("Condition si/sinon\n"); $$=$1; } | boucle_for{ printf("Boucle repetee\n"); $$=$1; } | boucle_while{ printf("Boucle tant que\n"); $$=$1; } | boucle_do_while{ printf("Boucle faire tant que\n"); $$=$1; } | suppression{ printf("\tInstruction type Suppression\n"); $$=$1; }; <:vspace> variable_arithmetique: TOK_VARE{ printf("\t\t\tVariable entiere %s\n",$1); $$=g_node_new((gpointer)VARIABLE); g_node_append_data($$,strdup($1)); }; <:vspace> variable_booleenne: TOK_VARB{ printf("\t\t\tVariable booleenne %s\n",$1); $$=g_node_new((gpointer)VARIABLE); g_node_append_data($$,strdup($1)); }; <:vspace> variable_texte: TOK_VART{ printf("\t\t\tVariable texte %s\n",$1); $$=g_node_new((gpointer)VARIABLE); g_node_append_data($$,strdup($1)); }; <:vspace> condition: condition_si TOK_FINSTR{ printf("\tCondition si\n"); $$=g_node_new((gpointer)CONDITION_SI); g_node_append($$,$1); } | condition_si condition_sinon TOK_FINSTR{ printf("\tCondition si/sinon\n"); $$=g_node_new((gpointer)CONDITION_SI_SINON); g_node_append($$,$1); g_node_append($$,$2); } | TOK_PARG expression_booleenne TOK_PARD TOK_POINT_INTERROGATION bloc_code TOK_DOUBLE_POINT bloc_code TOK_FINSTR{ printf("\tCondition si/sinon\n"); $$=g_node_new((gpointer)CONDITION_SI_SINON); g_node_append($$,g_node_new((gpointer)SI)); g_node_append(g_node_nth_child($$,0),$2); g_node_append(g_node_nth_child($$,0),$5); g_node_append($$,g_node_new((gpointer)SINON)); g_node_append(g_node_nth_child($$,1),$7); <:vspace> }; <:vspace> condition_si: TOK_SI expression_booleenne TOK_ALORS bloc_code{ $$=g_node_new((gpointer)SI); g_node_append($$,$2); g_node_append($$,$4); }; <:vspace> condition_sinon: TOK_SINON bloc_code{ $$=g_node_new((gpointer)SINON); g_node_append($$,$2); }; <:vspace> boucle_for: TOK_PARG expression_arithmetique TOK_PARD TOK_CROIX bloc_code TOK_FINSTR{ $$=g_node_new((gpointer)BOUCLE_FOR); g_node_append($$,$2); g_node_append($$,$5); }; <:vspace> boucle_while: TOK_PARG expression_booleenne TOK_PARD TOK_POINT_INTERROGATION bloc_code TOK_FINSTR{ $$=g_node_new((gpointer)BOUCLE_WHILE); g_node_append($$,$2); g_node_append($$,$5); }; <:vspace> boucle_do_while: TOK_FAIRE bloc_code TOK_POINT_INTERROGATION TOK_PARG expression_booleenne TOK_PARD TOK_FINSTR{ $$=g_node_new((gpointer)BOUCLE_DO_WHILE); g_node_append($$,$2); g_node_append($$,$5); }; <:vspace> affectation: variable_arithmetique TOK_AFFECT expression_arithmetique TOK_FINSTR{ /* $1 est la valeur du premier non terminal. Ici c'est la valeur du non terminal variable. $3 est la valeur du 2nd non terminal. */ printf("\t\tAffectation sur la variable\n"); Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ /* On cree une Variable et on lui affecte le type que nous connaissons et la valeur */ var=malloc(sizeof(Variable)); if(var!=NULL){ var->type=strdup("entier"); var->value=$3; /* On l'insere dans la table de hachage (cle: <nom_variable> / valeur: <(type,valeur)>) */ if(g_hash_table_insert(table_variable,g_node_nth_child($1,0)->data,var)){ $$=g_node_new((gpointer)AFFECTATIONE); g_node_append($$,$1); g_node_append($$,$3); }else{ fprintf(stderr,"ERREUR - PROBLEME CREATION VARIABLE !\n"); exit(-1); } }else{ fprintf(stderr,"ERREUR - PROBLEME ALLOCATION MEMOIRE VARIABLE !\n"); exit(-1); } }else{ $$=g_node_new((gpointer)AFFECTATION); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_PLUS expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_PLUS); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_MOINS expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_MOINS); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_MUL expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_MUL); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_DIV expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_DIV); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_MOD expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_MOD); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_INCREMENTATION TOK_FINSTR{ printf("\t\t\tIncrementation de +1 sur la variable\n"); $$=g_node_new((gpointer)AFFECTATION_INCR); g_node_append($$,$1); } | variable_arithmetique TOK_DECREMENTATION TOK_FINSTR{ printf("\t\t\tDecrementation de -1 sur la variable\n"); $$=g_node_new((gpointer)AFFECTATION_DECR); g_node_append($$,$1); } | variable_booleenne TOK_AFFECT expression_booleenne TOK_FINSTR{ /* $1 est la valeur du premier non terminal. Ici c'est la valeur du non terminal variable. $3 est la valeur du 2nd non terminal. */ printf("\t\tAffectation sur la variable\n"); Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ /* On cree une Variable et on lui affecte le type que nous connaissons et la valeur */ var=malloc(sizeof(Variable)); if(var!=NULL){ var->type=strdup("booleen"); var->value=$3; /* On l'insere dans la table de hachage (cle: <nom_variable> / valeur: <(type,valeur)>) */ if(g_hash_table_insert(table_variable,g_node_nth_child($1,0)->data,var)){ $$=g_node_new((gpointer)AFFECTATIONB); g_node_append($$,$1); g_node_append($$,$3); }else{ fprintf(stderr,"ERREUR - PROBLEME CREATION VARIABLE !\n"); exit(-1); } }else{ fprintf(stderr,"ERREUR - PROBLEME ALLOCATION MEMOIRE VARIABLE !\n"); exit(-1); } }else{ $$=g_node_new((gpointer)AFFECTATION); g_node_append($$,$1); g_node_append($$,$3); } } | variable_booleenne TOK_AFFECT_ET expression_booleenne TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_ET); g_node_append($$,$1); g_node_append($$,$3); } } | variable_booleenne TOK_AFFECT_OU expression_booleenne TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_OU); g_node_append($$,$1); g_node_append($$,$3); } } | variable_texte TOK_AFFECT expression_texte TOK_FINSTR{ /* $1 est la valeur du premier non terminal. Ici c'est la valeur du non terminal variable. $3 est la valeur du 2nd non terminal. */ printf("\t\tAffectation sur la variable\n"); Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ /* On cree une Variable et on lui affecte le type que nous connaissons et la valeur */ var=malloc(sizeof(Variable)); if(var!=NULL){ var->type=strdup("texte"); var->value=$3; /* On l'insere dans la table de hachage (cle: <nom_variable> / valeur: <(type,valeur)>) */ if(g_hash_table_insert(table_variable,g_node_nth_child($1,0)->data,var)){ $$=g_node_new((gpointer)AFFECTATIONNT); g_node_append($$,$1); g_node_append($$,$3); }else{ fprintf(stderr,"ERREUR - PROBLEME CREATION VARIABLE !\n"); exit(-1); } }else{ fprintf(stderr,"ERREUR - PROBLEME ALLOCATION MEMOIRE VARIABLE !\n"); exit(-1); } }else{ $$=g_node_new((gpointer)AFFECTATIONT); g_node_append($$,$1); g_node_append($$,$3); } }; <:vspace> affichage: TOK_AFFICHER expression_arithmetique TOK_FINSTR{ printf("\t\tAffichage de la valeur de l'expression arithmetique\n"); $$=g_node_new((gpointer)AFFICHAGEE); g_node_append($$,$2); } | TOK_AFFICHER expression_booleenne TOK_FINSTR{ printf("\t\tAffichage de la valeur de l'expression booleenne\n"); $$=g_node_new((gpointer)AFFICHAGEB); g_node_append($$,$2); } | TOK_AFFICHER expression_texte TOK_FINSTR{ printf("\t\tAffichage de la valeur de l'expression textuelle\n"); $$=g_node_new((gpointer)AFFICHAGET); g_node_append($$,$2); }; <:vspace> suppression: TOK_SUPPR variable_texte TOK_FINSTR{ /* On recupere un pointeur vers la structure Variable */ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($2,0)->data); /* Si on a trouve un pointeur valable */ if(var!=NULL){ /* On verifie que le type est bien un entier - Inutile car impose a l'analyse syntaxique */ if(strcmp(var->type,"texte")==0){ printf("\t\t\tSuppression de la variable texte\n"); $$=g_node_new((gpointer)SUPPRESSIONT); g_node_append($$,$2); /* suppression de la variable dans la table de hachage */ printf("suppresion variable %s\n",(char*)g_node_nth_child($2,0)->data); if(g_hash_table_remove(table_variable,(char*)g_node_nth_child($2,0)->data)){ printf("Variable supprimee !\n"); }else{ fprintf(stderr,"ERREUR - PROBLEME DE SUPPRESSION VARIABLE !\n"); exit(-1); } }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Type incompatible !\n",lineno); error_semantical=true; } /* Sinon on conclue que la variable n'a jamais ete declaree car absente de la table */ }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($2,0)->data); error_semantical=true; } }; <:vspace> expression_arithmetique: TOK_NOMBRE{ printf("\t\t\tNombre : %ld\n",$1); /* Comme le token TOK_NOMBRE est de type entier et que on a type expression_arithmetique comme du texte, il nous faut convertir la valeur en texte. */ int length=snprintf(NULL,0,"%ld",$1); char* str=malloc(length+1); snprintf(str,length+1,"%ld",$1); $$=g_node_new((gpointer)ENTIER); g_node_append_data($$,strdup(str)); free(str); } | variable_arithmetique{ /* On recupere un pointeur vers la structure Variable */ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); /* Si on a trouve un pointeur valable */ if(var!=NULL){ /* On verifie que le type est bien un entier - Inutile car impose a l'analyse syntaxique */ if(strcmp(var->type,"entier")==0){ $$=$1; }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Type incompatible (entier attendu - valeur : %s) !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } /* Sinon on conclue que la variable n'a jamais ete declaree car absente de la table */ }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } } | addition{ $$=$1; } | soustraction{ $$=$1; } | multiplication{ $$=$1; } | division{ $$=$1; } | modulo{ $$=$1; } | TOK_PLUS expression_arithmetique{ $$=$2; } | expression_arithmetique TOK_INCREMENTATION{ printf("\t\t\tIncrementation de +1\n"); $$=g_node_new((gpointer)INCREMENTATION); g_node_append($$,$1); } | expression_arithmetique TOK_DECREMENTATION{ printf("\t\t\tDecrementation de -1\n"); $$=g_node_new((gpointer)DECREMENTATION); g_node_append($$,$1); } | TOK_MOINS expression_arithmetique{ printf("\t\t\tOperation unaire negation\n"); $$=g_node_new((gpointer)NEGATIF); g_node_append($$,$2); } | TOK_PARG expression_arithmetique TOK_PARD{ printf("\t\t\tC'est une expression artihmetique entre parentheses\n"); $$=g_node_new((gpointer)EXPR_PAR); g_node_append($$,$2); }; <:vspace> expression_booleenne: TOK_VRAI{ printf("\t\t\tBooleen Vrai\n"); $$=g_node_new((gpointer)VRAI); } | TOK_FAUX{ printf("\t\t\tBooleen Faux\n"); $$=g_node_new((gpointer)FAUX); } | variable_booleenne{ /* On recupere un pointeur vers la structure Variable */ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); /* Si on a trouve un pointeur valable */ if(var!=NULL){ /* On verifie que le type est bien un entier - Inutile car impose a l'analyse syntaxique */ if(strcmp(var->type,"booleen")==0){ $$=$1; }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Type incompatible (booleen attendu - valeur : %s) !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } /* Sinon on conclue que la variable n'a jamais ete declaree car absente de la table */ }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } } | TOK_NON expression_booleenne{ printf("\t\t\tOperation booleenne Non\n"); $$=g_node_new((gpointer)NON); g_node_append($$,$2); } | expression_booleenne TOK_ET expression_booleenne{ printf("\t\t\tOperation booleenne Et\n"); $$=g_node_new((gpointer)ET); g_node_append($$,$1); g_node_append($$,$3); } | expression_booleenne TOK_OU expression_booleenne{ printf("\t\t\tOperation booleenne Ou\n"); $$=g_node_new((gpointer)OU); g_node_append($$,$1); g_node_append($$,$3); } | TOK_PARG expression_booleenne TOK_PARD{ printf("\t\t\tC'est une expression booleenne entre parentheses\n"); $$=g_node_new((gpointer)EXPR_PAR); g_node_append($$,$2); } | expression_booleenne TOK_EQU expression_booleenne{ printf("\t\t\tOperateur d'egalite ==\n"); $$=g_node_new((gpointer)EGALITE); g_node_append($$,$1); g_node_append($$,$3); } | expression_booleenne TOK_DIFF expression_booleenne{ printf("\t\t\tOperateur d'inegalite !=\n"); $$=g_node_new((gpointer)DIFFERENT); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_EQU expression_arithmetique{ printf("\t\t\tOperateur d'egalite ==\n"); $$=g_node_new((gpointer)EGALITE); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_DIFF expression_arithmetique{ printf("\t\t\tOperateur d'inegalite !=\n"); $$=g_node_new((gpointer)DIFFERENT); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_SUP expression_arithmetique{ printf("\t\t\tOperateur de superiorite >\n"); $$=g_node_new((gpointer)SUPERIEUR); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_INF expression_arithmetique{ printf("\t\t\tOperateur d'inferiorite <\n"); $$=g_node_new((gpointer)INFERIEUR); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_SUPEQU expression_arithmetique{ printf("\t\t\tOperateur >=\n"); $$=g_node_new((gpointer)SUPEGAL); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_INFEQU expression_arithmetique{ printf("\t\t\tOperateur <=\n"); $$=g_node_new((gpointer)INFEGAL); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_IN TOK_CROG expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROD{ printf("\t\t\tOperateur dans\n"); $$=g_node_new((gpointer)DANSII); g_node_append($$,$1); g_node_append($$,$4); g_node_append($$,$6); } | expression_arithmetique TOK_IN TOK_CROD expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROD{ printf("\t\t\tOperateur dans\n"); $$=g_node_new((gpointer)DANSEI); g_node_append($$,$1); g_node_append($$,$4); g_node_append($$,$6); } | expression_arithmetique TOK_IN TOK_CROG expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROG{ printf("\t\t\tOperateur dans\n"); $$=g_node_new((gpointer)DANSIE); g_node_append($$,$1); g_node_append($$,$4); g_node_append($$,$6); } | expression_arithmetique TOK_IN TOK_CROD expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROG{ printf("\t\t\tOperateur dans\n"); $$=g_node_new((gpointer)DANSEE); g_node_append($$,$1); g_node_append($$,$4); g_node_append($$,$6); }; <:vspace> expression_texte: TOK_TEXTE{ printf("\t\t\tTexte %s\n",$1); $$=g_node_new((gpointer)TEXTE); g_node_append_data($$,strdup($1)); } | variable_texte{ /* On recupere un pointeur vers la structure Variable */ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); /* Si on a trouve un pointeur valable */ if(var!=NULL){ /* On verifie que le type est bien un entier - Inutile car impose a l'analyse syntaxique */ if(strcmp(var->type,"texte")==0){ $$=$1; }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Type incompatible (texte attendu - valeur : %s) !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } /* Sinon on conclue que la variable n'a jamais ete declaree car absente de la table */ }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } }; <:vspace> addition: expression_arithmetique TOK_PLUS expression_arithmetique{ printf("\t\t\tAddition\n"); $$=g_node_new((gpointer)ADDITION); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> soustraction: expression_arithmetique TOK_MOINS expression_arithmetique{ printf("\t\t\tSoustraction\n"); $$=g_node_new((gpointer)SOUSTRACTION); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> multiplication: expression_arithmetique TOK_MUL expression_arithmetique{ printf("\t\t\tMultiplication\n"); $$=g_node_new((gpointer)MULTIPLICATION); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> division: expression_arithmetique TOK_DIV expression_arithmetique{ printf("\t\t\tDivision\n"); $$=g_node_new((gpointer)DIVISION); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> modulo: expression_arithmetique TOK_MOD expression_arithmetique{ printf("\t\t\tModulo\n"); $$=g_node_new((gpointer)MODULO); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> %% <:vspace> /* Dans la fonction main on appelle bien la routine yyparse() qui sera genere par Bison. Cette routine appellera yylex() de notre analyseur lexical. */ <:vspace> int main(int argc, char** argv){ /* recuperation du nom de fichier d'entree (langage Simple) donne en parametre */ char* fichier_entree=strdup(argv[1]); /* ouverture du fichier en lecture dans le flux d'entree stdin */ stdin=fopen(fichier_entree,"r"); /* creation fichier de sortie (langage C) */ char* fichier_sortie=strdup(argv[1]); /* remplace l'extension par .c */ strcpy(rindex(fichier_sortie, '.'), ".c"); /* ouvre le fichier cree en ecriture */ fichier=fopen(fichier_sortie, "w"); /* Creation de la table de hachage */ table_variable=g_hash_table_new_full(g_str_hash,g_str_equal,NULL,free); printf("Debut de l'analyse syntaxique :\n"); debut_code(); yyparse(); fin_code(); printf("Fin de l'analyse !\n"); printf("Resultat :\n"); if(error_lexical){ printf("\t-- Echec : Certains lexemes ne font pas partie du lexique du langage ! --\n"); printf("\t-- Echec a l'analyse lexicale --\n"); } else{ printf("\t-- Succes a l'analyse lexicale ! --\n"); } if(error_syntaxical){ printf("\t-- Echec : Certaines phrases sont syntaxiquement incorrectes ! --\n"); printf("\t-- Echec a l'analyse syntaxique --\n"); } else{ printf("\t-- Succes a l'analyse syntaxique ! --\n"); if(error_semantical){ printf("\t-- Echec : Certaines phrases sont semantiquement incorrectes ! --\n"); printf("\t-- Echec a l'analyse semantique --\n"); } else{ printf("\t-- Succes a l'analyse semantique ! --\n"); } } /* Suppression du fichier genere si erreurs analyse */ if(error_lexical||error_syntaxical||error_semantical){ remove(fichier_sortie); printf("ECHEC GENERATION CODE !\n"); } else{ printf("Le fichier \"%s\" a ete genere !\n",fichier_sortie); } /* Fermeture des flux */ fclose(fichier); fclose(stdin); /* Liberation memoire */ free(fichier_entree); free(fichier_sortie); g_hash_table_destroy(table_variable); return EXIT_SUCCESS; } <:vspace> void yyerror(char *s) { fprintf(stderr, "Erreur de syntaxe a la ligne %d: %s\n", lineno, s); } <:vspace> /* Cette fonction supprime dans la table de hachage toutes les variables declarees pour la premiere fois dans l'arbre syntaxique donne en parametre */ <:vspace> void supprime_variable(GNode* ast){ /* si l'element est n'est pas NULL et que ce n'est pas une feuille et que ce n'est pas un type bloc code (pour eviter de supprimer une variable deja suprimee) */ if(ast&&!G_NODE_IS_LEAF(ast)&&(long)ast->data!=BLOC_CODE){ /* si le noeud est de type declaration */ if((long)ast->data==AFFECTATIONB||(long)ast->data==AFFECTATIONE||(long)ast->data==AFFECTATIONT){ /* suppression de la variable dans la table de hachage */ if(g_hash_table_remove(table_variable,(char*)g_node_nth_child(g_node_nth_child(ast,0),0)->data)){ printf("Variable supprimee !\n"); }else{ fprintf(stderr,"ERREUR - PROBLEME DE SUPPRESSION VARIABLE !\n"); exit(-1); } /* sinon on continue de parcourir l'arbre */ }else{ int nb_enfant; for(nb_enfant=0;nb_enfant<=g_node_n_children(ast);nb_enfant++){ supprime_variable(g_node_nth_child(ast,nb_enfant)); } } } } (:sourcend:) Et pour finir ? Le générateur de code : ⚠ (:source lang=c header="generation_code.c" linenum:) #include "simple.h" <:vspace> unsigned int nb_boucle=0; <:vspace> void debut_code(){ fprintf(fichier, "/* FICHIER GENERE PAR LE COMPILATEUR SIMPLE */\n\n"); fprintf(fichier, "#include<stdlib.h>\n#include<stdbool.h>\n#include<stdio.h>\n#include<string.h>\n\n"); fprintf(fichier, "int main(void){\n"); } <:vspace> void fin_code(){ fprintf(fichier, "\treturn EXIT_SUCCESS;\n"); fprintf(fichier, "}\n"); } <:vspace> void genere_code(GNode* ast){ if(ast){ switch((long)ast->data){ case SEQUENCE: genere_code(g_node_nth_child(ast,0)); genere_code(g_node_nth_child(ast,1)); break; case VARIABLE: fprintf(fichier,"%s",(char*)g_node_nth_child(ast,0)->data); break; case AFFECTATIONE: fprintf(fichier,"\tlong "); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATIONB: fprintf(fichier,"\tbool "); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_PLUS: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"+="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_MOINS: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"-="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_MUL: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"*="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_DIV: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"/="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_MOD: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"%%="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_ET: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"&="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_OU: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"|="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_INCR: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"++;\n"); break; case AFFECTATION_DECR: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"--;\n"); break; case AFFICHAGEE: fprintf(fichier,"\tprintf(\"%%ld\","); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,");\n"); break; case AFFICHAGEB: fprintf(fichier,"\tprintf(\"%%s\","); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"?\"vrai\":\"faux\");\n"); break; case ENTIER: fprintf(fichier,"%s",(char*)g_node_nth_child(ast,0)->data); break; case ADDITION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"+"); genere_code(g_node_nth_child(ast,1)); break; case SOUSTRACTION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"-"); genere_code(g_node_nth_child(ast,1)); break; case MULTIPLICATION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"*"); genere_code(g_node_nth_child(ast,1)); break; case DIVISION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"/"); genere_code(g_node_nth_child(ast,1)); break; case MODULO: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"%%"); genere_code(g_node_nth_child(ast,1)); break; case INCREMENTATION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"+1"); break; case DECREMENTATION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"-1"); break; case VRAI: fprintf(fichier,"true"); break; case FAUX: fprintf(fichier,"false"); break; case ET: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,1)); break; case OU: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"||"); genere_code(g_node_nth_child(ast,1)); break; case NON: fprintf(fichier,"!"); genere_code(g_node_nth_child(ast,0)); break; case EXPR_PAR: fprintf(fichier,"("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,")"); break; case EGALITE: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"=="); genere_code(g_node_nth_child(ast,1)); break; case DIFFERENT: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"!="); genere_code(g_node_nth_child(ast,1)); break; case INFERIEUR: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<"); genere_code(g_node_nth_child(ast,1)); break; case SUPERIEUR: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">"); genere_code(g_node_nth_child(ast,1)); break; case INFEGAL: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<="); genere_code(g_node_nth_child(ast,1)); break; case SUPEGAL: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">="); genere_code(g_node_nth_child(ast,1)); break; case DANSII: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<="); genere_code(g_node_nth_child(ast,2)); break; case DANSEI: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">"); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<="); genere_code(g_node_nth_child(ast,2)); break; case DANSIE: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<"); genere_code(g_node_nth_child(ast,2)); break; case DANSEE: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">"); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<"); genere_code(g_node_nth_child(ast,2)); break; case NEGATIF: fprintf(fichier,"-"); genere_code(g_node_nth_child(ast,0)); break; case CONDITION_SI: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"\n"); break; case CONDITION_SI_SINON: genere_code(g_node_nth_child(ast,0)); genere_code(g_node_nth_child(ast,1)); break; case SI: fprintf(fichier,"\tif("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"){\n"); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"\t}"); break; case SINON: fprintf(fichier,"else{\n"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"\t}\n"); break; case BOUCLE_FOR: fprintf(fichier,"\tint i%i;\n\tfor(i%i=0;i%i<",nb_boucle,nb_boucle,nb_boucle); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,";i%i++){\n",nb_boucle); nb_boucle++; genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"\t}\n"); break; case BOUCLE_WHILE: fprintf(fichier,"\twhile("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"){\n"); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"\t}\n"); break; case BOUCLE_DO_WHILE: fprintf(fichier,"\tdo{\n"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"\t}while("); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,");\n"); break; case BLOC_CODE: genere_code(g_node_nth_child(ast,0)); break; case AFFICHAGET: fprintf(fichier,"\tprintf(\"%%s\","); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,");\n"); break; case TEXTE: fprintf(fichier,"%s",(char*)g_node_nth_child(ast,0)->data); break; case AFFECTATIONNT: fprintf(fichier,"\tchar* "); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"=malloc(sizeof(char)*(strlen("); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,")+1));\n"); fprintf(fichier,"\tif("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"==NULL){\n"); fprintf(fichier,"\tprintf(\"Erreur d'allocation memoire sur la variable "); genere_code(g_node_nth_child(ast,0)); fprintf(fichier," !\");\n\texit(-1);\n\t}\n\tstrcpy("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,","); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,");\n"); break; case AFFECTATIONT: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"=realloc("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,",sizeof(char)*(strlen("); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,")+1));\n"); fprintf(fichier,"\tif("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"==NULL){\n"); fprintf(fichier,"\tprintf(\"Erreur de reallocation memoire sur la variable "); genere_code(g_node_nth_child(ast,0)); fprintf(fichier," !\");\n\texit(-1);\n\t}\n\tstrcpy("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,","); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,");\n"); break; case SUPPRESSIONT: fprintf(fichier,"\tfree("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,");\n"); break; } } } (:sourcend:) On compile et on teste avec ce joli programme : ⚠ (:source lang=text header="programme.simple" linenum:) //affichage d'une chaine de texte afficher "Bonjour le monde ! (oui on est en France ici, pas de Hallo Welt !!)\n"; <:vspace> //affectation de la variable texte tphrase_de_thomas="Thomas a dit : <:vspace> \"J'aime bien la science-fiction et les dessins animés Disney !\". <:vspace> Mais voilà on s'en fiche un peu... <:vspace> "; <:vspace> //affiche la variable texte afficher tphrase_de_thomas; <:vspace> //supprime la variable texte (libere la memoire) - la variable est desormais inutilisable en l'etat supprimer tphrase_de_thomas; <:vspace> <!-- <:vspace> AFFICHAGE DE LA TABLE DE MULTIPLICATION DE 1 A 10 <:vspace> --> <:vspace> afficher "---------------------------------TABLE DE MULTIPLICATION---------------------------------\n"; afficher "|X\t1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t|\n"; e_i=0; (10)x e_j=0; e_i++; afficher "|"; afficher e_i; (10)x e_j++; afficher "\t"; afficher e_i*e_j; ; afficher "\t|\n"; ; afficher "-----------------------------------------------------------------------------------------\n"; (:sourcend:) Cela devrait vous afficher sur la console : ⚠ (:source lang=text:) Bonjour le monde ! (oui on est en France ici, pas de Hallo Welt !!) Thomas a dit : <:vspace> "J'aime bien la science-fiction et les dessins animés Disney !". <:vspace> Mais voilà on s'en fiche un peu... <:vspace> ---------------------------------TABLE DE MULTIPLICATION--------------------------------- |X 1 2 3 4 5 6 7 8 9 10 | |1 1 2 3 4 5 6 7 8 9 10 | |2 2 4 6 8 10 12 14 16 18 20 | |3 3 6 9 12 15 18 21 24 27 30 | |4 4 8 12 16 20 24 28 32 36 40 | |5 5 10 15 20 25 30 35 40 45 50 | |6 6 12 18 24 30 36 42 48 54 60 | |7 7 14 21 28 35 42 49 56 63 70 | |8 8 16 24 32 40 48 56 64 72 80 | |9 9 18 27 36 45 54 63 72 81 90 | |10 10 20 30 40 50 60 70 80 90 100 | ----------------------------------------------------------------------------------------- (:sourcend:) On va continuer sur les chaînes de caractères. On implémentera dans la prochaine évolution la concaténation (sous forme d'affectation) de deux expressions texte.

<< Évolution 5 : Les boucles | Évolution 6 : Les chaînes de caractères (Partie 1 - Variable et déclaration) | Évolution 7 : Les chaînes de caractères (Partie 2.1 - Concaténation et ré-allocation dynamique de la mémoire) >>

Thomas - (CC BY-NC-SA 3.0 FR)

Edit - History - Print - Recent Changes - Search
Page last modified on July 12, 2018, at 11:09 AM EST