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 );
}
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(); if( uniteCourante == VIRGULE ) {
ouBis();
affiche_balise_fermante("ou", 1);
}
void ouBis(void){
affiche_balise_ouvrante("ouBis", 1);
if(uniteCourante == OU){
uniteCourante = yylex(); 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){ void declarationVariablisteExpressions()
affiche_balise_ouvrante("et", 1); {
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(); if( est_premier( _listeDecVariables_, uniteCourante ) ) {
etBis(); listeDecVariables();
} else if( !est_suivanterme( _optListeDecVariables_, uniteCourante ) ) {
affiche_balise_fermante("et", 1); syntaxError();
}
void etBis(void){
affiche_balise_ouvrante("etBis", 1);
if(uniteCourante == ET){
uniteCourante = yylex();
affiche_xml_texte(yytext);
et();
} }
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(); closeSection( __FUNCTION__ );
egalBis();
affiche_balise_fermante("egal", 1);
} }
void egalBis(void){ void instruction()
affiche_balise_ouvrante("egalBis", 1); {
openSection( __FUNCTION__ );
if(uniteCourante == EGAL || uniteCourante == INFERIEUR){ if( est_premier( _instructionAffect_, uniteCourante ) ) {
uniteCourante = yylex(); instructionAffecterme();
affiche_xml_texte(yytext); } else if( est_premier( _instructionBloc_, uniteCourante ) ) {
egal(); 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) { void instructionBloc()
affiche_balise_ouvrante("expArith", 1); {
openSection( __FUNCTION__ );
terme(); if( uniteCourante == ACCOLADE_OUVRANTE ) {
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(); 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) { void listeInstructions()
affiche_balise_ouvrante("terme", 1); {
openSection( __FUNCTION__ );
facteur(); if( est_premier( _instruction_, uniteCourante ) ) {
termeBis(); instruction();
listeInstructions();
} else if( !est_suivanterme( _listeInstructions_, uniteCourante ) ) {
syntaxError();
}
affiche_balise_fermante("terme", 1); closeSection( __FUNCTION__ );
} }
void termeBis (void) { void instructionSi()
affiche_balise_ouvrante("termeBis", 1); {
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(); uniteCourante = yylex();
affiche_xml_texte(yytext);
terme(); terme();
expArithBis();
} else if( !est_suivanterme( _expArithBis_, uniteCourante ) ) {
syntaxError();
} }
affiche_balise_fermante("termeBis", 1); closeSection( __FUNCTION__ );
} }
void facteur (void) { void terme()
affiche_balise_ouvrante("facteur", 1); {
openSection( __FUNCTION__ );
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);
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(); uniteCourante = yylex();
affiche_xml_texte(yytext);
if(uniteCourante != PARENTHESE_OUVRANTE){ facteur();
printf("Erreur de syntaxe"); termeBis();
exit(-1); } 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(); uniteCourante = yylex();
affiche_xml_texte(yytext); } else if( est_premier( _appelFct_, uniteCourante ) ) {
if(uniteCourante != PARENTHESE_FERMANTE){ appelFct();
ou(); } else if( est_premier( _var_, uniteCourante ) ) {
while(uniteCourante == VIRGULE){ var();
} else if( uniteCourante == LIRE ) {
uniteCourante = yylex();
if( uniteCourante == PARENTHESE_OUVRANTE ) {
uniteCourante = yylex();
if( uniteCourante == PARENTHESE_FERMANTE ) {
uniteCourante = yylex(); uniteCourante = yylex();
affiche_xml_texte(yytext); } else {
ou(); syntaxErrorMsg( "')' été attendu" );
} }
} else {
syntaxErrorMsg( "'(' été attendu" );
} }
if(uniteCourante != PARENTHESE_FERMANTE){ } else {
printf("Erreur de syntaxe"); syntaxError();
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();