Recent Changes - Search:

My Projects

Courses

Writings

Source Code

Social Networks

Live Traffic !

Évolution 5 : Les boucles

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

<< Évolution 4 : Les raccourcis syntaxiques | Évolution 5 : Les boucles | Évolution 6 : Les chaînes de caractères (Partie 1 - Variable et déclaration) >>

On implémente maintenant les boucles. Très utiles dans la programmation, elles permettent d'exécuter des instructions un certain nombre de fois ou tant qu'une condition est vraie. Pour cette évolution, j'ai fait directement une vidéo où je fais l'implémentation en live. Vidéo !

Je vous donne les fichiers :

Évolution 5 du langage :

  1. <instruction> ::= <instruction> | <boucle_for> | <boucle_while> | <boucle_do_while>
  2.  
  3. <boucle_for> ::= "(" <expression_arithmetique> ")" "x" <code > ";"
  4.  
  5. <boucle_while> ::= "(" <expression_booleenne> ")" "?" <code > ";"
  6.  
  7. <boucle_do while> ::= "faire" <code > "?" "(" <expression_booleenne> ")" ";"
lexique_simple.lex
  1. %{
  2.  
  3. #include "simple.h"
  4. unsigned int lineno=1;
  5. bool error_lexical=false;
  6.  
  7. %}
  8.  
  9. %option noyywrap
  10.  
  11. nombre 0|[1-9][[:digit:]]*
  12. variable_booleenne b(_|[[:alnum:]])*
  13. variable_arithmetique e(_|[[:alnum:]])*
  14.  
  15. /* regex de commentaire d'une seule ligne */
  16. commentaire ((\/\/|#).*)
  17.  
  18. /* pour les commentaires de plusieurs lignes, on declare nos deux lexemes en tant que conditions de demarrage exclusives (%x) dans Flex */
  19. %x  commentaire_1
  20. %x  commentaire_2
  21.  
  22. %%
  23.  
  24. "/*"    {
  25.             /* un marqueur de debut de commentaire trouve -> on lui dit que le lexeme commentaire_1 commence */
  26.             BEGIN(commentaire_1);
  27.             printf("Commentaire detecte en ligne %i\n",lineno);
  28.         }
  29.  
  30. <commentaire_1>"\n"     {
  31.                             /* 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 */
  32.                             lineno++;
  33.                         }
  34.  
  35. <commentaire_1>"*"+"/"      {
  36.                                 /* 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 */
  37.                                 BEGIN(INITIAL);
  38.                                 printf("Fin du commentaire en ligne %i\n",lineno);
  39.                                 return TOK_COMMENT;
  40.                             }
  41.  
  42. <commentaire_1>.    {/* les autres caracteres suivants la conditions de demarrage sont absorbes par l'analyse est donc ingores */}
  43.  
  44. "<!--"      {
  45.                 BEGIN(commentaire_2);
  46.                 printf("Commentaire detecte en ligne %i\n",lineno);
  47.             }
  48. <commentaire_2>"\n"         {lineno++;}
  49. <commentaire_2>"-"+"-"+">"  {
  50.                                 BEGIN(INITIAL);
  51.                                 printf("Fin du commentaire en ligne %i\n",lineno);
  52.                                 return TOK_COMMENT;
  53.                             }
  54. <commentaire_2>.            {}
  55.  
  56. {nombre} {
  57.     sscanf(yytext, "%ld", &yylval.nombre);
  58.     return TOK_NOMBRE;
  59. }
  60.  
  61. "si"    {return TOK_SI;}
  62.  
  63. "alors" {return TOK_ALORS;}
  64.  
  65. "sinon" {return TOK_SINON;}
  66.  
  67. "++"    {return TOK_INCREMENTATION;}
  68.  
  69. "--"    {return TOK_DECREMENTATION;}
  70.  
  71. "+="    {return TOK_AFFECT_PLUS;}
  72.  
  73. "-="    {return TOK_AFFECT_MOINS;}
  74.  
  75. "*="    {return TOK_AFFECT_MUL;}
  76.  
  77. "/="    {return TOK_AFFECT_DIV;}
  78.  
  79. "%="    {return TOK_AFFECT_MOD;}
  80.  
  81. "&="    {return TOK_AFFECT_ET;}
  82.  
  83. "|="    {return TOK_AFFECT_OU;}
  84.  
  85. "egal a"|"equivalent a"|"=="        {return TOK_EQU;}
  86.  
  87. "different de"|"!="|"<>"            {return TOK_DIFF;}
  88.  
  89. "superieur a"|"plus grand que"|">"  {return TOK_SUP;}
  90.  
  91. "inferieur a"|"plus petit que"|"<"  {return TOK_INF;}
  92.  
  93. "superieur ou egal a"|">="          {return TOK_SUPEQU;}
  94.  
  95. "inferieur ou egal a"|"<="          {return TOK_INFEQU;}
  96.  
  97. "compris dans"|"dans"               {return TOK_IN;}
  98.  
  99. "afficher"      {return TOK_AFFICHER;}
  100.  
  101. "faire"         {return TOK_FAIRE;}
  102.  
  103. "x"             {return TOK_CROIX;}
  104.  
  105. "="             {return TOK_AFFECT;}
  106.  
  107. "+"             {return TOK_PLUS;}
  108.  
  109. "-"             {return TOK_MOINS;}
  110.  
  111. "*"             {return TOK_MUL;}
  112.  
  113. "/"             {return TOK_DIV;}
  114.  
  115. "%"             {return TOK_MOD;}
  116.  
  117. "("             {return TOK_PARG;}
  118.  
  119. ")"             {return TOK_PARD;}
  120.  
  121. "["             {return TOK_CROG;}
  122.  
  123. "]"             {return TOK_CROD;}
  124.  
  125. "?"             {return TOK_POINT_INTERROGATION;}
  126.  
  127. ":"             {return TOK_DOUBLE_POINT;}
  128.  
  129. "et"            {return TOK_ET;}
  130.  
  131. "ou"            {return TOK_OU;}
  132.  
  133. "non"|"!"           {return TOK_NON;}
  134.  
  135. ";"             {return TOK_FINSTR;}
  136.  
  137. "vrai"          {return TOK_VRAI;}
  138.  
  139. "faux"          {return TOK_FAUX;}
  140.  
  141. "\n"            {lineno++;}
  142.  
  143. {variable_booleenne} {
  144.     yylval.texte = yytext;
  145.     return TOK_VARB;
  146. }
  147.  
  148.  
  149. {variable_arithmetique} {
  150.     yylval.texte = yytext;
  151.     return TOK_VARE;
  152. }
  153.  
  154. {commentaire}   {
  155.     printf("Commentaire detecte en ligne %i\n",lineno);
  156.     printf("Fin du commentaire en ligne %i\n",lineno);
  157.     return TOK_COMMENT;
  158. }
  159.  
  160. " "|"\t" {}
  161.  
  162. . {
  163.     fprintf(stderr,"\tERREUR : Lexeme inconnu a la ligne %d. Il s'agit de %s et comporte %d lettre(s)\n",lineno,yytext,yyleng);
  164.     error_lexical=true;
  165.     return yytext[0];
  166. }
  167.  
  168. %%
syntaxe_simple.y
  1. %{
  2.  
  3. #include "simple.h"
  4. bool error_syntaxical=false;
  5. bool error_semantical=false;
  6. /* Notre table de hachage */
  7. GHashTable* table_variable;
  8.  
  9. /* Fonction de suppression des variables declarees a l'interieur d'un arbre syntaxique */
  10. void supprime_variable(GNode*);
  11.  
  12. /* Notre structure Variable qui a comme membre le type et un pointeur generique vers la valeur */
  13. typedef struct Variable Variable;
  14.  
  15. struct Variable{
  16.         char* type;
  17.         GNode* value;
  18. };
  19.  
  20. %}
  21.  
  22. /* 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*) */
  23.  
  24. %union {
  25.         long nombre;
  26.         char* texte;
  27.         GNode*  noeud;
  28. }
  29.  
  30. /* 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 */
  31.  
  32. %left                   TOK_INCREMENTATION      TOK_DECREMENTATION      /* ++ -- */
  33. %left                   TOK_PLUS        TOK_MOINS       /* +- */
  34. %left                   TOK_MUL         TOK_DIV         TOK_MOD         /* /*% */
  35. %left                   TOK_ET          TOK_OU          TOK_NON         /* et ou non */
  36. %left                   TOK_EQU         TOK_DIFF        TOK_SUP         TOK_INF         TOK_SUPEQU      TOK_INFEQU      /* comparaisons */
  37. %right                  TOK_PARG        TOK_PARD        /* () */
  38.  
  39. /* Nous avons la liste de nos expressions (les non terminaux). Nous les typons tous en noeud de l'arbre syntaxique (GNode*) */
  40.  
  41. %type<noeud>            code
  42. %type<noeud>            bloc_code
  43. %type<noeud>            commentaire
  44. %type<noeud>            instruction
  45. %type<noeud>            condition
  46. %type<noeud>            condition_si
  47. %type<noeud>            condition_sinon
  48. %type<noeud>            boucle_for
  49. %type<noeud>            boucle_while
  50. %type<noeud>            boucle_do_while
  51. %type<noeud>            variable_arithmetique
  52. %type<noeud>            variable_booleenne
  53. %type<noeud>            affectation
  54. %type<noeud>            affichage
  55. %type<noeud>            expression_arithmetique
  56. %type<noeud>            expression_booleenne
  57. %type<noeud>            addition
  58. %type<noeud>            soustraction
  59. %type<noeud>            multiplication
  60. %type<noeud>            division
  61. %type<noeud>            modulo
  62.  
  63. /* Nous avons la liste de nos tokens (les terminaux de notre grammaire) */
  64.  
  65. %token<nombre>          TOK_NOMBRE
  66. %token                  TOK_VRAI        /* true */
  67. %token                  TOK_FAUX        /* false */
  68. %token                  TOK_AFFECT      /* = */
  69. %token                  TOK_FINSTR      /* ; */
  70. %token                  TOK_IN          /* dans */
  71. %token                  TOK_CROG    TOK_CROD    /* [] */
  72. %token                  TOK_AFFICHER    /* afficher */
  73. %token<texte>           TOK_VARB        /* variable booleenne */
  74. %token<texte>           TOK_VARE        /* variable arithmetique */
  75. %token                  TOK_SI          /* si */
  76. %token                  TOK_ALORS       /* alors */
  77. %token                  TOK_SINON       /* sinon */
  78. %token                  TOK_COMMENT             /* commentaire */
  79. %token                  TOK_AFFECT_PLUS TOK_AFFECT_MOINS        TOK_AFFECT_MUL  TOK_AFFECT_DIV  TOK_AFFECT_MOD  /* += -= *= /= %= */
  80. %token                  TOK_AFFECT_ET   TOK_AFFECT_OU   /* &= |= */
  81. %token                  TOK_POINT_INTERROGATION /* ? */
  82. %token                  TOK_DOUBLE_POINT        /* : */
  83. %token                  TOK_FAIRE               /* faire */
  84. %token                  TOK_CROIX               /* x */
  85.  
  86. %%
  87.  
  88. /* 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 */
  89.  
  90. entree:         code{
  91.                                 genere_code($1);
  92.                                 g_node_destroy($1);
  93.                         };
  94.  
  95. bloc_code:      code{
  96.                                 $$=g_node_new((gpointer)BLOC_CODE);
  97.                                 g_node_append($$,$1);
  98.                                 supprime_variable($1);
  99.                         }
  100.  
  101. code:           %empty{$$=g_node_new((gpointer)CODE_VIDE);}
  102.                 |
  103.                 code commentaire{
  104.                         $$=g_node_new((gpointer)SEQUENCE);
  105.                         g_node_append($$,$1);
  106.                         g_node_append($$,$2);
  107.                 }
  108.                 |
  109.                 code instruction{
  110.                         printf("Resultat : C'est une instruction valide !\n\n");
  111.                         $$=g_node_new((gpointer)SEQUENCE);
  112.                         g_node_append($$,$1);
  113.                         g_node_append($$,$2);
  114.                 }
  115.                 |
  116.                 code error{
  117.                         fprintf(stderr,"\tERREUR : Erreur de syntaxe a la ligne %d.\n",lineno);
  118.                         error_syntaxical=true;
  119.                 };
  120.  
  121. commentaire:    TOK_COMMENT{
  122.                                         $$=g_node_new((gpointer)CODE_VIDE);
  123.                                 };
  124.  
  125. instruction:    affectation{
  126.                         printf("\tInstruction type Affectation\n");
  127.                         $$=$1;
  128.                 }
  129.                 |
  130.                 affichage{
  131.                         printf("\tInstruction type Affichage\n");
  132.                         $$=$1;
  133.                 }
  134.                 |
  135.                 condition{
  136.                     printf("Condition si/sinon\n");
  137.                     $$=$1;
  138.                 }
  139.                 |
  140.                 boucle_for{
  141.                         printf("Boucle repetee\n");
  142.                         $$=$1;
  143.                 }
  144.                 |
  145.                 boucle_while{
  146.                         printf("Boucle tant que\n");
  147.                         $$=$1;
  148.                 }
  149.                 |
  150.                 boucle_do_while{
  151.                         printf("Boucle faire tant que\n");
  152.                         $$=$1;
  153.                 };
  154.  
  155. variable_arithmetique:  TOK_VARE{
  156.                                 printf("\t\t\tVariable entiere %s\n",$1);
  157.                                 $$=g_node_new((gpointer)VARIABLE);
  158.                                 g_node_append_data($$,strdup($1));
  159.                         };
  160.  
  161. variable_booleenne:     TOK_VARB{
  162.                                 printf("\t\t\tVariable booleenne %s\n",$1);
  163.                                 $$=g_node_new((gpointer)VARIABLE);
  164.                                 g_node_append_data($$,strdup($1));
  165.                         };
  166.  
  167. condition:      condition_si TOK_FINSTR{
  168.                     printf("\tCondition si\n");
  169.                     $$=g_node_new((gpointer)CONDITION_SI);
  170.                     g_node_append($$,$1);
  171.                 }
  172.                 |
  173.                 condition_si condition_sinon TOK_FINSTR{
  174.                     printf("\tCondition si/sinon\n");
  175.                     $$=g_node_new((gpointer)CONDITION_SI_SINON);
  176.                     g_node_append($$,$1);
  177.                     g_node_append($$,$2);
  178.                 }
  179.                 |
  180.                 TOK_PARG expression_booleenne TOK_PARD TOK_POINT_INTERROGATION bloc_code TOK_DOUBLE_POINT bloc_code TOK_FINSTR{
  181.                         printf("\tCondition si/sinon\n");
  182.                     $$=g_node_new((gpointer)CONDITION_SI_SINON);
  183.                     g_node_append($$,g_node_new((gpointer)SI));
  184.                     g_node_append(g_node_nth_child($$,0),$2);
  185.                     g_node_append(g_node_nth_child($$,0),$5);
  186.                     g_node_append($$,g_node_new((gpointer)SINON));
  187.                     g_node_append(g_node_nth_child($$,1),$7);
  188.  
  189.                 };
  190.  
  191. condition_si:   TOK_SI expression_booleenne TOK_ALORS bloc_code{
  192.                     $$=g_node_new((gpointer)SI);
  193.                     g_node_append($$,$2);
  194.                     g_node_append($$,$4);
  195.                 };
  196.  
  197. condition_sinon:   TOK_SINON bloc_code{
  198.                         $$=g_node_new((gpointer)SINON);
  199.                         g_node_append($$,$2);
  200.                     };
  201.  
  202. boucle_for:             TOK_PARG expression_arithmetique TOK_PARD TOK_CROIX bloc_code TOK_FINSTR{
  203.                                         $$=g_node_new((gpointer)BOUCLE_FOR);
  204.                                         g_node_append($$,$2);
  205.                     g_node_append($$,$5);
  206.                                 };
  207.  
  208. boucle_while:   TOK_PARG expression_booleenne TOK_PARD TOK_POINT_INTERROGATION bloc_code TOK_FINSTR{
  209.                                         $$=g_node_new((gpointer)BOUCLE_WHILE);
  210.                                         g_node_append($$,$2);
  211.                     g_node_append($$,$5);
  212.                                 };
  213.  
  214. boucle_do_while:        TOK_FAIRE bloc_code TOK_POINT_INTERROGATION TOK_PARG expression_booleenne TOK_PARD TOK_FINSTR{
  215.                                                 $$=g_node_new((gpointer)BOUCLE_DO_WHILE);
  216.                                                 g_node_append($$,$2);
  217.                             g_node_append($$,$5);
  218.                                         };
  219.  
  220. affectation:    variable_arithmetique TOK_AFFECT expression_arithmetique TOK_FINSTR{
  221.                         /* $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. */
  222.                         printf("\t\tAffectation sur la variable\n");
  223.                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  224.                         if(var==NULL){
  225.                                 /* On cree une Variable et on lui affecte le type que nous connaissons et la valeur */
  226.                                 var=malloc(sizeof(Variable));
  227.                                 if(var!=NULL){
  228.                                         var->type=strdup("entier");
  229.                                         var->value=$3;
  230.                                         /* On l'insere dans la table de hachage (cle: <nom_variable> / valeur: <(type,valeur)>) */
  231.                                         if(g_hash_table_insert(table_variable,g_node_nth_child($1,0)->data,var)){
  232.                                         $$=g_node_new((gpointer)AFFECTATIONE);
  233.                                         g_node_append($$,$1);
  234.                                         g_node_append($$,$3);
  235.                                         }else{
  236.                                             fprintf(stderr,"ERREUR - PROBLEME CREATION VARIABLE !\n");
  237.                                             exit(-1);
  238.                                         }
  239.                                 }else{
  240.                                         fprintf(stderr,"ERREUR - PROBLEME ALLOCATION MEMOIRE VARIABLE !\n");
  241.                                         exit(-1);
  242.                                 }
  243.                         }else{
  244.                                 $$=g_node_new((gpointer)AFFECTATION);
  245.                                 g_node_append($$,$1);
  246.                                 g_node_append($$,$3);
  247.                         }
  248.                 }
  249.                 |
  250.                 variable_arithmetique TOK_AFFECT_PLUS expression_arithmetique TOK_FINSTR{
  251.                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  252.                         if(var==NULL){
  253.                                 fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data);
  254.                                 error_semantical=true;
  255.                         }else{
  256.                                 $$=g_node_new((gpointer)AFFECTATION_PLUS);
  257.                                 g_node_append($$,$1);
  258.                                 g_node_append($$,$3);
  259.                         }
  260.                 }
  261.                 |
  262.                 variable_arithmetique TOK_AFFECT_MOINS expression_arithmetique TOK_FINSTR{
  263.                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  264.                         if(var==NULL){
  265.                                 fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data);
  266.                                 error_semantical=true;
  267.                         }else{
  268.                                 $$=g_node_new((gpointer)AFFECTATION_MOINS);
  269.                                 g_node_append($$,$1);
  270.                                 g_node_append($$,$3);
  271.                         }
  272.                 }
  273.                 |
  274.                 variable_arithmetique TOK_AFFECT_MUL expression_arithmetique TOK_FINSTR{
  275.                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  276.                         if(var==NULL){
  277.                                 fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data);
  278.                                 error_semantical=true;
  279.                         }else{
  280.                                 $$=g_node_new((gpointer)AFFECTATION_MUL);
  281.                                 g_node_append($$,$1);
  282.                                 g_node_append($$,$3);
  283.                         }
  284.                 }
  285.                 |
  286.                 variable_arithmetique TOK_AFFECT_DIV expression_arithmetique TOK_FINSTR{
  287.                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  288.                         if(var==NULL){
  289.                                 fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data);
  290.                                 error_semantical=true;
  291.                         }else{
  292.                                 $$=g_node_new((gpointer)AFFECTATION_DIV);
  293.                                 g_node_append($$,$1);
  294.                                 g_node_append($$,$3);
  295.                         }
  296.                 }
  297.                 |
  298.                 variable_arithmetique TOK_AFFECT_MOD expression_arithmetique TOK_FINSTR{
  299.                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  300.                         if(var==NULL){
  301.                                 fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data);
  302.                                 error_semantical=true;
  303.                         }else{
  304.                                 $$=g_node_new((gpointer)AFFECTATION_MOD);
  305.                                 g_node_append($$,$1);
  306.                                 g_node_append($$,$3);
  307.                         }
  308.                 }
  309.                 |
  310.                 variable_arithmetique TOK_INCREMENTATION TOK_FINSTR{
  311.                         printf("\t\t\tIncrementation de +1 sur la variable\n");
  312.                     $$=g_node_new((gpointer)AFFECTATION_INCR);
  313.                     g_node_append($$,$1);
  314.                 }
  315.                 |
  316.                 variable_arithmetique TOK_DECREMENTATION TOK_FINSTR{
  317.                         printf("\t\t\tDecrementation de -1 sur la variable\n");
  318.                     $$=g_node_new((gpointer)AFFECTATION_DECR);
  319.                     g_node_append($$,$1);
  320.                 }
  321.                 |
  322.                 variable_booleenne TOK_AFFECT expression_booleenne TOK_FINSTR{
  323.                         /* $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. */
  324.                         printf("\t\tAffectation sur la variable\n");
  325.                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  326.                         if(var==NULL){
  327.                                 /* On cree une Variable et on lui affecte le type que nous connaissons et la valeur */
  328.                                 var=malloc(sizeof(Variable));
  329.                                 if(var!=NULL){
  330.                                         var->type=strdup("booleen");
  331.                                         var->value=$3;
  332.                                         /* On l'insere dans la table de hachage (cle: <nom_variable> / valeur: <(type,valeur)>) */
  333.                                         if(g_hash_table_insert(table_variable,g_node_nth_child($1,0)->data,var)){
  334.                                         $$=g_node_new((gpointer)AFFECTATIONB);
  335.                                         g_node_append($$,$1);
  336.                                         g_node_append($$,$3);
  337.                                         }else{
  338.                                             fprintf(stderr,"ERREUR - PROBLEME CREATION VARIABLE !\n");
  339.                                             exit(-1);
  340.                                         }
  341.                                 }else{
  342.                                         fprintf(stderr,"ERREUR - PROBLEME ALLOCATION MEMOIRE VARIABLE !\n");
  343.                                         exit(-1);
  344.                                 }
  345.                         }else{
  346.                                 $$=g_node_new((gpointer)AFFECTATION);
  347.                                 g_node_append($$,$1);
  348.                                 g_node_append($$,$3);
  349.                         }
  350.                 }
  351.                 |
  352.                 variable_booleenne TOK_AFFECT_ET expression_booleenne TOK_FINSTR{
  353.                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  354.                         if(var==NULL){
  355.                                 fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data);
  356.                                 error_semantical=true;
  357.                         }else{
  358.                                 $$=g_node_new((gpointer)AFFECTATION_ET);
  359.                                 g_node_append($$,$1);
  360.                                 g_node_append($$,$3);
  361.                         }
  362.                 }
  363.                 |
  364.                 variable_booleenne TOK_AFFECT_OU expression_booleenne TOK_FINSTR{
  365.                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  366.                         if(var==NULL){
  367.                                 fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data);
  368.                                 error_semantical=true;
  369.                         }else{
  370.                                 $$=g_node_new((gpointer)AFFECTATION_OU);
  371.                                 g_node_append($$,$1);
  372.                                 g_node_append($$,$3);
  373.                         }
  374.                 };
  375.  
  376. affichage:      TOK_AFFICHER expression_arithmetique TOK_FINSTR{
  377.                         printf("\t\tAffichage de la valeur de l'expression arithmetique\n");
  378.                         $$=g_node_new((gpointer)AFFICHAGEE);
  379.                         g_node_append($$,$2);
  380.                 }
  381.                 |
  382.                 TOK_AFFICHER expression_booleenne TOK_FINSTR{
  383.                         printf("\t\tAffichage de la valeur de l'expression booleenne\n");
  384.                         $$=g_node_new((gpointer)AFFICHAGEB);
  385.                         g_node_append($$,$2);
  386.                 };
  387.  
  388. expression_arithmetique:        TOK_NOMBRE{
  389.                                         printf("\t\t\tNombre : %ld\n",$1);
  390.                                         /* 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. */
  391.                                         int length=snprintf(NULL,0,"%ld",$1);
  392.                                         char* str=malloc(length+1);
  393.                                         snprintf(str,length+1,"%ld",$1);
  394.                                         $$=g_node_new((gpointer)ENTIER);
  395.                                         g_node_append_data($$,strdup(str));
  396.                                         free(str);
  397.                                 }
  398.                                 |
  399.                                 variable_arithmetique{
  400.                                         /* On recupere un pointeur vers la structure Variable */
  401.                                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  402.                                         /* Si on a trouve un pointeur valable */
  403.                                         if(var!=NULL){
  404.                                                 /* On verifie que le type est bien un entier - Inutile car impose a l'analyse syntaxique */
  405.                                                 if(strcmp(var->type,"entier")==0){
  406.                                                         $$=$1;
  407.                                                 }else{
  408.                                                         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);
  409.                                                         error_semantical=true;
  410.                                                 }
  411.                                         /* Sinon on conclue que la variable n'a jamais ete declaree car absente de la table */
  412.                                         }else{
  413.                                                 fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data);
  414.                                                 error_semantical=true;
  415.                                         }
  416.                                 }
  417.                                 |
  418.                                 addition{
  419.                                         $$=$1;
  420.                                 }
  421.                                 |
  422.                                 soustraction{
  423.                                         $$=$1;
  424.                                 }
  425.                                 |
  426.                                 multiplication{
  427.                                         $$=$1;
  428.                                 }
  429.                                 |
  430.                                 division{
  431.                                         $$=$1;
  432.                                 }
  433.                                 |
  434.                                 modulo{
  435.                                         $$=$1;
  436.                                 }
  437.                                 |
  438.                                 TOK_PLUS expression_arithmetique{
  439.                                     $$=$2;
  440.                                 }
  441.                                 |
  442.                                 expression_arithmetique TOK_INCREMENTATION{
  443.                                         printf("\t\t\tIncrementation de +1\n");
  444.                                     $$=g_node_new((gpointer)INCREMENTATION);
  445.                                     g_node_append($$,$1);
  446.                                 }
  447.                                 |
  448.                                 expression_arithmetique TOK_DECREMENTATION{
  449.                                         printf("\t\t\tDecrementation de -1\n");
  450.                                     $$=g_node_new((gpointer)DECREMENTATION);
  451.                                     g_node_append($$,$1);
  452.                                 }
  453.                                 |
  454.                                 TOK_MOINS expression_arithmetique{
  455.                                     printf("\t\t\tOperation unaire negation\n");
  456.                                     $$=g_node_new((gpointer)NEGATIF);
  457.                                         g_node_append($$,$2);
  458.                                 }
  459.                                 |
  460.                                 TOK_PARG expression_arithmetique TOK_PARD{
  461.                                         printf("\t\t\tC'est une expression artihmetique entre parentheses\n");
  462.                                         $$=g_node_new((gpointer)EXPR_PAR);
  463.                                         g_node_append($$,$2);
  464.                                 };
  465.  
  466. expression_booleenne:           TOK_VRAI{
  467.                                         printf("\t\t\tBooleen Vrai\n");
  468.                                         $$=g_node_new((gpointer)VRAI);
  469.                                 }
  470.                                 |
  471.                                 TOK_FAUX{
  472.                                         printf("\t\t\tBooleen Faux\n");
  473.                                         $$=g_node_new((gpointer)FAUX);
  474.                                 }
  475.                                 |
  476.                                 variable_booleenne{
  477.                                         /* On recupere un pointeur vers la structure Variable */
  478.                                         Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data);
  479.                                         /* Si on a trouve un pointeur valable */
  480.                                         if(var!=NULL){
  481.                                                 /* On verifie que le type est bien un entier - Inutile car impose a l'analyse syntaxique */
  482.                                                 if(strcmp(var->type,"booleen")==0){
  483.                                                         $$=$1;
  484.                                                 }else{
  485.                                                         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);
  486.                                                         error_semantical=true;
  487.                                                 }
  488.                                         /* Sinon on conclue que la variable n'a jamais ete declaree car absente de la table */
  489.                                         }else{
  490.                                                 fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data);
  491.                                                 error_semantical=true;
  492.                                         }
  493.                                 }
  494.                                 |
  495.                                 TOK_NON expression_booleenne{
  496.                                         printf("\t\t\tOperation booleenne Non\n");
  497.                                         $$=g_node_new((gpointer)NON);
  498.                                         g_node_append($$,$2);
  499.                                 }
  500.                                 |
  501.                                 expression_booleenne TOK_ET expression_booleenne{
  502.                                         printf("\t\t\tOperation booleenne Et\n");
  503.                                         $$=g_node_new((gpointer)ET);
  504.                                         g_node_append($$,$1);
  505.                                         g_node_append($$,$3);
  506.                                 }
  507.                                 |
  508.                                 expression_booleenne TOK_OU expression_booleenne{
  509.                                         printf("\t\t\tOperation booleenne Ou\n");
  510.                                         $$=g_node_new((gpointer)OU);
  511.                                         g_node_append($$,$1);
  512.                                         g_node_append($$,$3);
  513.                                 }
  514.                                 |
  515.                                 TOK_PARG expression_booleenne TOK_PARD{
  516.                                         printf("\t\t\tC'est une expression booleenne entre parentheses\n");
  517.                                         $$=g_node_new((gpointer)EXPR_PAR);
  518.                                         g_node_append($$,$2);
  519.                                 }
  520.                                 |
  521.                                 expression_booleenne TOK_EQU expression_booleenne{
  522.                                         printf("\t\t\tOperateur d'egalite ==\n");
  523.                                         $$=g_node_new((gpointer)EGALITE);
  524.                                         g_node_append($$,$1);
  525.                                         g_node_append($$,$3);
  526.                                 }
  527.                                 |
  528.                                 expression_booleenne TOK_DIFF expression_booleenne{
  529.                                         printf("\t\t\tOperateur d'inegalite !=\n");
  530.                                         $$=g_node_new((gpointer)DIFFERENT);
  531.                                         g_node_append($$,$1);
  532.                                         g_node_append($$,$3);
  533.                                 }
  534.                                 |
  535.                                 expression_arithmetique TOK_EQU expression_arithmetique{
  536.                                         printf("\t\t\tOperateur d'egalite ==\n");
  537.                                         $$=g_node_new((gpointer)EGALITE);
  538.                                         g_node_append($$,$1);
  539.                                         g_node_append($$,$3);
  540.                                 }
  541.                                 |
  542.                                 expression_arithmetique TOK_DIFF expression_arithmetique{
  543.                                         printf("\t\t\tOperateur d'inegalite !=\n");
  544.                                         $$=g_node_new((gpointer)DIFFERENT);
  545.                                         g_node_append($$,$1);
  546.                                         g_node_append($$,$3);
  547.                                 }
  548.                                 |
  549.                                 expression_arithmetique TOK_SUP expression_arithmetique{
  550.                                         printf("\t\t\tOperateur de superiorite >\n");
  551.                                         $$=g_node_new((gpointer)SUPERIEUR);
  552.                                         g_node_append($$,$1);
  553.                                         g_node_append($$,$3);
  554.                                 }
  555.                                 |
  556.                                 expression_arithmetique TOK_INF expression_arithmetique{
  557.                                         printf("\t\t\tOperateur d'inferiorite <\n");
  558.                                         $$=g_node_new((gpointer)INFERIEUR);
  559.                                         g_node_append($$,$1);
  560.                                         g_node_append($$,$3);
  561.                                 }
  562.                                 |
  563.                                 expression_arithmetique TOK_SUPEQU expression_arithmetique{
  564.                                         printf("\t\t\tOperateur >=\n");
  565.                                         $$=g_node_new((gpointer)SUPEGAL);
  566.                                         g_node_append($$,$1);
  567.                                         g_node_append($$,$3);
  568.                                 }
  569.                                 |
  570.                                 expression_arithmetique TOK_INFEQU expression_arithmetique{
  571.                                         printf("\t\t\tOperateur <=\n");
  572.                                         $$=g_node_new((gpointer)INFEGAL);
  573.                                         g_node_append($$,$1);
  574.                                         g_node_append($$,$3);
  575.                                 }
  576.                                 |
  577.                 expression_arithmetique TOK_IN TOK_CROG expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROD{
  578.                                         printf("\t\t\tOperateur dans\n");
  579.                                         $$=g_node_new((gpointer)DANSII);
  580.                                         g_node_append($$,$1);
  581.                                         g_node_append($$,$4);
  582.                                         g_node_append($$,$6);
  583.                                 }
  584.                                 |
  585.                 expression_arithmetique TOK_IN TOK_CROD expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROD{
  586.                                         printf("\t\t\tOperateur dans\n");
  587.                                         $$=g_node_new((gpointer)DANSEI);
  588.                                         g_node_append($$,$1);
  589.                                         g_node_append($$,$4);
  590.                                         g_node_append($$,$6);
  591.                                 }
  592.                                 |
  593.                 expression_arithmetique TOK_IN TOK_CROG expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROG{
  594.                                         printf("\t\t\tOperateur dans\n");
  595.                                         $$=g_node_new((gpointer)DANSIE);
  596.                                         g_node_append($$,$1);
  597.                                         g_node_append($$,$4);
  598.                                         g_node_append($$,$6);
  599.                                 }
  600.                                 |
  601.                 expression_arithmetique TOK_IN TOK_CROD expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROG{
  602.                                         printf("\t\t\tOperateur dans\n");
  603.                                         $$=g_node_new((gpointer)DANSEE);
  604.                                         g_node_append($$,$1);
  605.                                         g_node_append($$,$4);
  606.                                         g_node_append($$,$6);
  607.                                 };
  608.  
  609. addition:       expression_arithmetique TOK_PLUS expression_arithmetique{
  610.                         printf("\t\t\tAddition\n");
  611.                         $$=g_node_new((gpointer)ADDITION);
  612.                         g_node_append($$,$1);
  613.                         g_node_append($$,$3);
  614.                 };
  615.  
  616. soustraction:   expression_arithmetique TOK_MOINS expression_arithmetique{
  617.                                 printf("\t\t\tSoustraction\n");
  618.                                 $$=g_node_new((gpointer)SOUSTRACTION);
  619.                                 g_node_append($$,$1);
  620.                                 g_node_append($$,$3);
  621.                         };
  622.  
  623. multiplication: expression_arithmetique TOK_MUL expression_arithmetique{
  624.                         printf("\t\t\tMultiplication\n");
  625.                         $$=g_node_new((gpointer)MULTIPLICATION);
  626.                         g_node_append($$,$1);
  627.                         g_node_append($$,$3);
  628.                 };
  629.  
  630. division:       expression_arithmetique TOK_DIV expression_arithmetique{
  631.                                 printf("\t\t\tDivision\n");
  632.                                 $$=g_node_new((gpointer)DIVISION);
  633.                                 g_node_append($$,$1);
  634.                                 g_node_append($$,$3);
  635.                         };
  636.  
  637. modulo:         expression_arithmetique TOK_MOD expression_arithmetique{
  638.                                 printf("\t\t\tModulo\n");
  639.                                 $$=g_node_new((gpointer)MODULO);
  640.                                 g_node_append($$,$1);
  641.                                 g_node_append($$,$3);
  642.                         };
  643.  
  644. %%
  645.  
  646. /* Dans la fonction main on appelle bien la routine yyparse() qui sera genere par Bison. Cette routine appellera yylex() de notre analyseur lexical. */
  647.  
  648. int main(int argc, char** argv){
  649.         /* recuperation du nom de fichier d'entree (langage Simple) donne en parametre */
  650.         char* fichier_entree=strdup(argv[1]);
  651.         /* ouverture du fichier en lecture dans le flux d'entree stdin */
  652.         stdin=fopen(fichier_entree,"r");
  653.         /* creation fichier de sortie (langage C) */
  654.         char* fichier_sortie=strdup(argv[1]);
  655.         /* remplace l'extension par .c */
  656.         strcpy(rindex(fichier_sortie, '.'), ".c");
  657.         /* ouvre le fichier cree en ecriture */
  658.         fichier=fopen(fichier_sortie, "w");
  659.         /* Creation de la table de hachage */
  660.         table_variable=g_hash_table_new_full(g_str_hash,g_str_equal,NULL,free);
  661.         printf("Debut de l'analyse syntaxique :\n");
  662.         debut_code();
  663.         yyparse();
  664.         fin_code();
  665.         printf("Fin de l'analyse !\n");
  666.         printf("Resultat :\n");
  667.         if(error_lexical){
  668.                 printf("\t-- Echec : Certains lexemes ne font pas partie du lexique du langage ! --\n");
  669.                 printf("\t-- Echec a l'analyse lexicale --\n");
  670.         }
  671.         else{
  672.                 printf("\t-- Succes a l'analyse lexicale ! --\n");
  673.         }
  674.         if(error_syntaxical){
  675.                 printf("\t-- Echec : Certaines phrases sont syntaxiquement incorrectes ! --\n");
  676.                 printf("\t-- Echec a l'analyse syntaxique --\n");
  677.         }
  678.         else{
  679.                 printf("\t-- Succes a l'analyse syntaxique ! --\n");
  680.                 if(error_semantical){
  681.                         printf("\t-- Echec : Certaines phrases sont semantiquement incorrectes ! --\n");
  682.                         printf("\t-- Echec a l'analyse semantique --\n");
  683.                 }
  684.                 else{
  685.                         printf("\t-- Succes a l'analyse semantique ! --\n");
  686.                 }
  687.         }
  688.         /* Suppression du fichier genere si erreurs analyse */
  689.         if(error_lexical||error_syntaxical||error_semantical){
  690.                 remove(fichier_sortie);
  691.                 printf("ECHEC GENERATION CODE !\n");
  692.         }
  693.         else{
  694.                 printf("Le fichier \"%s\" a ete genere !\n",fichier_sortie);
  695.         }
  696.         /* Fermeture des flux */
  697.         fclose(fichier);
  698.         fclose(stdin);
  699.         /* Liberation memoire */
  700.         free(fichier_entree);
  701.         free(fichier_sortie);
  702.         g_hash_table_destroy(table_variable);
  703.         return EXIT_SUCCESS;
  704. }
  705.  
  706. void yyerror(char *s) {
  707.         fprintf(stderr, "Erreur de syntaxe a la ligne %d: %s\n", lineno, s);
  708. }
  709.  
  710. /* Cette fonction supprime dans la table de hachage toutes les variables declarees pour la premiere fois dans l'arbre syntaxique donne en parametre */
  711.  
  712. void supprime_variable(GNode* ast){
  713.     /* 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) */
  714.     if(ast&&!G_NODE_IS_LEAF(ast)&&(long)ast->data!=BLOC_CODE){
  715.         /* si le noeud est de type declaration */
  716.         if((long)ast->data==AFFECTATIONB||(long)ast->data==AFFECTATIONE){
  717.             /* suppression de la variable dans la table de hachage */
  718.             if(g_hash_table_remove(table_variable,(char*)g_node_nth_child(g_node_nth_child(ast,0),0)->data)){
  719.                 printf("Variable supprimee !\n");
  720.             }else{
  721.                 fprintf(stderr,"ERREUR - PROBLEME DE SUPPRESSION VARIABLE !\n");
  722.                 exit(-1);
  723.             }
  724.         /* sinon on continue de parcourir l'arbre */
  725.         }else{
  726.             int nb_enfant;
  727.             for(nb_enfant=0;nb_enfant<=g_node_n_children(ast);nb_enfant++){
  728.                 supprime_variable(g_node_nth_child(ast,nb_enfant));
  729.             }
  730.         }
  731.     }
  732. }
