Update analyseur syntaxyque

This commit is contained in:
2016-02-02 17:17:38 +01:00
parent 3f15e27954
commit 91a7241cb4
3 changed files with 683 additions and 155 deletions

View File

@@ -210,6 +210,16 @@ void nom_token( int token, char *nom, char *valeur ) {
else if(token == FIN) strcpy(valeur, "FIN"); else if(token == FIN) strcpy(valeur, "FIN");
else if(token == VIRGULE) strcpy(valeur, "VIRGULE"); 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 ) { else if( token == ID_VAR ) {
strcpy( nom, "id_variable" ); strcpy( nom, "id_variable" );
strcpy( valeur, yytext ); strcpy( valeur, yytext );

View File

@@ -3,202 +3,721 @@
int uniteCourante; int uniteCourante;
extern char yytext[100]; extern char yytext[100];
void ou (void){ void openSection( const char * section )
affiche_balise_ouvrante("ou", 1); {
affiche_balise_ouvrantexpression( section, 1 );
et();
ouBis();
affiche_balise_fermante("ou", 1);
} }
void ouBis(void){ void closeSection( const char * section )
affiche_balise_ouvrante("ouBis", 1); {
affiche_balise_fermantexpression( section, 1 );
}
if(uniteCourante == OU){ 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(); uniteCourante = yylex();
affiche_xml_texte(yytext); } else {
ou(); syntaxErrorMsg( "';' été attendu" );
}
} else if ( !est_suivanterme( _optDecVariables_, uniteCourante ) ) {
syntaxError();
} }
affiche_balise_fermante("ouBis", 1); closeSection( __FUNCTION__ );
} }
void et(void){ void listeDecVariables()
affiche_balise_ouvrante("et", 1); {
openSection( __FUNCTION__ );
non(); if( est_premier( _declarationVariable_, uniteCourante ) ) {
etBis(); declarationVariablisteExpressions();
listeDecVariablesBis();
affiche_balise_fermante("et", 1); } else {
syntaxError();
} }
void etBis(void){ closeSection( __FUNCTION__ );
affiche_balise_ouvrante("etBis", 1); }
if(uniteCourante == ET){ void listeDecVariablesBis()
{
openSection( __FUNCTION__ );
if( uniteCourante == VIRGULE ) {
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext);
et(); declarationVariablisteExpressions();
listeDecVariablesBis();
} else if( !est_suivanterme( _listeDecVariablesBis_, uniteCourante ) ) {
syntaxError();
} }
affiche_balise_fermante("etBis", 1); closeSection( __FUNCTION__ );
} }
void non(void){ void declarationVariablisteExpressions()
affiche_balise_ouvrante("non", 1); {
nonBis(); openSection( __FUNCTION__ );
egal();
affiche_balise_fermante("non", 1); if( uniteCourante == ENTIER ) {
}
void nonBis(void){
affiche_balise_ouvrante("nonBis", 1);
if(uniteCourante == NON){
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext);
nonBis();
}
affiche_balise_fermante("nonBis", 1); if( uniteCourante == ID_VAR ) {
}
void egal(void){
affiche_balise_ouvrante("egal", 1);
expArith();
egalBis();
affiche_balise_fermante("egal", 1);
}
void egalBis(void){
affiche_balise_ouvrante("egalBis", 1);
if(uniteCourante == EGAL || uniteCourante == INFERIEUR){
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext);
egal(); optTailleTableau();
} else {
syntaxErrorMsg( "Un identificateur de variable été attendu" );
}
} else {
syntaxErrorMsg( "'ENTIER' été attendu" );
} }
affiche_balise_fermante("egalBis", 1); closeSection( __FUNCTION__ );
} }
void expArith (void) { void optTailleTableau()
affiche_balise_ouvrante("expArith", 1); {
openSection( __FUNCTION__ );
terme(); if( uniteCourante == CROCHET_OUVRANT ) {
expArithBis();
affiche_balise_fermante("expArith", 1);
}
void expArithBis (void) {
affiche_balise_ouvrante("expArithBis", 1);
if(uniteCourante == PLUS || uniteCourante == MOINS ) {
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext);
expArith(); if( uniteCourante == NOMBRE ) {
}
affiche_balise_fermante("expArithBis", 1);
}
void terme (void) {
affiche_balise_ouvrante("terme", 1);
facteur();
termeBis();
affiche_balise_fermante("terme", 1);
}
void termeBis (void) {
affiche_balise_ouvrante("termeBis", 1);
if(uniteCourante == FOIS || uniteCourante == DIVISE) {
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext);
terme(); if( uniteCourante == CROCHET_FERMANT ) {
uniteCourante = yylex();
} else {
syntaxErrorMsg( "']' été attendu" );
}
} else {
syntaxErrorMsg( "Un nombre été attendu" );
}
} else if( !est_suivanterme( _optTailleTableau_, uniteCourante ) )
{
syntaxError();
} }
affiche_balise_fermante("termeBis", 1); closeSection( __FUNCTION__ );
} }
void facteur (void) { void listeDecFonctions()
affiche_balise_ouvrante("facteur", 1); {
/// 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 ) { if( uniteCourante == PARENTHESE_OUVRANTE ) {
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext);
ou(); optListeDecVariables();
if(uniteCourante != PARENTHESE_FERMANTE) { if( uniteCourante == PARENTHESE_FERMANTE ) {
printf("Erreur de syntaxe");
exit(-1);
}
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext); } else {
syntaxErrorMsg( "')' été attendu" );
}
} else {
syntaxErrorMsg( "'(' été attendu" );
}
closeSection( __FUNCTION__ );
}
void optListeDecVariables()
{
openSection( __FUNCTION__ );
if( est_premier( _listeDecVariables_, uniteCourante ) ) {
listeDecVariables();
} else if( !est_suivanterme( _optListeDecVariables_, uniteCourante ) ) {
syntaxError();
}
closeSection( __FUNCTION__ );
}
void instruction()
{
openSection( __FUNCTION__ );
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();
}
closeSection( __FUNCTION__ );
}
void instructionBloc()
{
openSection( __FUNCTION__ );
if( uniteCourante == ACCOLADE_OUVRANTE ) {
uniteCourante = yylex();
listeInstructions();
if( uniteCourante == ACCOLADE_FERMANTE ) {
uniteCourante = yylex();
} else {
syntaxErrorMsg( "'}' été attendu" );
}
} else {
syntaxErrorMsg( "'{' été attendu" );
}
closeSection( __FUNCTION__ );
}
void listeInstructions()
{
openSection( __FUNCTION__ );
if( est_premier( _instruction_, uniteCourante ) ) {
instruction();
listeInstructions();
} else if( !est_suivanterme( _listeInstructions_, uniteCourante ) ) {
syntaxError();
}
closeSection( __FUNCTION__ );
}
void instructionSi()
{
openSection( __FUNCTION__ );
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();
terme();
expArithBis();
} else if( !est_suivanterme( _expArithBis_, uniteCourante ) ) {
syntaxError();
}
closeSection( __FUNCTION__ );
}
void terme()
{
openSection( __FUNCTION__ );
if( est_premier( _facteur_, uniteCourante ) ) {
facteur();
termeBis();
} else {
syntaxError();
}
closeSection( __FUNCTION__ );
}
void termeBis()
{
openSection( __FUNCTION__ );
if( uniteCourante == FOIS || ylval == DIVISE ) {
uniteCourante = yylex();
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 ) { } else if( uniteCourante == NOMBRE ) {
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext); } else if( est_premier( _appelFct_, uniteCourante ) ) {
} else if(uniteCourante == ID_VAR) { appelFct();
} else if( est_premier( _var_, uniteCourante ) ) {
var();
} else if( uniteCourante == LIRE ) {
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext);
} else if(uniteCourante == CROCHET_OUVRANT){
uniteCourante = yylex();
affiche_xml_texte(yytext);
ou(); if( uniteCourante == PARENTHESE_OUVRANTE ) {
if(uniteCourante != CROCHET_FERMANT) {
printf("Erreur de syntaxe");
exit(-1);
}
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext);
} if( uniteCourante == PARENTHESE_FERMANTE ) {
else if(uniteCourante == ID_FCT){
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext); } else {
if(uniteCourante != PARENTHESE_OUVRANTE){ syntaxErrorMsg( "')' été attendu" );
printf("Erreur de syntaxe");
exit(-1);
} }
uniteCourante = yylex(); } else {
affiche_xml_texte(yytext); syntaxErrorMsg( "'(' été attendu" );
if(uniteCourante != PARENTHESE_FERMANTE){
ou();
while(uniteCourante == VIRGULE){
uniteCourante = yylex();
affiche_xml_texte(yytext);
ou();
} }
} } else {
if(uniteCourante != PARENTHESE_FERMANTE){ syntaxError();
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);
} }
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__ );
} }

View File

@@ -19,9 +19,8 @@ int main(int argc, char **argv) {
// test_yylex_internal(yyin); // test_yylex_internal(yyin);
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext);
ou(); programme();
uniteCourante = yylex(); uniteCourante = yylex();