Recent Changes - Search:

My Projects

Courses

Writings

Source Code

Social Networks

Live Traffic !

Évolution 4 : Les raccourcis syntaxiques

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

<< Évolution 3 : Les commentaires | Évolution 4 : Les raccourcis syntaxiques | Évolution 5 : Les boucles >>

Nous allons implémenter quelques raccourcis syntaxiques. On apporte en soi aucune nouveauté au langage. On ajoute l'opérateur arithmétique binaire modulo, l'incrémentation et décrémentation et des raccourcis sur les affectations. On ajoute aussi le lexème "!" qui représentera le même token que celui que représente le lexème "non". On offre une nouvelle syntaxe possible pour le If/Else. On peut écrire des conditions sous la forme (expression_booleenne) ? code_si_vrai : code_si_faux ;.

Évolution 4 du langage :

  1. <condition> ::= "(" <expression_booleenne> ")" "?" <code > ":" <code > ";"
  2. <non> ::= <non> | "!" <expression_booleenne>
  3. <expression_arithmetique> ::= <expression_arithmetique> | <expression_arithmetique> "%" <expression_arithmetique> | <expression_arithmetique> "++" | <expression_arithmetique> "--"
  4. <affectation> ::= <affectation>
  5.                 | variable_arithmetique "+=" <expression_arithmetique> ";"
  6.                 | variable_arithmetique "-=" <expression_arithmetique> ";"
  7.                 | variable_arithmetique "*=" <expression_arithmetique> ";"
  8.                 | variable_arithmetique "/=" <expression_arithmetique> ";"
  9.                 | variable_arithmetique "%=" <expression_arithmetique> ";"
  10.                 | variable_booleenne "&=" <expression_boolenne> ";"
  11.                 | variable_booleenne "|=" <expression_boolenne> ";"
  12.                 | variable_arithmetique "++" ";"
  13.                 | variable_arithmetique "--" ";"

Nous avons les lexèmes suivants à ajouter : "%", "++", "--", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "!", ":" et "?" :

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. "="             {return TOK_AFFECT;}
  102.  
  103. "+"             {return TOK_PLUS;}
  104.  
  105. "-"             {return TOK_MOINS;}
  106.  
  107. "*"             {return TOK_MUL;}
  108.  
  109. "/"             {return TOK_DIV;}
  110.  
  111. "%"             {return TOK_MOD;}
  112.  
  113. "("             {return TOK_PARG;}
  114.  
  115. ")"             {return TOK_PARD;}
  116.  
  117. "["             {return TOK_CROG;}
  118.  
  119. "]"             {return TOK_CROD;}
  120.  
  121. "?"             {return TOK_POINT_INTERROGATION;}
  122.  
  123. ":"             {return TOK_DOUBLE_POINT;}
  124.  
  125. "et"            {return TOK_ET;}
  126.  
  127. "ou"            {return TOK_OU;}
  128.  
  129. "non"|"!"       {return TOK_NON;}
  130.  
  131. ";"             {return TOK_FINSTR;}
  132.  
  133. "vrai"          {return TOK_VRAI;}
  134.  
  135. "faux"          {return TOK_FAUX;}
  136.  
  137. "\n"            {lineno++;}
  138.  
  139. {variable_booleenne} {
  140.     yylval.texte = yytext;
  141.     return TOK_VARB;
  142. }
  143.  
  144.  
  145. {variable_arithmetique} {
  146.     yylval.texte = yytext;
  147.     return TOK_VARE;
  148. }
  149.  
  150. {commentaire}   {
  151.     printf("Commentaire detecte en ligne %i\n",lineno);
  152.     printf("Fin du commentaire en ligne %i\n",lineno);
  153.     return TOK_COMMENT;
  154. }
  155.  
  156. " "|"\t" {}
  157.  
  158. . {
  159.     fprintf(stderr,"\tERREUR : Lexeme inconnu a la ligne %d. Il s'agit de %s et comporte %d lettre(s)\n",lineno,yytext,yyleng);
  160.     error_lexical=true;
  161.     return yytext[0];
  162. }
  163.  
  164. %%

On ajoute les séquences syntaxiques dans le fichier d'entête :

simple.h
  1. #define MODULO      35
  2. #define AFFECTATION_PLUS    36
  3. #define AFFECTATION_MOINS   37
  4. #define AFFECTATION_MUL     38
  5. #define AFFECTATION_DIV     39
  6. #define AFFECTATION_MOD     40
  7. #define AFFECTATION_ET      41
  8. #define AFFECTATION_OU      42
  9. #define INCREMENTATION      43
  10. #define DECREMENTATION      44
  11. #define AFFECTATION_INCR    45
  12. #define AFFECTATION_DECR    46

On met à jour l'analyseur syntaxique et le générateur de code C respectivement :

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

On compile le compilateur et on lui passe en entrée ce fichier test :

programme.simple
  1. entier=5;
  2. booleen=vrai;
  3.  
  4. // Test de l'operateur Modulo %
  5.  
  6. afficher entier%2; #affiche 1
  7.  
  8. // Test de l'incrementation
  9.  
  10. entier++;
  11. afficher entier; #affiche 6
  12.  
  13. // Test de la decrementation
  14.  
  15. entier--;
  16. afficher entier; #affiche 5
  17.  
  18. /* TEST DES AFFECTATIONS */
  19.  
  20. // Expressions booleennes
  21.  
  22. booleen&=faux;
  23. afficher booleen; #affiche faux
  24.  
  25. booleen|=vrai;
  26. afficher booleen; #affiche vrai
  27.  
  28. // Expressions arithmetiques
  29.  
  30. entier+=5;
  31. afficher entier; #affiche 10
  32. entier-=5;
  33. afficher entier; #affiche 5
  34. entier*=20;
  35. afficher entier; #affiche 100
  36. entier/=2;
  37. afficher entier; #affiche 50
  38. entier%=45;
  39. afficher entier; #affiche 5
  40.  
  41. // Test du If/Else reduit
  42. (entier==5)?
  43.         afficher entier++; #affiche 6
  44. :
  45.         afficher entier--;
  46. ;
  47.  
  48. afficher entier; #affiche 5 (la variable entier n'a pas ete incremente dans la boucle if car il ne s'agissait pas d'une affectation mais d'une simple expression arithmetique)

On compile le fichier C fraîchement généré par notre compilateur et on obtient sans surprise ceci :

1
6
5
faux
vrai
10
5
100
50
5
6
5

La prochaine évolution sera plus importante car il s'agira cette fois-ci des boucles. Alors à toute !

<< Évolution 3 : Les commentaires | Évolution 4 : Les raccourcis syntaxiques | Évolution 5 : Les boucles >>

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

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

This page has been requested 1989 times (Today : 2) - Total number of requests : 262521

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.