simple.h
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <glib.h>
  6. #include "syntaxe_simple.tab.h"
  7. int yylex(void);
  8. void yyerror(char*);
  9. extern unsigned int lineno;
  10. extern bool error_lexical;
  11.  
  12. /* Le flux de notre fichier de sortie final */
  13. FILE* fichier;
  14.  
  15. /* Definition des methodes de generation de code C */
  16. extern void debut_code(void);
  17. extern void genere_code(GNode*);
  18. extern void fin_code(void);
  19.  
  20. /* 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. */
  21. #define CODE_VIDE       0
  22. #define SEQUENCE        1
  23. #define VARIABLE        2
  24. #define AFFECTATION     3
  25. #define AFFECTATIONE    4
  26. #define AFFECTATIONB    5
  27. #define AFFICHAGEE      6
  28. #define AFFICHAGEB      7
  29. #define ENTIER          8
  30. #define ADDITION        9
  31. #define SOUSTRACTION    10
  32. #define MULTIPLICATION  11
  33. #define DIVISION        12
  34. #define ET              13
  35. #define OU              14
  36. #define NON             15
  37. #define VRAI            16
  38. #define FAUX            17
  39. #define EXPR_PAR        18
  40. #define EGALITE         19
  41. #define DIFFERENT       20
  42. #define SUPERIEUR       21
  43. #define INFERIEUR       22
  44. #define SUPEGAL         23
  45. #define INFEGAL         24
  46. #define DANSII          25      /* inclus-inclus */
  47. #define DANSEI          26      /* exclus-inclus */
  48. #define DANSIE          27      /* inclus-exclus */
  49. #define DANSEE          28      /* exclus-exclus */
  50. #define CONDITION_SI    29
  51. #define CONDITION_SI_SINON 30
  52. #define SI          31
  53. #define SINON       32
  54. #define NEGATIF     33
  55. #define BLOC_CODE   34
  56. #define MODULO      35
  57. #define AFFECTATION_PLUS    36
  58. #define AFFECTATION_MOINS   37
  59. #define AFFECTATION_MUL     38
  60. #define AFFECTATION_DIV     39
  61. #define AFFECTATION_MOD     40
  62. #define AFFECTATION_ET      41
  63. #define AFFECTATION_OU      42
  64. #define INCREMENTATION      43
  65. #define DECREMENTATION      44
  66. #define AFFECTATION_INCR    45
  67. #define AFFECTATION_DECR    46
  68. #define BOUCLE_FOR          47
  69. #define BOUCLE_WHILE        48
  70. #define BOUCLE_DO_WHILE     49
