Mamannnnnnnnn !!!!!!!!!!!!!!

This commit is contained in:
2016-03-01 16:13:24 +01:00
parent 336d8d58bd
commit c400284b80
7 changed files with 807 additions and 20 deletions

View File

@@ -0,0 +1,362 @@
#include <stdio.h>
#include "syntabs.h"
#include "util.h"
void affiche_n_prog(n_prog *n);
void affiche_l_instr(n_l_instr *n);
void affiche_instr(n_instr *n);
void affiche_instr_si(n_instr *n);
void affiche_instr_tantque(n_instr *n);
void affiche_instr_faire(n_instr *n); /* MODIFIE POUR EVAL */
void affiche_instr_pour(n_instr *n); /* MODIFIE POUR EVAL */
void affiche_instr_affect(n_instr *n);
void affiche_instr_appel(n_instr *n);
void affiche_instr_retour(n_instr *n);
void affiche_instr_ecrire(n_instr *n);
void affiche_l_exp(n_l_exp *n);
void affiche_exp(n_exp *n);
void affiche_varExp(n_exp *n);
void affiche_opExp(n_exp *n);
void affiche_intExp(n_exp *n);
void affiche_lireExp(n_exp *n);
void affiche_appelExp(n_exp *n);
void affiche_l_dec(n_l_dec *n);
void affiche_dec(n_dec *n);
void affiche_foncDec(n_dec *n);
void affiche_varDec(n_dec *n);
void affiche_tabDec(n_dec *n);
void affiche_var(n_var *n);
void affiche_var_simple(n_var *n);
void affiche_var_indicee(n_var *n);
void affiche_appel(n_appel *n);
int trace_abs = 1;
/*-------------------------------------------------------------------------*/
void affiche_n_prog(n_prog *n)
{
char *fct = "prog";
affiche_balise_ouvrante(fct, trace_abs);
affiche_l_dec(n->variables);
affiche_l_dec(n->fonctions);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
void affiche_l_instr(n_l_instr *n)
{
char *fct = "l_instr";
if(n){
affiche_balise_ouvrante(fct, trace_abs);
affiche_instr(n->tete);
affiche_l_instr(n->queue);
affiche_balise_fermante(fct, trace_abs);
}
}
/*-------------------------------------------------------------------------*/
void affiche_instr(n_instr *n)
{
if(n){
if(n->type == blocInst) affiche_l_instr(n->u.liste);
else if(n->type == affecteInst) affiche_instr_affect(n);
else if(n->type == siInst) affiche_instr_si(n);
else if(n->type == tantqueInst) affiche_instr_tantque(n);
else if(n->type == faireInst) affiche_instr_faire(n);
else if(n->type == pourInst) affiche_instr_pour(n);
else if(n->type == appelInst) affiche_instr_appel(n);
else if(n->type == retourInst) affiche_instr_retour(n);
else if(n->type == ecrireInst) affiche_instr_ecrire(n);
}
}
/*-------------------------------------------------------------------------*/
void affiche_instr_si(n_instr *n)
{
char *fct = "instr_si";
affiche_balise_ouvrante(fct, trace_abs);
affiche_exp(n->u.si_.test);
affiche_instr(n->u.si_.alors);
if(n->u.si_.sinon){
affiche_instr(n->u.si_.sinon);
}
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_instr_tantque(n_instr *n)
{
char *fct = "instr_tantque";
affiche_balise_ouvrante(fct, trace_abs);
affiche_exp(n->u.tantque_.test);
affiche_instr(n->u.tantque_.faire);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_instr_faire(n_instr *n) /* MODIFIE POUR EVAL */
{ /* MODIFIE POUR EVAL */
char *fct = "instr_faire"; /* MODIFIE POUR EVAL */
affiche_balise_ouvrante(fct, trace_abs); /* MODIFIE POUR EVAL */
affiche_instr(n->u.faire_.faire); /* MODIFIE POUR EVAL */
affiche_exp(n->u.faire_.test); /* MODIFIE POUR EVAL */
affiche_balise_fermante(fct, trace_abs); /* MODIFIE POUR EVAL */
} /* MODIFIE POUR EVAL */
/*-------------------------------------------------------------------------*/
void affiche_instr_pour(n_instr *n) /* MODIFIE POUR EVAL */
{ /* MODIFIE POUR EVAL */
char *fct = "instr_pour"; /* MODIFIE POUR EVAL */
affiche_balise_ouvrante(fct, trace_abs); /* MODIFIE POUR EVAL */
affiche_instr(n->u.pour_.init); /* MODIFIE POUR EVAL */
affiche_exp(n->u.pour_.test); /* MODIFIE POUR EVAL */
affiche_instr(n->u.pour_.faire); /* MODIFIE POUR EVAL */
affiche_instr(n->u.pour_.incr); /* MODIFIE POUR EVAL */
affiche_balise_fermante(fct, trace_abs); /* MODIFIE POUR EVAL */
} /* MODIFIE POUR EVAL */
/*-------------------------------------------------------------------------*/
void affiche_instr_affect(n_instr *n)
{
char *fct = "instr_affect";
affiche_balise_ouvrante(fct, trace_abs);
affiche_var(n->u.affecte_.var);
affiche_exp(n->u.affecte_.exp);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_instr_appel(n_instr *n)
{
char *fct = "instr_appel";
affiche_balise_ouvrante(fct, trace_abs);
affiche_appel(n->u.appel);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_appel(n_appel *n)
{
char *fct = "appel";
affiche_balise_ouvrante(fct, trace_abs);
affiche_texte( n->fonction, trace_abs);
affiche_l_exp(n->args);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_instr_retour(n_instr *n)
{
char *fct = "instr_retour";
affiche_balise_ouvrante(fct, trace_abs);
affiche_exp(n->u.retour_.expression);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_instr_ecrire(n_instr *n)
{
char *fct = "instr_ecrire";
affiche_balise_ouvrante(fct, trace_abs);
affiche_exp(n->u.ecrire_.expression);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_l_exp(n_l_exp *n)
{
char *fct = "l_exp";
affiche_balise_ouvrante(fct, trace_abs);
if(n){
affiche_exp(n->tete);
affiche_l_exp(n->queue);
}
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_exp(n_exp *n)
{
if(n->type == varExp) affiche_varExp(n);
else if(n->type == opExp) affiche_opExp(n);
else if(n->type == intExp) affiche_intExp(n);
else if(n->type == appelExp) affiche_appelExp(n);
else if(n->type == lireExp) affiche_lireExp(n);
}
/*-------------------------------------------------------------------------*/
void affiche_varExp(n_exp *n)
{
char *fct = "varExp";
affiche_balise_ouvrante(fct, trace_abs);
affiche_var(n->u.var);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_opExp(n_exp *n)
{
char *fct = "opExp";
affiche_balise_ouvrante(fct, trace_abs);
if(n->u.opExp_.op == plus) affiche_texte("plus", trace_abs);
else if(n->u.opExp_.op == moins) affiche_texte("moins", trace_abs);
else if(n->u.opExp_.op == fois) affiche_texte("fois", trace_abs);
else if(n->u.opExp_.op == divise) affiche_texte("divise", trace_abs);
else if(n->u.opExp_.op == egal) affiche_texte("egal", trace_abs);
else if(n->u.opExp_.op == diff) affiche_texte("diff", trace_abs);
else if(n->u.opExp_.op == inf) affiche_texte("inf", trace_abs);
else if(n->u.opExp_.op == infeg) affiche_texte("infeg", trace_abs);
else if(n->u.opExp_.op == ou) affiche_texte("ou", trace_abs);
else if(n->u.opExp_.op == et) affiche_texte("et", trace_abs);
else if(n->u.opExp_.op == non) affiche_texte("non", trace_abs);
if( n->u.opExp_.op1 != NULL ) {
affiche_exp(n->u.opExp_.op1);
}
if( n->u.opExp_.op2 != NULL ) {
affiche_exp(n->u.opExp_.op2);
}
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_intExp(n_exp *n)
{
char texte[ 50 ]; // Max. 50 chiffres
sprintf(texte, "%d", n->u.entier);
affiche_element( "intExp", texte, trace_abs );
}
/*-------------------------------------------------------------------------*/
void affiche_lireExp(n_exp *n)
{
char *fct = "lireExp";
affiche_balise_ouvrante(fct, trace_abs);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_appelExp(n_exp *n)
{
char *fct = "appelExp";
affiche_balise_ouvrante(fct, trace_abs);
affiche_appel(n->u.appel);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_l_dec(n_l_dec *n)
{
char *fct = "l_dec";
if( n ){
affiche_balise_ouvrante(fct, trace_abs);
affiche_dec(n->tete);
affiche_l_dec(n->queue);
affiche_balise_fermante(fct, trace_abs);
}
}
/*-------------------------------------------------------------------------*/
void affiche_dec(n_dec *n)
{
if(n){
if(n->type == foncDec) {
affiche_foncDec(n);
}
else if(n->type == varDec) {
affiche_varDec(n);
}
else if(n->type == tabDec) {
affiche_tabDec(n);
}
}
}
/*-------------------------------------------------------------------------*/
void affiche_foncDec(n_dec *n)
{
char *fct = "foncDec";
affiche_balise_ouvrante(fct, trace_abs);
affiche_texte( n->nom, trace_abs );
affiche_l_dec(n->u.foncDec_.param);
affiche_l_dec(n->u.foncDec_.variables);
affiche_instr(n->u.foncDec_.corps);
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_varDec(n_dec *n)
{
affiche_element("varDec", n->nom, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_tabDec(n_dec *n)
{
char texte[100]; // Max. 100 chars nom tab + taille
sprintf(texte, "%s[%d]", n->nom, n->u.tabDec_.taille);
affiche_element( "tabDec", texte, trace_abs );
}
/*-------------------------------------------------------------------------*/
void affiche_var(n_var *n)
{
if(n->type == simple) {
affiche_var_simple(n);
}
else if(n->type == indicee) {
affiche_var_indicee(n);
}
}
/*-------------------------------------------------------------------------*/
void affiche_var_simple(n_var *n)
{
affiche_element("var_simple", n->nom, trace_abs);
}
/*-------------------------------------------------------------------------*/
void affiche_var_indicee(n_var *n)
{
char *fct = "var_indicee";
affiche_balise_ouvrante(fct, trace_abs);
affiche_element("var_base_tableau", n->nom, trace_abs);
affiche_exp( n->u.indicee_.indice );
affiche_balise_fermante(fct, trace_abs);
}
/*-------------------------------------------------------------------------*/

View File

@@ -1,7 +1,7 @@
#include "analyseur_syntaxyque.h"
int uniteCourante;
int afficheSyntaxyque = 1;
int afficheSyntaxyque = 0;
extern char yytext[100];
extern int nb_ligne;
@@ -608,85 +608,113 @@ void comparaisonBis() {
closeSection( __func__ );
}
void expArith()
n_exp expArith()
{
openSection( __func__ );
n_exp *sreturn;
n_exp *herite_fils;
if( est_premier( _terme_, uniteCourante ) ) {
terme();
expArithBis();
herite_fils = terme();
sreturn = expArithBis(herite_fils);
} else {
syntaxError();
}
closeSection( __func__ );
return sreturn;
}
void expArithBis() {
n_exp expArithBis(n_exp *herite) {
openSection( __func__ );
n_exp *s;
n_exp *sreturn;
n_exp *herite_fils;
if( uniteCourante == PLUS ) {
elementConsome();
uniteCourante = yylex();
terme();
expArithBis();
s = terme();
herite_fils = cree_n_exp_op(plus, herite, s);
sreturn = expArithBis(herite_fils);
} else if( uniteCourante == MOINS) {
elementConsome();
uniteCourante = yylex();
terme();
expArithBis();
s = terme();
erite_fils = cree_n_exp_op(moins, herite, s);
sreturn = expArithBis();
} else if( !est_suivant( _expArithBis_, uniteCourante ) ) {
syntaxError();
} else {
sreturn = herite;
}
closeSection( __func__ );
return sreturn;
}
void terme() {
n_exp terme() {
openSection( __func__ );
n_exp *sreturn;
n_exp *herite_fils;
if( est_premier( _facteur_, uniteCourante ) ) {
facteur();
termeBis();
herite_fils = facteur();
sreturn = termeBis(herite_fils);
} else {
syntaxError();
}
closeSection( __func__ );
return sreturn;
}
void termeBis() {
n_exp *termeBis(n_exp *herite) {
openSection( __func__ );
n_exp *s;
n_exp *sreturn;
n_exp *herite_fils;
if( uniteCourante == FOIS ) {
elementConsome();
uniteCourante = yylex();
facteur();
termeBis();
s = facteur();
herite_fils = cree_n_exp_op(fois, herite, s);
sreturn = termeBis(herite_fils);
} else if( uniteCourante == DIVISE) {
elementConsome();
uniteCourante = yylex();
facteur();
termeBis();
s = facteur();
herite_fils = cree_n_exp_op(divise, herite, s);
sreturn = termeBis(herite_fils);
} else if( !est_suivant( _termeBis_, uniteCourante ) ) {
syntaxError();
} else {
sreturn = herite;
}
closeSection( __func__ );
return sreturn;
}
void facteur() {
n_exp facteur() {
openSection( __func__ );
n_exp *sreturn;
if( uniteCourante == PARENTHESE_OUVRANTE ) {
elementConsome();
uniteCourante = yylex();
expression();
sreturn = expression();
if( uniteCourante == PARENTHESE_FERMANTE ) {
elementConsome();
@@ -695,10 +723,11 @@ void facteur() {
syntaxErrorMsg( "')' été attendu" );
}
} else if( uniteCourante == NOMBRE ) {
sreturn = cree_n_exp_entier(atoi(yytext));
elementConsome();
uniteCourante = yylex();
} else if( est_premier( _appelFct_, uniteCourante ) ) {
appelFct();
appelFct(); // TODOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
} else if( est_premier( _var_, uniteCourante ) ) {
var();
} else if( uniteCourante == LIRE ) {

View File

@@ -2,9 +2,11 @@
#include <stdlib.h>
#include "analyseur_lexical.h"
#include "analyseur_syntaxyque.h"
#include "affiche_arbre_abstrait.h"
#include "symboles.h"
extern int uniteCourante;
extern int afficheSyntaxyque;
char yytext[100];
FILE *yyin;
@@ -20,14 +22,22 @@ int main(int argc, char **argv) {
if (!strcmp(argv[1], "-l")) {
test_yylex_internal(yyin);
} else if (!strcmp(argv[1], "-s")) {
afficheSyntaxyque = 1;
initialise_premiers();
initialise_suivants();
uniteCourante = yylex();
programme();
} else if (!strcmp(argv[1], "-a")) {
initialise_premiers();
initialise_suivants();
uniteCourante = yylex();
n_prog *p = programme();
affiche_n_prog(p);
} else {
fprintf(stderr, "option Inconue");
exit(1);
}
uniteCourante = yylex();

236
src/syntabs.c Normal file
View File

@@ -0,0 +1,236 @@
#include<stdio.h>
#include<stdlib.h>
#include"util.h"
#include"syntabs.h"
n_appel *cree_n_appel(char *fonction, n_l_exp *args)
{
n_appel *n = malloc(sizeof(n_appel));
n->fonction = fonction;
n->args = args;
return n;
}
n_prog *cree_n_prog(n_l_dec *variables, n_l_dec *fonctions)
{
n_prog *n = malloc(sizeof(n_prog));
n->variables = variables;
n->fonctions = fonctions;
return n;
}
n_var *cree_n_var_simple(char *nom)
{
n_var *n = malloc(sizeof(n_var));
n->type = simple;
n->nom = nom;
return n;
}
n_var *cree_n_var_indicee(char *nom, n_exp *indice)
{
n_var *n = malloc(sizeof(n_var));
n->type = indicee;
n->nom = nom;
n->u.indicee_.indice = indice;
return n;
}
n_exp *cree_n_exp_op(operation op, n_exp *op1, n_exp *op2)
{
n_exp *n = malloc(sizeof(n_exp));
n->type = opExp;
n->u.opExp_.op = op;
n->u.opExp_.op1 = op1;
n->u.opExp_.op2 = op2;
return n;
}
n_exp *cree_n_exp_appel(n_appel *app)
{
n_exp *n = malloc(sizeof(n_exp));
n->type = appelExp;
n->u.appel = app;
return n;
}
n_exp *cree_n_exp_incr(n_var *var)
{
n_exp *n = malloc(sizeof(n_exp));
n->type = incrExp;
n->u.incr = var;
return n;
}
n_exp *cree_n_exp_var(n_var *var)
{
n_exp *n = malloc(sizeof(n_exp));
n->type = varExp;
n->u.var = var;
return n;
}
n_exp *cree_n_exp_entier(int entier)
{
n_exp *n = malloc(sizeof(n_exp));
n->type = intExp;
n->u.entier = entier;
return n;
}
n_exp *cree_n_exp_lire()
{
n_exp *n = malloc(sizeof(n_exp));
n->type = lireExp;
return n;
}
n_l_exp *cree_n_l_exp(n_exp *tete, n_l_exp *queue)
{
n_l_exp *n = malloc(sizeof(n_l_exp));
n->tete = tete;
n->queue = queue;
return n;
}
n_instr *cree_n_instr_incr(n_exp *incr)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = incrInst;
n->u.incr = incr;
return n;
}
n_instr *cree_n_instr_si(n_exp *test, n_instr *alors, n_instr *sinon)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = siInst;
n->u.si_.test = test;
n->u.si_.alors = alors;
n->u.si_.sinon = sinon;
return n;
}
n_instr *cree_n_instr_tantque(n_exp *test, n_instr *faire)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = tantqueInst;
n->u.tantque_.test = test;
n->u.tantque_.faire = faire;
return n;
}
n_instr *cree_n_instr_faire(n_instr *faire, n_exp *test)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = faireInst;
n->u.tantque_.test = test;
n->u.tantque_.faire = faire;
return n;
}
n_instr *cree_n_instr_pour(n_instr *init, n_exp *test,
n_instr *incr, n_instr *faire)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = pourInst;
n->u.pour_.test = test;
n->u.pour_.faire = faire;
n->u.pour_.incr = incr;
n->u.pour_.init = init;
return n;
}
n_instr *cree_n_instr_affect(n_var *var, n_exp *exp)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = affecteInst;
n->u.affecte_.var = var;
n->u.affecte_.exp = exp;
return n;
}
n_l_instr *cree_n_l_instr(n_instr *tete, n_l_instr *queue)
{
n_l_instr *n = malloc(sizeof(n_l_instr));
n->tete = tete;
n->queue = queue;
return n;
}
n_instr *cree_n_instr_bloc(n_l_instr *liste)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = blocInst;
n->u.liste = liste;
return n;
}
n_instr *cree_n_instr_appel(n_appel *app)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = appelInst;
n->u.appel = app;
return n;
}
n_instr *cree_n_instr_ecrire(n_exp *expression)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = ecrireInst;
n->u.ecrire_.expression = expression;
return n;
}
n_instr *cree_n_instr_retour(n_exp *expression)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = retourInst;
n->u.retour_.expression = expression;
return n;
}
n_instr *cree_n_instr_vide(void)
{
n_instr *n = malloc(sizeof(n_instr));
n->type = videInst;
return n;
}
n_dec *cree_n_dec_var(char *nom)
{
n_dec *n = malloc(sizeof(n_dec));
n->type = varDec;
n->nom = nom;
return n;
}
n_dec *cree_n_dec_tab(char *nom, int taille)
{
n_dec *n = malloc(sizeof(n_dec));
n->type = tabDec;
n->nom = nom;
n->u.tabDec_.taille = taille;
return n;
}
n_dec *cree_n_dec_fonc(char *nom, n_l_dec *param, n_l_dec *variables, n_instr *corps)
{
n_dec *n = malloc(sizeof(n_dec));
n->type = foncDec;
n->nom = nom;
n->u.foncDec_.param = param;
n->u.foncDec_.variables = variables;
n->u.foncDec_.corps = corps;
return n;
}
n_l_dec *cree_n_l_dec(n_dec *tete, n_l_dec *queue)
{
n_l_dec *n = malloc(sizeof(n_l_dec));
n->tete = tete;
n->queue = queue;
return n;
}