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 :

⚠ (:source lang=bnf linenum:) <condition> ::= "(" <expression_booleenne> ")" "?" <code > ":" <code > ";" <non> ::= <non> | "!" <expression_booleenne> <expression_arithmetique> ::= <expression_arithmetique> | <expression_arithmetique> "%" <expression_arithmetique> | <expression_arithmetique> "++" | <expression_arithmetique> "--" <affectation> ::= <affectation> | variable_arithmetique "+=" <expression_arithmetique> ";" | variable_arithmetique "-=" <expression_arithmetique> ";" | variable_arithmetique "*=" <expression_arithmetique> ";" | variable_arithmetique "/=" <expression_arithmetique> ";" | variable_arithmetique "%=" <expression_arithmetique> ";" | variable_booleenne "&=" <expression_boolenne> ";" | variable_booleenne "|=" <expression_boolenne> ";" | variable_arithmetique "++" ";" | variable_arithmetique "--" ";" (:sourcend:) Nous avons les lexèmes suivants à ajouter : "%", "++", "--", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "!", ":" et "?" : ⚠ (:source lang=c header="lexique_simple.lex" linenum:) %{ <:vspace> #include "simple.h" unsigned int lineno=1; bool error_lexical=false; <:vspace> %} <:vspace> %option noyywrap <:vspace> nombre 0|[1-9][[:digit:]]* variable_booleenne b(_|[[:alnum:]])* variable_arithmetique e(_|[[:alnum:]])* <:vspace> /* regex de commentaire d'une seule ligne */ commentaire ((\/\/|#).*) <:vspace> /* pour les commentaires de plusieurs lignes, on declare nos deux lexemes en tant que conditions de demarrage exclusives (%x) dans Flex */ %x commentaire_1 %x commentaire_2 <:vspace> %% <:vspace> "/*" { /* un marqueur de debut de commentaire trouve -> on lui dit que le lexeme commentaire_1 commence */ BEGIN(commentaire_1); printf("Commentaire detecte en ligne %i\n",lineno); } <:vspace> <commentaire_1>"\n" { /* si on trouve des retours chariots et que la condition de demarrage est commentaire_1, alors on incremente la variable lineno. sans cela, on serait en decalage pour la suite de l'analyse */ lineno++; } <:vspace> <commentaire_1>"*"+"/" { /* si on au moins une fois "*" suivi de "/" et que la condition de demarrage est commentaire_1, alors on lui dit que le lexeme commentaire_1 est fini */ BEGIN(INITIAL); printf("Fin du commentaire en ligne %i\n",lineno); return TOK_COMMENT; } <:vspace> <commentaire_1>. {/* les autres caracteres suivants la conditions de demarrage sont absorbes par l'analyse est donc ingores */} <:vspace> "<!--" { BEGIN(commentaire_2); printf("Commentaire detecte en ligne %i\n",lineno); } <commentaire_2>"\n" {lineno++;} <commentaire_2>"-"+"-"+">" { BEGIN(INITIAL); printf("Fin du commentaire en ligne %i\n",lineno); return TOK_COMMENT; } <commentaire_2>. {} <:vspace> {nombre} { sscanf(yytext, "%ld", &yylval.nombre); return TOK_NOMBRE; } <:vspace> "si" {return TOK_SI;} <:vspace> "alors" {return TOK_ALORS;} <:vspace> "sinon" {return TOK_SINON;} <:vspace> "++" {return TOK_INCREMENTATION;} <:vspace> "--" {return TOK_DECREMENTATION;} <:vspace> "+=" {return TOK_AFFECT_PLUS;} <:vspace> "-=" {return TOK_AFFECT_MOINS;} <:vspace> "*=" {return TOK_AFFECT_MUL;} <:vspace> "/=" {return TOK_AFFECT_DIV;} <:vspace> "%=" {return TOK_AFFECT_MOD;} <:vspace> "&=" {return TOK_AFFECT_ET;} <:vspace> "|=" {return TOK_AFFECT_OU;} <:vspace> "egal a"|"equivalent a"|"==" {return TOK_EQU;} <:vspace> "different de"|"!="|"<>" {return TOK_DIFF;} <:vspace> "superieur a"|"plus grand que"|">" {return TOK_SUP;} <:vspace> "inferieur a"|"plus petit que"|"<" {return TOK_INF;} <:vspace> "superieur ou egal a"|">=" {return TOK_SUPEQU;} <:vspace> "inferieur ou egal a"|"<=" {return TOK_INFEQU;} <:vspace> "compris dans"|"dans" {return TOK_IN;} <:vspace> "afficher" {return TOK_AFFICHER;} <:vspace> "=" {return TOK_AFFECT;} <:vspace> "+" {return TOK_PLUS;} <:vspace> "-" {return TOK_MOINS;} <:vspace> "*" {return TOK_MUL;} <:vspace> "/" {return TOK_DIV;} <:vspace> "%" {return TOK_MOD;} <:vspace> "(" {return TOK_PARG;} <:vspace> ")" {return TOK_PARD;} <:vspace> "[" {return TOK_CROG;} <:vspace> "]" {return TOK_CROD;} <:vspace> "?" {return TOK_POINT_INTERROGATION;} <:vspace> ":" {return TOK_DOUBLE_POINT;} <:vspace> "et" {return TOK_ET;} <:vspace> "ou" {return TOK_OU;} <:vspace> "non"|"!" {return TOK_NON;} <:vspace> ";" {return TOK_FINSTR;} <:vspace> "vrai" {return TOK_VRAI;} <:vspace> "faux" {return TOK_FAUX;} <:vspace> "\n" {lineno++;} <:vspace> {variable_booleenne} { yylval.texte = yytext; return TOK_VARB; } <:vspace> <:vspace> {variable_arithmetique} { yylval.texte = yytext; return TOK_VARE; } <:vspace> {commentaire} { printf("Commentaire detecte en ligne %i\n",lineno); printf("Fin du commentaire en ligne %i\n",lineno); return TOK_COMMENT; } <:vspace> " "|"\t" {} <:vspace> . { fprintf(stderr,"\tERREUR : Lexeme inconnu a la ligne %d. Il s'agit de %s et comporte %d lettre(s)\n",lineno,yytext,yyleng); error_lexical=true; return yytext[0]; } <:vspace> %% (:sourcend:) On ajoute les séquences syntaxiques dans le fichier d'entête : ⚠ (:source lang=c header="simple.h" linestart=56 linenum:) #define MODULO 35 #define AFFECTATION_PLUS 36 #define AFFECTATION_MOINS 37 #define AFFECTATION_MUL 38 #define AFFECTATION_DIV 39 #define AFFECTATION_MOD 40 #define AFFECTATION_ET 41 #define AFFECTATION_OU 42 #define INCREMENTATION 43 #define DECREMENTATION 44 #define AFFECTATION_INCR 45 #define AFFECTATION_DECR 46 (:sourcend:) On met à jour l'analyseur syntaxique et le générateur de code C respectivement : ⚠ (:source lang=c header="syntaxe_simple.y" linenum:) %{ <:vspace> #include "simple.h" bool error_syntaxical=false; bool error_semantical=false; /* Notre table de hachage */ GHashTable* table_variable; <:vspace> /* Fonction de suppression des variables declarees a l'interieur d'un arbre syntaxique */ void supprime_variable(GNode*); <:vspace> /* Notre structure Variable qui a comme membre le type et un pointeur generique vers la valeur */ typedef struct Variable Variable; <:vspace> struct Variable{ char* type; GNode* value; }; <:vspace> %} <:vspace> /* L'union dans Bison est utilisee pour typer nos tokens ainsi que nos non terminaux. Ici nous avons declare une union avec trois types : nombre de type int, texte de type pointeur de char (char*) et noeud d'arbre syntaxique (AST) de type (GNode*) */ <:vspace> %union { long nombre; char* texte; GNode* noeud; } <:vspace> /* Nous avons ici les operateurs, ils sont definis par leur ordre de priorite. Si je definis par exemple la multiplication en premier et l'addition apres, le + l'emportera alors sur le * dans le langage. Les parenthese sont prioritaires avec %right */ <:vspace> %left TOK_INCREMENTATION TOK_DECREMENTATION /* ++ -- */ %left TOK_PLUS TOK_MOINS /* +- */ %left TOK_MUL TOK_DIV TOK_MOD /* /*% */ %left TOK_ET TOK_OU TOK_NON /* et ou non */ %left TOK_EQU TOK_DIFF TOK_SUP TOK_INF TOK_SUPEQU TOK_INFEQU /* comparaisons */ %right TOK_PARG TOK_PARD /* () */ <:vspace> /* Nous avons la liste de nos expressions (les non terminaux). Nous les typons tous en noeud de l'arbre syntaxique (GNode*) */ <:vspace> %type<noeud> code %type<noeud> bloc_code %type<noeud> commentaire %type<noeud> instruction %type<noeud> condition %type<noeud> condition_si %type<noeud> condition_sinon %type<noeud> variable_arithmetique %type<noeud> variable_booleenne %type<noeud> affectation %type<noeud> affichage %type<noeud> expression_arithmetique %type<noeud> expression_booleenne %type<noeud> addition %type<noeud> soustraction %type<noeud> multiplication %type<noeud> division %type<noeud> modulo <:vspace> /* Nous avons la liste de nos tokens (les terminaux de notre grammaire) */ <:vspace> %token<nombre> TOK_NOMBRE %token TOK_VRAI /* true */ %token TOK_FAUX /* false */ %token TOK_AFFECT /* = */ %token TOK_FINSTR /* ; */ %token TOK_IN /* dans */ %token TOK_CROG TOK_CROD /* [] */ %token TOK_AFFICHER /* afficher */ %token<texte> TOK_VARB /* variable booleenne */ %token<texte> TOK_VARE /* variable arithmetique */ %token TOK_SI /* si */ %token TOK_ALORS /* alors */ %token TOK_SINON /* sinon */ %token TOK_COMMENT /* commentaire */ %token TOK_AFFECT_PLUS TOK_AFFECT_MOINS TOK_AFFECT_MUL TOK_AFFECT_DIV TOK_AFFECT_MOD /* += -= *= /= %= */ %token TOK_AFFECT_ET TOK_AFFECT_OU /* &= |= */ %token TOK_POINT_INTERROGATION /* ? */ %token TOK_DOUBLE_POINT /* : */ <:vspace> %% <:vspace> /* Nous definissons toutes les regles grammaticales de chaque non terminal de notre langage. Par defaut on commence a definir l'axiome, c'est a dire ici le non terminal code. Si nous le definissons pas en premier nous devons le specifier en option dans Bison avec %start */ <:vspace> entree: code{ genere_code($1); g_node_destroy($1); }; <:vspace> code: %empty{$$=g_node_new((gpointer)CODE_VIDE);} | code commentaire{ $$=g_node_new((gpointer)SEQUENCE); g_node_append($$,$1); g_node_append($$,$2); } | code instruction{ printf("Resultat : C'est une instruction valide !\n\n"); $$=g_node_new((gpointer)SEQUENCE); g_node_append($$,$1); g_node_append($$,$2); } | code error{ fprintf(stderr,"\tERREUR : Erreur de syntaxe a la ligne %d.\n",lineno); error_syntaxical=true; }; <:vspace> commentaire: TOK_COMMENT{ $$=g_node_new((gpointer)CODE_VIDE); }; <:vspace> bloc_code: code{ $$=g_node_new((gpointer)BLOC_CODE); g_node_append($$,$1); supprime_variable($1); } <:vspace> instruction: affectation{ printf("\tInstruction type Affectation\n"); $$=$1; } | affichage{ printf("\tInstruction type Affichage\n"); $$=$1; } | condition{ printf("Condition si/sinon\n"); $$=$1; } | boucle_for{ printf("Boucle repetee\n"); $$=$1; } | boucle_while{ printf("Boucle tant que\n"); $$=$1; } | boucle_do_while{ printf("Boucle faire tant que\n"); $$=$1; } | suppression{ printf("\tInstruction type Suppression\n"); $$=$1; }; <:vspace> variable_arithmetique: TOK_VARE{ printf("\t\t\tVariable entiere %s\n",$1); $$=g_node_new((gpointer)VARIABLE); g_node_append_data($$,strdup($1)); }; <:vspace> variable_booleenne: TOK_VARB{ printf("\t\t\tVariable booleenne %s\n",$1); $$=g_node_new((gpointer)VARIABLE); g_node_append_data($$,strdup($1)); }; <:vspace> variable_texte: TOK_VART{ printf("\t\t\tVariable texte %s\n",$1); $$=g_node_new((gpointer)VARIABLE); g_node_append_data($$,strdup($1)); }; <:vspace> condition: condition_si TOK_FINSTR{ printf("\tCondition si\n"); $$=g_node_new((gpointer)CONDITION_SI); g_node_append($$,$1); } | condition_si condition_sinon TOK_FINSTR{ printf("\tCondition si/sinon\n"); $$=g_node_new((gpointer)CONDITION_SI_SINON); g_node_append($$,$1); g_node_append($$,$2); } | TOK_PARG expression_booleenne TOK_PARD TOK_POINT_INTERROGATION bloc_code TOK_DOUBLE_POINT bloc_code TOK_FINSTR{ printf("\tCondition si/sinon\n"); $$=g_node_new((gpointer)CONDITION_SI_SINON); g_node_append($$,g_node_new((gpointer)SI)); g_node_append(g_node_nth_child($$,0),$2); g_node_append(g_node_nth_child($$,0),$5); g_node_append($$,g_node_new((gpointer)SINON)); g_node_append(g_node_nth_child($$,1),$7); <:vspace> }; <:vspace> condition_si: TOK_SI expression_booleenne TOK_ALORS bloc_code{ $$=g_node_new((gpointer)SI); g_node_append($$,$2); g_node_append($$,$4); }; <:vspace> condition_sinon: TOK_SINON bloc_code{ $$=g_node_new((gpointer)SINON); g_node_append($$,$2); }; <:vspace> affectation: variable_arithmetique TOK_AFFECT expression_arithmetique TOK_FINSTR{ /* $1 est la valeur du premier non terminal. Ici c'est la valeur du non terminal variable. $3 est la valeur du 2nd non terminal. */ printf("\t\tAffectation sur la variable\n"); Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ /* On cree une Variable et on lui affecte le type que nous connaissons et la valeur */ var=malloc(sizeof(Variable)); if(var!=NULL){ var->type=strdup("entier"); var->value=$3; /* On l'insere dans la table de hachage (cle: <nom_variable> / valeur: <(type,valeur)>) */ if(g_hash_table_insert(table_variable,g_node_nth_child($1,0)->data,var)){ $$=g_node_new((gpointer)AFFECTATIONE); g_node_append($$,$1); g_node_append($$,$3); }else{ fprintf(stderr,"ERREUR - PROBLEME CREATION VARIABLE !\n"); exit(-1); } }else{ fprintf(stderr,"ERREUR - PROBLEME ALLOCATION MEMOIRE VARIABLE !\n"); exit(-1); } }else{ $$=g_node_new((gpointer)AFFECTATION); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_PLUS expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_PLUS); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_MOINS expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_MOINS); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_MUL expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_MUL); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_DIV expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_DIV); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_AFFECT_MOD expression_arithmetique TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_MOD); g_node_append($$,$1); g_node_append($$,$3); } } | variable_arithmetique TOK_INCREMENTATION TOK_FINSTR{ printf("\t\t\tIncrementation de +1 sur la variable\n"); $$=g_node_new((gpointer)AFFECTATION_INCR); g_node_append($$,$1); } | variable_arithmetique TOK_DECREMENTATION TOK_FINSTR{ printf("\t\t\tDecrementation de -1 sur la variable\n"); $$=g_node_new((gpointer)AFFECTATION_DECR); g_node_append($$,$1); } | variable_booleenne TOK_AFFECT expression_booleenne TOK_FINSTR{ /* $1 est la valeur du premier non terminal. Ici c'est la valeur du non terminal variable. $3 est la valeur du 2nd non terminal. */ printf("\t\tAffectation sur la variable\n"); Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ /* On cree une Variable et on lui affecte le type que nous connaissons et la valeur */ var=malloc(sizeof(Variable)); if(var!=NULL){ var->type=strdup("booleen"); var->value=$3; /* On l'insere dans la table de hachage (cle: <nom_variable> / valeur: <(type,valeur)>) */ if(g_hash_table_insert(table_variable,g_node_nth_child($1,0)->data,var)){ $$=g_node_new((gpointer)AFFECTATIONB); g_node_append($$,$1); g_node_append($$,$3); }else{ fprintf(stderr,"ERREUR - PROBLEME CREATION VARIABLE !\n"); exit(-1); } }else{ fprintf(stderr,"ERREUR - PROBLEME ALLOCATION MEMOIRE VARIABLE !\n"); exit(-1); } }else{ $$=g_node_new((gpointer)AFFECTATION); g_node_append($$,$1); g_node_append($$,$3); } } | variable_booleenne TOK_AFFECT_ET expression_booleenne TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_ET); g_node_append($$,$1); g_node_append($$,$3); } } | variable_booleenne TOK_AFFECT_OU expression_booleenne TOK_FINSTR{ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); if(var==NULL){ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; }else{ $$=g_node_new((gpointer)AFFECTATION_OU); g_node_append($$,$1); g_node_append($$,$3); } }; <:vspace> affichage: TOK_AFFICHER expression_arithmetique TOK_FINSTR{ printf("\t\tAffichage de la valeur de l'expression arithmetique\n"); $$=g_node_new((gpointer)AFFICHAGEE); g_node_append($$,$2); } | TOK_AFFICHER expression_booleenne TOK_FINSTR{ printf("\t\tAffichage de la valeur de l'expression booleenne\n"); $$=g_node_new((gpointer)AFFICHAGEB); g_node_append($$,$2); }; <:vspace> <:vspace> expression_arithmetique: TOK_NOMBRE{ printf("\t\t\tNombre : %ld\n",$1); /* Comme le token TOK_NOMBRE est de type entier et que on a type expression_arithmetique comme du texte, il nous faut convertir la valeur en texte. */ int length=snprintf(NULL,0,"%ld",$1); char* str=malloc(length+1); snprintf(str,length+1,"%ld",$1); $$=g_node_new((gpointer)ENTIER); g_node_append_data($$,strdup(str)); free(str); } | variable_arithmetique{ /* On recupere un pointeur vers la structure Variable */ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); /* Si on a trouve un pointeur valable */ if(var!=NULL){ /* On verifie que le type est bien un entier - Inutile car impose a l'analyse syntaxique */ if(strcmp(var->type,"entier")==0){ $$=$1; }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Type incompatible (entier attendu - valeur : %s) !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } /* Sinon on conclue que la variable n'a jamais ete declaree car absente de la table */ }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } } | addition{ $$=$1; } | soustraction{ $$=$1; } | multiplication{ $$=$1; } | division{ $$=$1; } | modulo{ $$=$1; } | TOK_PLUS expression_arithmetique{ $$=$2; } | expression_arithmetique TOK_INCREMENTATION{ printf("\t\t\tIncrementation de +1\n"); $$=g_node_new((gpointer)INCREMENTATION); g_node_append($$,$1); } | expression_arithmetique TOK_DECREMENTATION{ printf("\t\t\tDecrementation de -1\n"); $$=g_node_new((gpointer)DECREMENTATION); g_node_append($$,$1); } | TOK_MOINS expression_arithmetique{ printf("\t\t\tOperation unaire negation\n"); $$=g_node_new((gpointer)NEGATIF); g_node_append($$,$2); } | TOK_PARG expression_arithmetique TOK_PARD{ printf("\t\t\tC'est une expression artihmetique entre parentheses\n"); $$=g_node_new((gpointer)EXPR_PAR); g_node_append($$,$2); }; <:vspace> expression_booleenne: TOK_VRAI{ printf("\t\t\tBooleen Vrai\n"); $$=g_node_new((gpointer)VRAI); } | TOK_FAUX{ printf("\t\t\tBooleen Faux\n"); $$=g_node_new((gpointer)FAUX); } | variable_booleenne{ /* On recupere un pointeur vers la structure Variable */ Variable* var=g_hash_table_lookup(table_variable,(char*)g_node_nth_child($1,0)->data); /* Si on a trouve un pointeur valable */ if(var!=NULL){ /* On verifie que le type est bien un entier - Inutile car impose a l'analyse syntaxique */ if(strcmp(var->type,"booleen")==0){ $$=$1; }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Type incompatible (booleen attendu - valeur : %s) !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } /* Sinon on conclue que la variable n'a jamais ete declaree car absente de la table */ }else{ fprintf(stderr,"\tERREUR : Erreur de semantique a la ligne %d. Variable %s jamais declaree !\n",lineno,(char*)g_node_nth_child($1,0)->data); error_semantical=true; } } | TOK_NON expression_booleenne{ printf("\t\t\tOperation booleenne Non\n"); $$=g_node_new((gpointer)NON); g_node_append($$,$2); } | expression_booleenne TOK_ET expression_booleenne{ printf("\t\t\tOperation booleenne Et\n"); $$=g_node_new((gpointer)ET); g_node_append($$,$1); g_node_append($$,$3); } | expression_booleenne TOK_OU expression_booleenne{ printf("\t\t\tOperation booleenne Ou\n"); $$=g_node_new((gpointer)OU); g_node_append($$,$1); g_node_append($$,$3); } | TOK_PARG expression_booleenne TOK_PARD{ printf("\t\t\tC'est une expression booleenne entre parentheses\n"); $$=g_node_new((gpointer)EXPR_PAR); g_node_append($$,$2); } | expression_booleenne TOK_EQU expression_booleenne{ printf("\t\t\tOperateur d'egalite ==\n"); $$=g_node_new((gpointer)EGALITE); g_node_append($$,$1); g_node_append($$,$3); } | expression_booleenne TOK_DIFF expression_booleenne{ printf("\t\t\tOperateur d'inegalite !=\n"); $$=g_node_new((gpointer)DIFFERENT); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_EQU expression_arithmetique{ printf("\t\t\tOperateur d'egalite ==\n"); $$=g_node_new((gpointer)EGALITE); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_DIFF expression_arithmetique{ printf("\t\t\tOperateur d'inegalite !=\n"); $$=g_node_new((gpointer)DIFFERENT); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_SUP expression_arithmetique{ printf("\t\t\tOperateur de superiorite >\n"); $$=g_node_new((gpointer)SUPERIEUR); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_INF expression_arithmetique{ printf("\t\t\tOperateur d'inferiorite <\n"); $$=g_node_new((gpointer)INFERIEUR); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_SUPEQU expression_arithmetique{ printf("\t\t\tOperateur >=\n"); $$=g_node_new((gpointer)SUPEGAL); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_INFEQU expression_arithmetique{ printf("\t\t\tOperateur <=\n"); $$=g_node_new((gpointer)INFEGAL); g_node_append($$,$1); g_node_append($$,$3); } | expression_arithmetique TOK_IN TOK_CROG expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROD{ printf("\t\t\tOperateur dans\n"); $$=g_node_new((gpointer)DANSII); g_node_append($$,$1); g_node_append($$,$4); g_node_append($$,$6); } | expression_arithmetique TOK_IN TOK_CROD expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROD{ printf("\t\t\tOperateur dans\n"); $$=g_node_new((gpointer)DANSEI); g_node_append($$,$1); g_node_append($$,$4); g_node_append($$,$6); } | expression_arithmetique TOK_IN TOK_CROG expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROG{ printf("\t\t\tOperateur dans\n"); $$=g_node_new((gpointer)DANSIE); g_node_append($$,$1); g_node_append($$,$4); g_node_append($$,$6); } | expression_arithmetique TOK_IN TOK_CROD expression_arithmetique TOK_FINSTR expression_arithmetique TOK_CROG{ printf("\t\t\tOperateur dans\n"); $$=g_node_new((gpointer)DANSEE); g_node_append($$,$1); g_node_append($$,$4); g_node_append($$,$6); }; <:vspace> addition: expression_arithmetique TOK_PLUS expression_arithmetique{ printf("\t\t\tAddition\n"); $$=g_node_new((gpointer)ADDITION); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> soustraction: expression_arithmetique TOK_MOINS expression_arithmetique{ printf("\t\t\tSoustraction\n"); $$=g_node_new((gpointer)SOUSTRACTION); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> multiplication: expression_arithmetique TOK_MUL expression_arithmetique{ printf("\t\t\tMultiplication\n"); $$=g_node_new((gpointer)MULTIPLICATION); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> division: expression_arithmetique TOK_DIV expression_arithmetique{ printf("\t\t\tDivision\n"); $$=g_node_new((gpointer)DIVISION); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> modulo: expression_arithmetique TOK_MOD expression_arithmetique{ printf("\t\t\tModulo\n"); $$=g_node_new((gpointer)MODULO); g_node_append($$,$1); g_node_append($$,$3); }; <:vspace> %% <:vspace> /* Dans la fonction main on appelle bien la routine yyparse() qui sera genere par Bison. Cette routine appellera yylex() de notre analyseur lexical. */ <:vspace> int main(int argc, char** argv){ /* recuperation du nom de fichier d'entree (langage Simple) donne en parametre */ char* fichier_entree=strdup(argv[1]); /* ouverture du fichier en lecture dans le flux d'entree stdin */ stdin=fopen(fichier_entree,"r"); /* creation fichier de sortie (langage C) */ char* fichier_sortie=strdup(argv[1]); /* remplace l'extension par .c */ strcpy(rindex(fichier_sortie, '.'), ".c"); /* ouvre le fichier cree en ecriture */ fichier=fopen(fichier_sortie, "w"); /* Creation de la table de hachage */ table_variable=g_hash_table_new_full(g_str_hash,g_str_equal,free,free); printf("Debut de l'analyse syntaxique :\n"); debut_code(); yyparse(); fin_code(); printf("Fin de l'analyse !\n"); printf("Resultat :\n"); if(error_lexical){ printf("\t-- Echec : Certains lexemes ne font pas partie du lexique du langage ! --\n"); printf("\t-- Echec a l'analyse lexicale --\n"); } else{ printf("\t-- Succes a l'analyse lexicale ! --\n"); } if(error_syntaxical){ printf("\t-- Echec : Certaines phrases sont syntaxiquement incorrectes ! --\n"); printf("\t-- Echec a l'analyse syntaxique --\n"); } else{ printf("\t-- Succes a l'analyse syntaxique ! --\n"); if(error_semantical){ printf("\t-- Echec : Certaines phrases sont semantiquement incorrectes ! --\n"); printf("\t-- Echec a l'analyse semantique --\n"); } else{ printf("\t-- Succes a l'analyse semantique ! --\n"); } } /* Suppression du fichier genere si erreurs analyse */ if(error_lexical||error_syntaxical||error_semantical){ remove(fichier_sortie); printf("ECHEC GENERATION CODE !\n"); } else{ printf("Le fichier \"%s\" a ete genere !\n",fichier_sortie); } /* Fermeture des flux */ fclose(fichier); fclose(stdin); /* Liberation memoire */ free(fichier_entree); free(fichier_sortie); g_hash_table_destroy(table_variable); return EXIT_SUCCESS; } <:vspace> void yyerror(char *s) { fprintf(stderr, "Erreur de syntaxe a la ligne %d: %s\n", lineno, s); } <:vspace> /* Cette fonction supprime dans la table de hachage toutes les variables declarees pour la premiere fois dans l'arbre syntaxique donne en parametre */ <:vspace> void supprime_variable(GNode* ast){ /* si l'element est n'est pas NULL et que ce n'est pas une feuille */ if(ast&&!G_NODE_IS_LEAF(ast)){ /* si le noeud est de type declaration */ if((long)ast->data==AFFECTATIONB||(long)ast->data==AFFECTATIONE){ /* suppression de la variable dans la table de hachage */ if(g_hash_table_remove(table_variable,(char*)g_node_nth_child(g_node_nth_child(ast,0),0)->data)){ printf("Variable supprimee !\n"); }else{ fprintf(stderr,"ERREUR - PROBLEME DE SUPPRESSION VARIABLE !\n"); exit(-1); } /* sinon on continue de parcourir l'arbre */ }else{ int nb_enfant; for(nb_enfant=0;nb_enfant<=g_node_n_children(ast);nb_enfant++){ supprime_variable(g_node_nth_child(ast,nb_enfant)); } } } } (:sourcend:) ⚠ (:source lang=c header="generation_code.c" linenum:) #include "simple.h" <:vspace> void debut_code(){ fprintf(fichier, "/* FICHIER GENERE PAR LE COMPILATEUR SIMPLE */\n\n"); fprintf(fichier, "#include<stdlib.h>\n#include<stdbool.h>\n#include<stdio.h>\n\n"); fprintf(fichier, "int main(void){\n"); } <:vspace> void fin_code(){ fprintf(fichier, "\treturn EXIT_SUCCESS;\n"); fprintf(fichier, "}\n"); } <:vspace> void genere_code(GNode* ast){ if(ast){ switch((long)ast->data){ case SEQUENCE: genere_code(g_node_nth_child(ast,0)); genere_code(g_node_nth_child(ast,1)); break; case VARIABLE: fprintf(fichier,"%s",(char*)g_node_nth_child(ast,0)->data); break; case AFFECTATIONE: fprintf(fichier,"\tlong "); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATIONB: fprintf(fichier,"\tbool "); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_PLUS: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"+="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_MOINS: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"-="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_MUL: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"*="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_DIV: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"/="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_MOD: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"%%="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_ET: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"&="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_OU: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"|="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,";\n"); break; case AFFECTATION_INCR: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"++;\n"); break; case AFFECTATION_DECR: fprintf(fichier,"\t"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"--;\n"); break; case AFFICHAGEE: fprintf(fichier,"\tprintf(\"%%ld\\n\","); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,");\n"); break; case AFFICHAGEB: fprintf(fichier,"\tprintf(\"%%s\\n\","); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"?\"vrai\":\"faux\");\n"); break; case ENTIER: fprintf(fichier,"%s",(char*)g_node_nth_child(ast,0)->data); break; case ADDITION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"+"); genere_code(g_node_nth_child(ast,1)); break; case SOUSTRACTION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"-"); genere_code(g_node_nth_child(ast,1)); break; case MULTIPLICATION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"*"); genere_code(g_node_nth_child(ast,1)); break; case DIVISION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"/"); genere_code(g_node_nth_child(ast,1)); break; case MODULO: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"%%"); genere_code(g_node_nth_child(ast,1)); break; case INCREMENTATION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"+1"); break; case DECREMENTATION: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"-1"); break; case VRAI: fprintf(fichier,"true"); break; case FAUX: fprintf(fichier,"false"); break; case ET: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,1)); break; case OU: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"||"); genere_code(g_node_nth_child(ast,1)); break; case NON: fprintf(fichier,"!"); genere_code(g_node_nth_child(ast,0)); break; case EXPR_PAR: fprintf(fichier,"("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,")"); break; case EGALITE: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"=="); genere_code(g_node_nth_child(ast,1)); break; case DIFFERENT: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"!="); genere_code(g_node_nth_child(ast,1)); break; case INFERIEUR: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<"); genere_code(g_node_nth_child(ast,1)); break; case SUPERIEUR: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">"); genere_code(g_node_nth_child(ast,1)); break; case INFEGAL: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<="); genere_code(g_node_nth_child(ast,1)); break; case SUPEGAL: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">="); genere_code(g_node_nth_child(ast,1)); break; case DANSII: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<="); genere_code(g_node_nth_child(ast,2)); break; case DANSEI: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">"); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<="); genere_code(g_node_nth_child(ast,2)); break; case DANSIE: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">="); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<"); genere_code(g_node_nth_child(ast,2)); break; case DANSEE: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,">"); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"&&"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"<"); genere_code(g_node_nth_child(ast,2)); break; case NEGATIF: fprintf(fichier,"-"); genere_code(g_node_nth_child(ast,0)); break; case CONDITION_SI: genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"\n"); break; case CONDITION_SI_SINON: genere_code(g_node_nth_child(ast,0)); genere_code(g_node_nth_child(ast,1)); break; case SI: fprintf(fichier,"\tif("); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"){\n"); genere_code(g_node_nth_child(ast,1)); fprintf(fichier,"\t}"); break; case SINON: fprintf(fichier,"else{\n"); genere_code(g_node_nth_child(ast,0)); fprintf(fichier,"\t}\n"); break; case BLOC_CODE: genere_code(g_node_nth_child(ast,0)); break; } } } (:sourcend:) On compile le compilateur et on lui passe en entrée ce fichier test : ⚠ (:source lang=text header="programme.simple" linenum:) entier=5; booleen=vrai; <:vspace> // Test de l'operateur Modulo % <:vspace> afficher entier%2; #affiche 1 <:vspace> // Test de l'incrementation <:vspace> entier++; afficher entier; #affiche 6 <:vspace> // Test de la decrementation <:vspace> entier--; afficher entier; #affiche 5 <:vspace> /* TEST DES AFFECTATIONS */ <:vspace> // Expressions booleennes <:vspace> booleen&=faux; afficher booleen; #affiche faux <:vspace> booleen|=vrai; afficher booleen; #affiche vrai <:vspace> // Expressions arithmetiques <:vspace> entier+=5; afficher entier; #affiche 10 entier-=5; afficher entier; #affiche 5 entier*=20; afficher entier; #affiche 100 entier/=2; afficher entier; #affiche 50 entier%=45; afficher entier; #affiche 5 <:vspace> // Test du If/Else reduit (entier==5)? afficher entier++; #affiche 6 : afficher entier--; ; <:vspace> 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) (:sourcend:) On compile le fichier C fraîchement généré par notre compilateur et on obtient sans surprise ceci : ⚠ (:source lang=text:) 1 6 5 faux vrai 10 5 100 50 5 6 5 (:sourcend:) 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)

Edit - History - Print - Recent Changes - Search
Page last modified on July 09, 2017, at 07:51 PM EST