generation_code.c
  1. #include "simple.h"
  2.  
  3. unsigned int NB_BOUCLE=0;
  4.  
  5. void debut_code(){
  6.         fprintf(fichier, "/* FICHIER GENERE PAR LE COMPILATEUR SIMPLE */\n\n");
  7.         fprintf(fichier, "#include<stdlib.h>\n#include<stdbool.h>\n#include<stdio.h>\n#include<string.h>\n\n");
  8.         fprintf(fichier, "int main(void){\n");
  9. }
  10.  
  11. void fin_code(){
  12.         fprintf(fichier, "\treturn EXIT_SUCCESS;\n");
  13.         fprintf(fichier, "}\n");
  14. }
  15.  
  16. void genere_code(GNode* ast){
  17.         if(ast){
  18.                 switch((long)ast->data){
  19.                         case SEQUENCE:
  20.                                 genere_code(g_node_nth_child(ast,0));
  21.                                 genere_code(g_node_nth_child(ast,1));
  22.                                 break;
  23.                         case VARIABLE:
  24.                                 fprintf(fichier,"%s",(char*)g_node_nth_child(ast,0)->data);
  25.                                 break;
  26.                         case AFFECTATIONE:
  27.                                 fprintf(fichier,"\tint ");
  28.                                 genere_code(g_node_nth_child(ast,0));
  29.                                 fprintf(fichier,"=");
  30.                                 genere_code(g_node_nth_child(ast,1));
  31.                                 fprintf(fichier,";\n");
  32.                                 break;
  33.                         case AFFECTATIONB:
  34.                                 fprintf(fichier,"\tbool ");
  35.                                 genere_code(g_node_nth_child(ast,0));
  36.                                 fprintf(fichier,"=");
  37.                                 genere_code(g_node_nth_child(ast,1));
  38.                                 fprintf(fichier,";\n");
  39.                                 break;
  40.                         case AFFECTATION:
  41.                                 fprintf(fichier,"\t");
  42.                                 genere_code(g_node_nth_child(ast,0));
  43.                                 fprintf(fichier,"=");
  44.                                 genere_code(g_node_nth_child(ast,1));
  45.                                 fprintf(fichier,";\n");
  46.                                 break;
  47.                         case AFFECTATION_PLUS:
  48.                                 fprintf(fichier,"\t");
  49.                                 genere_code(g_node_nth_child(ast,0));
  50.                                 fprintf(fichier,"+=");
  51.                                 genere_code(g_node_nth_child(ast,1));
  52.                                 fprintf(fichier,";\n");
  53.                                 break;
  54.                         case AFFECTATION_MOINS:
  55.                                 fprintf(fichier,"\t");
  56.                                 genere_code(g_node_nth_child(ast,0));
  57.                                 fprintf(fichier,"-=");
  58.                                 genere_code(g_node_nth_child(ast,1));
  59.                                 fprintf(fichier,";\n");
  60.                                 break;
  61.                         case AFFECTATION_MUL:
  62.                                 fprintf(fichier,"\t");
  63.                                 genere_code(g_node_nth_child(ast,0));
  64.                                 fprintf(fichier,"*=");
  65.                                 genere_code(g_node_nth_child(ast,1));
  66.                                 fprintf(fichier,";\n");
  67.                                 break;
  68.                         case AFFECTATION_DIV:
  69.                                 fprintf(fichier,"\t");
  70.                                 genere_code(g_node_nth_child(ast,0));
  71.                                 fprintf(fichier,"/=");
  72.                                 genere_code(g_node_nth_child(ast,1));
  73.                                 fprintf(fichier,";\n");
  74.                                 break;
  75.                         case AFFECTATION_MOD:
  76.                                 fprintf(fichier,"\t");
  77.                                 genere_code(g_node_nth_child(ast,0));
  78.                                 fprintf(fichier,"%%=");
  79.                                 genere_code(g_node_nth_child(ast,1));
  80.                                 fprintf(fichier,";\n");
  81.                                 break;
  82.                         case AFFECTATION_ET:
  83.                                 fprintf(fichier,"\t");
  84.                                 genere_code(g_node_nth_child(ast,0));
  85.                                 fprintf(fichier,"&=");
  86.                                 genere_code(g_node_nth_child(ast,1));
  87.                                 fprintf(fichier,";\n");
  88.                                 break;
  89.                         case AFFECTATION_OU:
  90.                                 fprintf(fichier,"\t");
  91.                                 genere_code(g_node_nth_child(ast,0));
  92.                                 fprintf(fichier,"|=");
  93.                                 genere_code(g_node_nth_child(ast,1));
  94.                                 fprintf(fichier,";\n");
  95.                                 break;
  96.                         case AFFECTATION_INCR:
  97.                                 fprintf(fichier,"\t");
  98.                                 genere_code(g_node_nth_child(ast,0));
  99.                                 fprintf(fichier,"++;\n");
  100.                                 break;
  101.                         case AFFECTATION_DECR:
  102.                                 fprintf(fichier,"\t");
  103.                                 genere_code(g_node_nth_child(ast,0));
  104.                                 fprintf(fichier,"--;\n");
  105.                                 break;
  106.                         case AFFICHAGEE:
  107.                                 fprintf(fichier,"\tprintf(\"%%ld\",");
  108.                                 genere_code(g_node_nth_child(ast,0));
  109.                                 fprintf(fichier,");\n");
  110.                                 break;
  111.                         case AFFICHAGEB:
  112.                                 fprintf(fichier,"\tprintf(\"%%s\",");
  113.                                 genere_code(g_node_nth_child(ast,0));
  114.                                 fprintf(fichier,"?\"vrai\":\"faux\");\n");
  115.                                 break;
  116.                         case ENTIER:
  117.                                 fprintf(fichier,"%s",(char*)g_node_nth_child(ast,0)->data);
  118.                                 break;
  119.                         case ADDITION:
  120.                                 genere_code(g_node_nth_child(ast,0));
  121.                                 fprintf(fichier,"+");
  122.                                 genere_code(g_node_nth_child(ast,1));
  123.                                 break;
  124.                         case SOUSTRACTION:
  125.                                 genere_code(g_node_nth_child(ast,0));
  126.                                 fprintf(fichier,"-");
  127.                                 genere_code(g_node_nth_child(ast,1));
  128.                                 break;
  129.                         case MULTIPLICATION:
  130.                                 genere_code(g_node_nth_child(ast,0));
  131.                                 fprintf(fichier,"*");
  132.                                 genere_code(g_node_nth_child(ast,1));
  133.                                 break;
  134.                         case DIVISION:
  135.                                 genere_code(g_node_nth_child(ast,0));
  136.                                 fprintf(fichier,"/");
  137.                                 genere_code(g_node_nth_child(ast,1));
  138.                                 break;
  139.                         case MODULO:
  140.                                 genere_code(g_node_nth_child(ast,0));
  141.                                 fprintf(fichier,"%%");
  142.                                 genere_code(g_node_nth_child(ast,1));
  143.                                 break;
  144.                         case INCREMENTATION:
  145.                                 genere_code(g_node_nth_child(ast,0));
  146.                                 fprintf(fichier,"+1");
  147.                                 break;
  148.                         case DECREMENTATION:
  149.                                 genere_code(g_node_nth_child(ast,0));
  150.                                 fprintf(fichier,"-1");
  151.                                 break;
  152.                         case VRAI:
  153.                                 fprintf(fichier,"true");
  154.                                 break;
  155.                         case FAUX:
  156.                                 fprintf(fichier,"false");
  157.                                 break;
  158.                         case ET:
  159.                                 genere_code(g_node_nth_child(ast,0));
  160.                                 fprintf(fichier,"&&");
  161.                                 genere_code(g_node_nth_child(ast,1));
  162.                                 break;
  163.                         case OU:
  164.                                 genere_code(g_node_nth_child(ast,0));
  165.                                 fprintf(fichier,"||");
  166.                                 genere_code(g_node_nth_child(ast,1));
  167.                                 break;
  168.                         case NON:
  169.                                 fprintf(fichier,"!");
  170.                                 genere_code(g_node_nth_child(ast,0));
  171.                                 break;
  172.                         case EXPR_PAR:
  173.                                 fprintf(fichier,"(");
  174.                                 genere_code(g_node_nth_child(ast,0));
  175.                                 fprintf(fichier,")");
  176.                                 break;
  177.                         case EGALITE:
  178.                                 genere_code(g_node_nth_child(ast,0));
  179.                                 fprintf(fichier,"==");
  180.                                 genere_code(g_node_nth_child(ast,1));
  181.                                 break;
  182.                         case DIFFERENT:
  183.                                 genere_code(g_node_nth_child(ast,0));
  184.                                 fprintf(fichier,"!=");
  185.                                 genere_code(g_node_nth_child(ast,1));
  186.                                 break;
  187.                         case INFERIEUR:
  188.                                 genere_code(g_node_nth_child(ast,0));
  189.                                 fprintf(fichier,"<");
  190.                                 genere_code(g_node_nth_child(ast,1));
  191.                                 break;
  192.                         case SUPERIEUR:
  193.                                 genere_code(g_node_nth_child(ast,0));
  194.                                 fprintf(fichier,">");
  195.                                 genere_code(g_node_nth_child(ast,1));
  196.                                 break;
  197.                         case INFEGAL:
  198.                                 genere_code(g_node_nth_child(ast,0));
  199.                                 fprintf(fichier,"<=");
  200.                                 genere_code(g_node_nth_child(ast,1));
  201.                                 break;
  202.                         case SUPEGAL:
  203.                                 genere_code(g_node_nth_child(ast,0));
  204.                                 fprintf(fichier,">=");
  205.                                 genere_code(g_node_nth_child(ast,1));
  206.                                 break;
  207.                         case DANSII:
  208.                                 genere_code(g_node_nth_child(ast,0));
  209.                                 fprintf(fichier,">=");
  210.                                 genere_code(g_node_nth_child(ast,1));
  211.                                 fprintf(fichier,"&&");
  212.                                 genere_code(g_node_nth_child(ast,0));
  213.                                 fprintf(fichier,"<=");
  214.                                 genere_code(g_node_nth_child(ast,2));
  215.                                 break;
  216.                         case DANSEI:
  217.                                 genere_code(g_node_nth_child(ast,0));
  218.                                 fprintf(fichier,">");
  219.                                 genere_code(g_node_nth_child(ast,1));
  220.                                 fprintf(fichier,"&&");
  221.                                 genere_code(g_node_nth_child(ast,0));
  222.                                 fprintf(fichier,"<=");
  223.                                 genere_code(g_node_nth_child(ast,2));
  224.                                 break;
  225.                         case DANSIE:
  226.                                 genere_code(g_node_nth_child(ast,0));
  227.                                 fprintf(fichier,">=");
  228.                                 genere_code(g_node_nth_child(ast,1));
  229.                                 fprintf(fichier,"&&");
  230.                                 genere_code(g_node_nth_child(ast,0));
  231.                                 fprintf(fichier,"<");
  232.                                 genere_code(g_node_nth_child(ast,2));
  233.                                 break;
  234.                         case DANSEE:
  235.                                 genere_code(g_node_nth_child(ast,0));
  236.                                 fprintf(fichier,">");
  237.                                 genere_code(g_node_nth_child(ast,1));
  238.                                 fprintf(fichier,"&&");
  239.                                 genere_code(g_node_nth_child(ast,0));
  240.                                 fprintf(fichier,"<");
  241.                                 genere_code(g_node_nth_child(ast,2));
  242.                                 break;
  243.                         case NEGATIF:
  244.                                 fprintf(fichier,"-");
  245.                                 genere_code(g_node_nth_child(ast,0));
  246.                                 break;
  247.                         case CONDITION_SI:
  248.                                 genere_code(g_node_nth_child(ast,0));
  249.                                 fprintf(fichier,"\n");
  250.                                 break;
  251.                         case CONDITION_SI_SINON:
  252.                                 genere_code(g_node_nth_child(ast,0));
  253.                                 genere_code(g_node_nth_child(ast,1));
  254.                                 break;
  255.                         case SI:
  256.                                 fprintf(fichier,"\tif(");
  257.                                 genere_code(g_node_nth_child(ast,0));
  258.                                 fprintf(fichier,"){\n");
  259.                                 genere_code(g_node_nth_child(ast,1));
  260.                                 fprintf(fichier,"\t}");
  261.                                 break;
  262.                         case SINON:
  263.                                 fprintf(fichier,"else{\n");
  264.                                 genere_code(g_node_nth_child(ast,0));
  265.                                 fprintf(fichier,"\t}\n");
  266.                                 break;
  267.                         case BLOC_CODE:
  268.                                 genere_code(g_node_nth_child(ast,0));
  269.                                 break;
  270.                         case BOUCLE_FOR:
  271.                                 fprintf(fichier,"\tint i%i;\n",NB_BOUCLE);
  272.                                 fprintf(fichier,"\tfor(i%i=0;i%i<",NB_BOUCLE,NB_BOUCLE);
  273.                                 genere_code(g_node_nth_child(ast,0));
  274.                                 fprintf(fichier,";i%i++){\n",NB_BOUCLE);
  275.                                 NB_BOUCLE++;
  276.                                 genere_code(g_node_nth_child(ast,1));
  277.                                 fprintf(fichier,"\t}\n");
  278.                                 break;
  279.                         case BOUCLE_WHILE:
  280.                                 fprintf(fichier,"\twhile(");
  281.                                 genere_code(g_node_nth_child(ast,0));
  282.                                 fprintf(fichier,"){\n");
  283.                                 genere_code(g_node_nth_child(ast,1));
  284.                                 fprintf(fichier,"\t}\n");
  285.                                 break;
  286.                         case BOUCLE_DO_WHILE:
  287.                                 fprintf(fichier,"\tdo{\n");
  288.                                 genere_code(g_node_nth_child(ast,0));
  289.                                 fprintf(fichier,"\t}while(");
  290.                                 genere_code(g_node_nth_child(ast,1));
  291.                                 fprintf(fichier,");\n");
  292.                                 break;
  293.                 }
  294.         }
  295. }

Comme dit dans la vidéo, la prochaine évolution concernera les chaînes de caractères. Grosse évolution car il faudra gérer le mécanisme d'allocation mémoire et de pointeur. Une chaîne n'est rien d'autre qu'un tableau de caractères. En C, il s'agit d'une zone mémoire où sont stockés les caractères. On manipulera donc des pointeurs (adresses mémoires) vers des zones de char (char*). L'implémentation des chaînes de caractères se fera certainement en plusieurs parties.

<< Évolution 4 : Les raccourcis syntaxiques | Évolution 5 : Les boucles | Évolution 6 : Les chaînes de caractères (Partie 1 - Variable et déclaration) >>

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

Page last modified on July 09, 2017, at 07:52 PM EST

This page has been requested 2834 times (Today : 1) - Total number of requests : 261845

Edit - History - Statistics - Print - Recent Changes - Search

Clin d'oeil aux victimes des attentats survenus dans la soirée du 13 novembre 2015. La nouvelle version du site a été installée quelques heures avant.