diff --git a/src/analyseur_lexical.c b/src/analyseur_lexical.c index 2812771..931613a 100644 --- a/src/analyseur_lexical.c +++ b/src/analyseur_lexical.c @@ -210,6 +210,16 @@ void nom_token( int token, char *nom, char *valeur ) { else if(token == FIN) strcpy(valeur, "FIN"); else if(token == VIRGULE) strcpy(valeur, "VIRGULE"); + else if(token == SI) strcpy(valeur, "SI"); + else if(token == ALORS) strcpy(valeur, "ALORS"); + else if(token == SINON) strcpy(valeur, "SINON"); + else if(token == TANTQUE) strcpy(valeur, "TANTQUE"); + else if(token == FAIRE) strcpy(valeur, "FAIRE"); + else if(token == ENTIER) strcpy(valeur, "ENTIER"); + else if(token == RETOUR) strcpy(valeur, "RETOUR"); + else if(token == LIRE) strcpy(valeur, "LIRE"); + else if(token == ECRIRE) strcpy(valeur, "ECRIRE"); + else if( token == ID_VAR ) { strcpy( nom, "id_variable" ); strcpy( valeur, yytext ); diff --git a/src/analyseur_syntaxyque.c b/src/analyseur_syntaxyque.c index 96b7c87..3ebb8f9 100644 --- a/src/analyseur_syntaxyque.c +++ b/src/analyseur_syntaxyque.c @@ -3,202 +3,721 @@ int uniteCourante; extern char yytext[100]; -void ou (void){ - affiche_balise_ouvrante("ou", 1); +void openSection( const char * section ) +{ + affiche_balise_ouvrantexpression( section, 1 ); +} + +void closeSection( const char * section ) +{ + affiche_balise_fermantexpression( section, 1 ); +} + +void programme() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _optDecVariables_, uniteCourante ) ) { + optDecVariables(); + listeDecFonctions(); + } else { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void optDecVariables() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _listeDecVariables_, uniteCourante ) ) + { + listeDecVariables(); + + if( uniteCourante == POINT_VIRGULE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "';' été attendu" ); + } + } else if ( !est_suivanterme( _optDecVariables_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void listeDecVariables() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _declarationVariable_, uniteCourante ) ) { + declarationVariablisteExpressions(); + listeDecVariablesBis(); + } else { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void listeDecVariablesBis() +{ + openSection( __FUNCTION__ ); - et(); - ouBis(); - - affiche_balise_fermante("ou", 1); -} - -void ouBis(void){ - affiche_balise_ouvrante("ouBis", 1); - - if(uniteCourante == OU){ + if( uniteCourante == VIRGULE ) { uniteCourante = yylex(); - affiche_xml_texte(yytext); - ou(); + + declarationVariablisteExpressions(); + listeDecVariablesBis(); + } else if( !est_suivanterme( _listeDecVariablesBis_, uniteCourante ) ) { + syntaxError(); } - affiche_balise_fermante("ouBis", 1); + closeSection( __FUNCTION__ ); } -void et(void){ - affiche_balise_ouvrante("et", 1); +void declarationVariablisteExpressions() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == ENTIER ) { + uniteCourante = yylex(); + + if( uniteCourante == ID_VAR ) { + uniteCourante = yylex(); + + optTailleTableau(); + } else { + syntaxErrorMsg( "Un identificateur de variable été attendu" ); + } + } else { + syntaxErrorMsg( "'ENTIER' été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +void optTailleTableau() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == CROCHET_OUVRANT ) { + uniteCourante = yylex(); + + if( uniteCourante == NOMBRE ) { + uniteCourante = yylex(); + + if( uniteCourante == CROCHET_FERMANT ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "']' été attendu" ); + } + } else { + syntaxErrorMsg( "Un nombre été attendu" ); + } + } else if( !est_suivanterme( _optTailleTableau_, uniteCourante ) ) + { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void listeDecFonctions() +{ + /// LDF -> DF LDF | Ø + openSection( __FUNCTION__ ); + + if( est_premier( _declarationFonction_, uniteCourante ) ) { + declarationFonction(); + listeDecFonctions(); + } else if( !est_suivanterme( _listeDecFonctionss_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void declarationFonction() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == ID_FCT ) { + uniteCourante = yylex(); + + listeParam(); + optDecVariables(); + instructionBloc(); + } else { + syntaxErrorMsg( "Un identificateur de fonction été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +void listeParam() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == PARENTHESE_OUVRANTE ) { + uniteCourante = yylex(); + + optListeDecVariables(); + + if( uniteCourante == PARENTHESE_FERMANTE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "')' été attendu" ); + } + } else { + syntaxErrorMsg( "'(' été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +void optListeDecVariables() +{ + openSection( __FUNCTION__ ); - non(); - etBis(); - - affiche_balise_fermante("et", 1); -} - -void etBis(void){ - affiche_balise_ouvrante("etBis", 1); - - if(uniteCourante == ET){ - uniteCourante = yylex(); - affiche_xml_texte(yytext); - et(); + if( est_premier( _listeDecVariables_, uniteCourante ) ) { + listeDecVariables(); + } else if( !est_suivanterme( _optListeDecVariables_, uniteCourante ) ) { + syntaxError(); } - - affiche_balise_fermante("etBis", 1); -} - -void non(void){ - affiche_balise_ouvrante("non", 1); - nonBis(); - egal(); - - affiche_balise_fermante("non", 1); -} - -void nonBis(void){ - affiche_balise_ouvrante("nonBis", 1); - if(uniteCourante == NON){ - uniteCourante = yylex(); - affiche_xml_texte(yytext); - nonBis(); - } - - affiche_balise_fermante("nonBis", 1); -} - -void egal(void){ - affiche_balise_ouvrante("egal", 1); - expArith(); - egalBis(); - - affiche_balise_fermante("egal", 1); + closeSection( __FUNCTION__ ); } -void egalBis(void){ - affiche_balise_ouvrante("egalBis", 1); +void instruction() +{ + openSection( __FUNCTION__ ); - if(uniteCourante == EGAL || uniteCourante == INFERIEUR){ - uniteCourante = yylex(); - affiche_xml_texte(yytext); - egal(); + if( est_premier( _instructionAffect_, uniteCourante ) ) { + instructionAffecterme(); + } else if( est_premier( _instructionBloc_, uniteCourante ) ) { + instructionBloc(); + } else if( est_premier( _instructionSi_, uniteCourante ) ) { + instructionSi(); + } else if( est_premier( _instructionTantque_, uniteCourante ) ) { + instructionTantquexpression(); + } else if( est_premier( _instructionAppel_, uniteCourante ) ) { + instructionAppel(); + } else if( est_premier( _instructionRetour_, uniteCourante ) ) { + instructionRetour(); + } else if( est_premier( _instructionEcriture_, uniteCourante ) ) { + instructionEcriturexpression(); + } else if( est_premier( _instructionVide_, uniteCourante ) ) { + instructionVidexpression(); + } else { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void instructionAffecterme() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _var_, uniteCourante ) ) { + var(); + + if( uniteCourante == EGAL ) { + uniteCourante = yylex(); + + expression(); + + if( uniteCourante == POINT_VIRGULE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "';' été attendu" ); + } + } else { + syntaxErrorMsg( "'=' été attendu" ); + } + } else { + syntaxError(); } - affiche_balise_fermante("egalBis", 1); + closeSection( __FUNCTION__ ); } -void expArith (void) { - affiche_balise_ouvrante("expArith", 1); +void instructionBloc() +{ + openSection( __FUNCTION__ ); - terme(); - expArithBis(); - - affiche_balise_fermante("expArith", 1); -} - -void expArithBis (void) { - affiche_balise_ouvrante("expArithBis", 1); - - if(uniteCourante == PLUS || uniteCourante == MOINS ) { + if( uniteCourante == ACCOLADE_OUVRANTE ) { uniteCourante = yylex(); - affiche_xml_texte(yytext); - expArith(); + listeInstructions(); + + if( uniteCourante == ACCOLADE_FERMANTE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "'}' été attendu" ); + } + } else { + syntaxErrorMsg( "'{' été attendu" ); } - affiche_balise_fermante("expArithBis", 1); + closeSection( __FUNCTION__ ); } -void terme (void) { - affiche_balise_ouvrante("terme", 1); +void listeInstructions() +{ + openSection( __FUNCTION__ ); - facteur(); - termeBis(); + if( est_premier( _instruction_, uniteCourante ) ) { + instruction(); + listeInstructions(); + } else if( !est_suivanterme( _listeInstructions_, uniteCourante ) ) { + syntaxError(); + } - affiche_balise_fermante("terme", 1); + closeSection( __FUNCTION__ ); } -void termeBis (void) { - affiche_balise_ouvrante("termeBis", 1); +void instructionSi() +{ + openSection( __FUNCTION__ ); - if(uniteCourante == FOIS || uniteCourante == DIVISE) { + if( uniteCourante == SI ) { + uniteCourante = yylex(); + + expression(); + + if( uniteCourante == ALORS ) { + uniteCourante = yylex(); + + instructionBloc(); + optSinon(); + } else { + syntaxErrorMsg( "'alors' été attendu" ); + } + } else { + syntaxErrorMsg( "'si' été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +// ICI + +void optSinon() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == SINON ) { + uniteCourante = yylex(); + + instructionBloc(); + } else if( !est_suivanterme( _optSinon_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void instructionTantquexpression() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == TANTQUE ) { + uniteCourante = yylex(); + + expression(); + + if( uniteCourante == FAIRE ) { + uniteCourante = yylex(); + + instructionBloc(); + } else { + syntaxErrorMsg( "'faire' été attendu" ); + } + } else { + syntaxErrorMsg( "'tantque' été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +void instructionAppel() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _instructionAppel_, uniteCourante ) ) { + appelFct(); + + if( uniteCourante == POINT_VIRGULE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "';' été attendu" ); + } + } else { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void instructionRetour() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == RETOUR ) { + uniteCourante = yylex(); + + expression(); + + if( uniteCourante == POINT_VIRGULE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "';' été attendu" ); + } + } else { + syntaxErrorMsg( "'retour' été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +void instructionEcriturexpression() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == ECRIRE ) { + uniteCourante = yylex(); + + if( uniteCourante == PARENTHESE_OUVRANTE ) { + uniteCourante = yylex(); + + expression(); + + if( uniteCourante == PARENTHESE_FERMANTE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "')' été attendu" ); + } + } else { + syntaxErrorMsg( "'(' été attendu" ); + } + } else { + syntaxErrorMsg( "'ecrire' été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +void instructionVidexpression() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == POINT_VIRGULE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "';' été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +void expression() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _conjonction_, uniteCourante ) ) { + conjonction(); + expressionBis(); + } else { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void expressionBis() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == OU ) { + uniteCourante = yylex(); + + conjonction(); + expressionBis(); + } else if( !est_suivanterme( _expressionBis_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void conjonction() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _negation_, uniteCourante ) ) { + negation(); + conjonctionBis(); + } else { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void conjonctionBis() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == ET ) { + uniteCourante = yylex(); + + negation(); + conjonctionBis(); + } else if( !est_suivanterme( _conjonctionBis_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void negation() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == NON ) { + uniteCourante = yylex(); + + comparaison(); + } else { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void comparaison() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _expression_, uniteCourante ) ) { + expression(); + comparaisonBis(); + } else { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void comparaisonBis() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == EGAL || uniteCourante == INFERIEUR) { + uniteCourante = yylex(); + + expression(); + comparaisonBis(); + } else if( !est_suivanterme( _comparaisonBis_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void expression() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _terme_, uniteCourante ) ) { + terme(); + expArithBis(); + } else { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void expArithBis() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == PLUS || uniteCourante == MOINS) { uniteCourante = yylex(); - affiche_xml_texte(yytext); terme(); + expArithBis(); + } else if( !est_suivanterme( _expArithBis_, uniteCourante ) ) { + syntaxError(); } - affiche_balise_fermante("termeBis", 1); + closeSection( __FUNCTION__ ); } -void facteur (void) { - affiche_balise_ouvrante("facteur", 1); - - if(uniteCourante == PARENTHESE_OUVRANTE) { - uniteCourante = yylex(); - affiche_xml_texte(yytext); - - ou(); - - if(uniteCourante != PARENTHESE_FERMANTE) { - printf("Erreur de syntaxe"); - exit(-1); - } - uniteCourante = yylex(); - affiche_xml_texte(yytext); - - } else if(uniteCourante == NOMBRE) { - uniteCourante = yylex(); - affiche_xml_texte(yytext); - } else if(uniteCourante == ID_VAR) { - uniteCourante = yylex(); - affiche_xml_texte(yytext); - } else if(uniteCourante == CROCHET_OUVRANT){ - uniteCourante = yylex(); - affiche_xml_texte(yytext); - - ou(); - if(uniteCourante != CROCHET_FERMANT) { - printf("Erreur de syntaxe"); - exit(-1); - } - uniteCourante = yylex(); - affiche_xml_texte(yytext); +void terme() +{ + openSection( __FUNCTION__ ); + if( est_premier( _facteur_, uniteCourante ) ) { + facteur(); + termeBis(); + } else { + syntaxError(); } - else if(uniteCourante == ID_FCT){ + + closeSection( __FUNCTION__ ); +} + +void termeBis() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == FOIS || ylval == DIVISE ) { uniteCourante = yylex(); - affiche_xml_texte(yytext); - if(uniteCourante != PARENTHESE_OUVRANTE){ - printf("Erreur de syntaxe"); - exit(-1); + + facteur(); + termeBis(); + } else if( !est_suivanterme( _termeBis_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void facteur() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == PARENTHESE_OUVRANTE ) { + uniteCourante = yylex(); + + expression(); + + if( uniteCourante == PARENTHESE_FERMANTE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "')' été attendu" ); } + } else if( uniteCourante == NOMBRE ) { uniteCourante = yylex(); - affiche_xml_texte(yytext); - if(uniteCourante != PARENTHESE_FERMANTE){ - ou(); - while(uniteCourante == VIRGULE){ + } else if( est_premier( _appelFct_, uniteCourante ) ) { + appelFct(); + } else if( est_premier( _var_, uniteCourante ) ) { + var(); + } else if( uniteCourante == LIRE ) { + uniteCourante = yylex(); + + if( uniteCourante == PARENTHESE_OUVRANTE ) { + uniteCourante = yylex(); + + if( uniteCourante == PARENTHESE_FERMANTE ) { uniteCourante = yylex(); - affiche_xml_texte(yytext); - ou(); - + } else { + syntaxErrorMsg( "')' été attendu" ); } + } else { + syntaxErrorMsg( "'(' été attendu" ); } - if(uniteCourante != PARENTHESE_FERMANTE){ - printf("Erreur de syntaxe"); - exit(-1); - } - uniteCourante = yylex(); - affiche_xml_texte(yytext); - if(uniteCourante != POINT_VIRGULE){ - printf("Erreur de syntaxe"); - exit(-1); - } - uniteCourante = yylex(); - affiche_xml_texte(yytext); - } - else { - printf("Erreur de syntaxe"); - exit(-1); + } else { + syntaxError(); } - affiche_balise_fermante("facteur", 1); + closeSection( __FUNCTION__ ); +} + +void var() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == ID_VAR ) { + uniteCourante = yylex(); + + optIndice(); + } else { + syntaxErrorMsg( "Indice de variable été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +void optIndice() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == CROCHET_OUVRANT ) { + uniteCourante = yylex(); + + expression(); + + if( uniteCourante == CROCHET_FERMANT ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "']' été attendu" ); + } + } else if( !est_suivanterme( _optIndice_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void appelFct() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == ID_FCT ) { + uniteCourante = yylex(); + + if( uniteCourante == PARENTHESE_OUVRANTE ) { + uniteCourante = yylex(); + + listeExpressions(); + + if( uniteCourante == PARENTHESE_FERMANTE ) { + uniteCourante = yylex(); + } else { + syntaxErrorMsg( "')' été attendu" ); + } + } else { + syntaxErrorMsg( "'(' été attendu" ); + } + } else { + syntaxErrorMsg( "Identificateur de fonction été attendu" ); + } + + closeSection( __FUNCTION__ ); +} + +void listeExpressions() +{ + openSection( __FUNCTION__ ); + + if( est_premier( _expression_, uniteCourante ) ) { + expression(); + listeExpressionsBis((); + } else if( !est_suivanterme( _listeexpressions_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); +} + +void listeExpressionsBis(() +{ + openSection( __FUNCTION__ ); + + if( uniteCourante == VIRGULE ) { + uniteCourante = yylex(); + + expression(); + listeExpressionsBis((); + } else if( !est_suivanterme( _listeexpressionsBis_, uniteCourante ) ) { + syntaxError(); + } + + closeSection( __FUNCTION__ ); } \ No newline at end of file diff --git a/src/compilateur-l.c b/src/compilateur-l.c index db3672c..b0c9159 100644 --- a/src/compilateur-l.c +++ b/src/compilateur-l.c @@ -19,9 +19,8 @@ int main(int argc, char **argv) { // test_yylex_internal(yyin); uniteCourante = yylex(); - affiche_xml_texte(yytext); - ou(); + programme(); uniteCourante = yylex();