From e71dc9bae0bdc40a356b1c2d969f510e3056b69a Mon Sep 17 00:00:00 2001 From: Xawirses Date: Tue, 26 Jan 2016 15:41:24 +0100 Subject: [PATCH] Ajout de l'analyse syntaxique des expresion : - () - + - * --- compilateur-l.c | 0 header/analyseur_syntaxyque.h | 20 ++++++++++ src/analyseur_syntaxyque.c | 74 +++++++++++++++++++++++++++++++++++ src/compilateur-l.c | 32 +++++++++++---- src/test_yylex.c | 17 -------- test/expressionFaux | 1 + test/expressionVrai | 1 + 7 files changed, 120 insertions(+), 25 deletions(-) delete mode 100644 compilateur-l.c create mode 100644 header/analyseur_syntaxyque.h delete mode 100644 src/test_yylex.c create mode 100644 test/expressionFaux create mode 100644 test/expressionVrai diff --git a/compilateur-l.c b/compilateur-l.c deleted file mode 100644 index e69de29..0000000 diff --git a/header/analyseur_syntaxyque.h b/header/analyseur_syntaxyque.h new file mode 100644 index 0000000..4255927 --- /dev/null +++ b/header/analyseur_syntaxyque.h @@ -0,0 +1,20 @@ +#ifndef __ANALYSEUR_SYNTAXYQUE__ +#define __ANALYSEUR_SYNTAXYQUE__ + +#include +#include +#include +#include +#include "symboles.h" +#include "util.h" +#include "stdio.h" +#include "analyseur_lexical.h" + +void expArith (void); +void expArithBis (void); +void terme (void); +void termeBis (void); +void facteur (void); + + +#endif diff --git a/src/analyseur_syntaxyque.c b/src/analyseur_syntaxyque.c index e69de29..72e97a9 100644 --- a/src/analyseur_syntaxyque.c +++ b/src/analyseur_syntaxyque.c @@ -0,0 +1,74 @@ +#include "analyseur_syntaxyque.h" + +int uniteCourante; +extern char yytext[100]; + + + +void expArith (void) { + affiche_balise_ouvrante("expArith", 1); + + terme(); + expArithBis(); + + affiche_balise_fermante("expArith", 1); +} + +void expArithBis (void) { + affiche_balise_ouvrante("expArithBis", 1); + + if(uniteCourante == PLUS) { + uniteCourante = yylex(); + affiche_xml_texte(yytext); + + expArith(); + } + + 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 = yylex(); + affiche_xml_texte(yytext); + + terme(); + } + + affiche_balise_fermante("termeBis", 1); +} + +void facteur (void) { + affiche_balise_ouvrante("facteur", 1); + + if(uniteCourante == PARENTHESE_OUVRANTE) { + uniteCourante = yylex(); + affiche_xml_texte(yytext); + + expArith(); + + if(uniteCourante != PARENTHESE_FERMANTE) { + printf("Erreur de syntaxe"); + exit(-1); + } + } else if(uniteCourante == NOMBRE) { + uniteCourante = yylex(); + affiche_xml_texte(yytext); + } else { + printf("Erreur de syntaxe"); + exit(-1); + } + + affiche_balise_fermante("facteur", 1); +} \ No newline at end of file diff --git a/src/compilateur-l.c b/src/compilateur-l.c index 13f962b..442dadd 100644 --- a/src/compilateur-l.c +++ b/src/compilateur-l.c @@ -1,17 +1,33 @@ #include #include #include "analyseur_lexical.h" +#include "analyseur_syntaxyque.h" #include "symboles.h" +extern int uniteCourante; + char yytext[100]; FILE *yyin; + int main(int argc, char **argv) { - yyin = fopen(argv[1], "r"); - if(yyin == NULL){ - fprintf(stderr, "impossible d'ouvrir le fichier %s\n", argv[1]); - exit(1); - } - test_yylex_internal(yyin); - return 0; -} + yyin = fopen(argv[1], "r"); + if(yyin == NULL){ + fprintf(stderr, "impossible d'ouvrir le fichier %s\n", argv[1]); + exit(1); + } + + // test_yylex_internal(yyin); + uniteCourante = yylex(); + affiche_xml_texte(yytext); + + expArith(); + + uniteCourante = yylex(); + + if(uniteCourante != FIN) { + printf("Erreur EOF"); + } + + return 0; +} \ No newline at end of file diff --git a/src/test_yylex.c b/src/test_yylex.c deleted file mode 100644 index 13f962b..0000000 --- a/src/test_yylex.c +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include "analyseur_lexical.h" -#include "symboles.h" - -char yytext[100]; -FILE *yyin; - -int main(int argc, char **argv) { - yyin = fopen(argv[1], "r"); - if(yyin == NULL){ - fprintf(stderr, "impossible d'ouvrir le fichier %s\n", argv[1]); - exit(1); - } - test_yylex_internal(yyin); - return 0; -} diff --git a/test/expressionFaux b/test/expressionFaux new file mode 100644 index 0000000..d77a149 --- /dev/null +++ b/test/expressionFaux @@ -0,0 +1 @@ +10 * + 5 diff --git a/test/expressionVrai b/test/expressionVrai new file mode 100644 index 0000000..94c6e65 --- /dev/null +++ b/test/expressionVrai @@ -0,0 +1 @@ +12 + 4 * (5 + 10)