Mamannnnnnnnn !!!!!!!!!!!!!!
This commit is contained in:
9
header/affiche_arbre_abstrait.h
Normal file
9
header/affiche_arbre_abstrait.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#ifndef __AFFICHE_ARBRE_ABSTRAIT__
|
||||||
|
#define __AFFICHE_ARBRE_ABSTRAIT__
|
||||||
|
|
||||||
|
#include "syntabs.h"
|
||||||
|
|
||||||
|
void affiche_n_prog(n_prog *n);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
@@ -11,6 +11,8 @@
|
|||||||
#include "analyseur_lexical.h"
|
#include "analyseur_lexical.h"
|
||||||
#include "premiers.h"
|
#include "premiers.h"
|
||||||
#include "suivants.h"
|
#include "suivants.h"
|
||||||
|
#include "syntabs.h"
|
||||||
|
#include "affiche_arbre_abstrait.h"
|
||||||
|
|
||||||
void openSection ( const char * section );
|
void openSection ( const char * section );
|
||||||
void closeSection ( const char * section );
|
void closeSection ( const char * section );
|
||||||
|
|||||||
139
header/syntabs.h
Normal file
139
header/syntabs.h
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
#ifndef __SYNTABS__
|
||||||
|
#define __SYNTABS__
|
||||||
|
|
||||||
|
typedef struct n_l_instr_ n_l_instr;
|
||||||
|
typedef struct n_instr_ n_instr;
|
||||||
|
typedef struct n_exp_ n_exp;
|
||||||
|
typedef struct n_l_exp_ n_l_exp;
|
||||||
|
typedef struct n_var_ n_var;
|
||||||
|
typedef struct n_l_dec_ n_l_dec;
|
||||||
|
typedef struct n_dec_ n_dec;
|
||||||
|
typedef struct n_prog_ n_prog;
|
||||||
|
typedef struct n_appel_ n_appel;
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
struct n_prog_ {
|
||||||
|
n_l_dec *variables;
|
||||||
|
n_l_dec *fonctions;
|
||||||
|
};
|
||||||
|
|
||||||
|
n_prog *cree_n_prog(n_l_dec *variables, n_l_dec *fonctions);
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
struct n_dec_ {
|
||||||
|
enum{foncDec, varDec, tabDec} type;
|
||||||
|
char *nom;
|
||||||
|
union {
|
||||||
|
struct{n_l_dec *param; n_l_dec *variables; n_instr *corps;}foncDec_;
|
||||||
|
struct{int type;}varDec_;
|
||||||
|
struct{int taille;}tabDec_;
|
||||||
|
} u;
|
||||||
|
};
|
||||||
|
|
||||||
|
n_dec *cree_n_dec_var(char *nom);
|
||||||
|
n_dec *cree_n_dec_tab(char *nom, int taille);
|
||||||
|
n_dec *cree_n_dec_fonc(char *nom, n_l_dec *param, n_l_dec *variables, n_instr *corps);
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/* ATTENTION : negatif => - unaire, comme dans -5
|
||||||
|
non => négation logique
|
||||||
|
Le opérateurs unaires ont op2 = NULL par convention */
|
||||||
|
typedef enum {plus, moins, fois, divise, modulo, egal, diff, inf, sup, infeg, supeg, ou, et, non, negatif} operation;
|
||||||
|
|
||||||
|
struct n_exp_ {
|
||||||
|
enum{varExp, opExp, intExp, appelExp, lireExp, incrExp} type;
|
||||||
|
union{
|
||||||
|
struct{operation op; struct n_exp_ *op1; struct n_exp_ *op2;} opExp_;
|
||||||
|
n_var *var;
|
||||||
|
n_var *incr;
|
||||||
|
int entier;
|
||||||
|
n_appel *appel;
|
||||||
|
}u;
|
||||||
|
};
|
||||||
|
|
||||||
|
n_exp *cree_n_exp_op(operation type, n_exp *op1, n_exp *op2);
|
||||||
|
n_exp *cree_n_exp_entier(int entier);
|
||||||
|
n_exp *cree_n_exp_var(n_var *var);
|
||||||
|
n_exp *cree_n_exp_appel(n_appel *app);
|
||||||
|
n_exp *cree_n_exp_lire(void);
|
||||||
|
n_exp *cree_n_exp_incr(n_var *var);
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
struct n_instr_ {
|
||||||
|
enum {incrInst, affecteInst, siInst, faireInst, tantqueInst, appelInst, retourInst, ecrireInst, videInst, blocInst, pourInst } type; /* MODIFIE POUR EVAL*/
|
||||||
|
union{
|
||||||
|
n_exp *incr;
|
||||||
|
struct{n_var *var; n_exp *exp;} affecte_;
|
||||||
|
struct{n_exp *test; struct n_instr_ *alors; struct n_instr_ *sinon;} si_;
|
||||||
|
struct{n_exp *test; struct n_instr_ *faire;} tantque_;
|
||||||
|
struct{n_exp *test; struct n_instr_ *faire;} faire_;
|
||||||
|
struct{n_exp *test; struct n_instr_ *init;
|
||||||
|
struct n_instr_ *incr; struct n_instr_ *faire;} pour_;
|
||||||
|
n_appel *appel;
|
||||||
|
struct{n_exp *expression;} retour_;
|
||||||
|
struct{n_exp *expression;} ecrire_;
|
||||||
|
n_l_instr *liste;
|
||||||
|
}u;
|
||||||
|
};
|
||||||
|
|
||||||
|
n_instr *cree_n_instr_incr(n_exp *incr);
|
||||||
|
n_instr *cree_n_instr_si(n_exp *test, n_instr *alors, n_instr *sinon);
|
||||||
|
n_instr *cree_n_instr_bloc(n_l_instr *liste);
|
||||||
|
n_instr *cree_n_instr_tantque(n_exp *test, n_instr *faire);
|
||||||
|
n_instr *cree_n_instr_faire(n_instr *faire, n_exp *test);
|
||||||
|
n_instr *cree_n_instr_pour(n_instr *init, n_exp *test, n_instr *incr, n_instr *faire);
|
||||||
|
n_instr *cree_n_instr_affect(n_var *var, n_exp *exp);
|
||||||
|
n_instr *cree_n_instr_appel(n_appel *appel);
|
||||||
|
n_instr *cree_n_instr_retour(n_exp *expression);
|
||||||
|
n_instr *cree_n_instr_ecrire(n_exp *expression);
|
||||||
|
n_instr *cree_n_instr_vide(void);
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
struct n_appel_{
|
||||||
|
char *fonction;
|
||||||
|
n_l_exp *args;
|
||||||
|
};
|
||||||
|
|
||||||
|
n_appel *cree_n_appel(char *fonction, n_l_exp *args);
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
struct n_var_ {
|
||||||
|
enum {simple, indicee} type;
|
||||||
|
char *nom;
|
||||||
|
union {
|
||||||
|
struct{n_exp *indice;} indicee_;
|
||||||
|
}u;
|
||||||
|
};
|
||||||
|
|
||||||
|
n_var *cree_n_var_simple(char *nom);
|
||||||
|
n_var *cree_n_var_indicee(char *nom, n_exp *indice);
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
struct n_l_exp_ {
|
||||||
|
n_exp *tete;
|
||||||
|
struct n_l_exp_ *queue;
|
||||||
|
};
|
||||||
|
|
||||||
|
n_l_exp *cree_n_l_exp(n_exp *tete, n_l_exp *queue);
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
struct n_l_instr_ {
|
||||||
|
n_instr *tete;
|
||||||
|
struct n_l_instr_ *queue;
|
||||||
|
};
|
||||||
|
|
||||||
|
n_l_instr *cree_n_l_instr(n_instr *tete, n_l_instr *queue);
|
||||||
|
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
struct n_l_dec_{
|
||||||
|
n_dec *tete;
|
||||||
|
struct n_l_dec_ *queue;
|
||||||
|
};
|
||||||
|
|
||||||
|
n_l_dec *cree_n_l_dec(n_dec *tete, n_l_dec *queue);
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
#endif
|
||||||
362
src/affiche_arbre_abstrait.c
Normal file
362
src/affiche_arbre_abstrait.c
Normal 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);
|
||||||
|
}
|
||||||
|
/*-------------------------------------------------------------------------*/
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include "analyseur_syntaxyque.h"
|
#include "analyseur_syntaxyque.h"
|
||||||
|
|
||||||
int uniteCourante;
|
int uniteCourante;
|
||||||
int afficheSyntaxyque = 1;
|
int afficheSyntaxyque = 0;
|
||||||
extern char yytext[100];
|
extern char yytext[100];
|
||||||
extern int nb_ligne;
|
extern int nb_ligne;
|
||||||
|
|
||||||
@@ -608,85 +608,113 @@ void comparaisonBis() {
|
|||||||
closeSection( __func__ );
|
closeSection( __func__ );
|
||||||
}
|
}
|
||||||
|
|
||||||
void expArith()
|
n_exp expArith()
|
||||||
{
|
{
|
||||||
openSection( __func__ );
|
openSection( __func__ );
|
||||||
|
|
||||||
|
n_exp *sreturn;
|
||||||
|
n_exp *herite_fils;
|
||||||
|
|
||||||
if( est_premier( _terme_, uniteCourante ) ) {
|
if( est_premier( _terme_, uniteCourante ) ) {
|
||||||
terme();
|
herite_fils = terme();
|
||||||
expArithBis();
|
sreturn = expArithBis(herite_fils);
|
||||||
} else {
|
} else {
|
||||||
syntaxError();
|
syntaxError();
|
||||||
}
|
}
|
||||||
|
|
||||||
closeSection( __func__ );
|
closeSection( __func__ );
|
||||||
|
return sreturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void expArithBis() {
|
n_exp expArithBis(n_exp *herite) {
|
||||||
openSection( __func__ );
|
openSection( __func__ );
|
||||||
|
|
||||||
|
n_exp *s;
|
||||||
|
n_exp *sreturn;
|
||||||
|
n_exp *herite_fils;
|
||||||
|
|
||||||
if( uniteCourante == PLUS ) {
|
if( uniteCourante == PLUS ) {
|
||||||
elementConsome();
|
elementConsome();
|
||||||
uniteCourante = yylex();
|
uniteCourante = yylex();
|
||||||
|
|
||||||
terme();
|
s = terme();
|
||||||
expArithBis();
|
herite_fils = cree_n_exp_op(plus, herite, s);
|
||||||
|
sreturn = expArithBis(herite_fils);
|
||||||
} else if( uniteCourante == MOINS) {
|
} else if( uniteCourante == MOINS) {
|
||||||
elementConsome();
|
elementConsome();
|
||||||
uniteCourante = yylex();
|
uniteCourante = yylex();
|
||||||
|
|
||||||
terme();
|
s = terme();
|
||||||
expArithBis();
|
erite_fils = cree_n_exp_op(moins, herite, s);
|
||||||
|
sreturn = expArithBis();
|
||||||
} else if( !est_suivant( _expArithBis_, uniteCourante ) ) {
|
} else if( !est_suivant( _expArithBis_, uniteCourante ) ) {
|
||||||
syntaxError();
|
syntaxError();
|
||||||
|
} else {
|
||||||
|
sreturn = herite;
|
||||||
}
|
}
|
||||||
|
|
||||||
closeSection( __func__ );
|
closeSection( __func__ );
|
||||||
|
return sreturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void terme() {
|
n_exp terme() {
|
||||||
openSection( __func__ );
|
openSection( __func__ );
|
||||||
|
|
||||||
|
n_exp *sreturn;
|
||||||
|
n_exp *herite_fils;
|
||||||
|
|
||||||
if( est_premier( _facteur_, uniteCourante ) ) {
|
if( est_premier( _facteur_, uniteCourante ) ) {
|
||||||
facteur();
|
herite_fils = facteur();
|
||||||
termeBis();
|
sreturn = termeBis(herite_fils);
|
||||||
} else {
|
} else {
|
||||||
syntaxError();
|
syntaxError();
|
||||||
}
|
}
|
||||||
|
|
||||||
closeSection( __func__ );
|
closeSection( __func__ );
|
||||||
|
return sreturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void termeBis() {
|
n_exp *termeBis(n_exp *herite) {
|
||||||
openSection( __func__ );
|
openSection( __func__ );
|
||||||
|
|
||||||
|
n_exp *s;
|
||||||
|
n_exp *sreturn;
|
||||||
|
n_exp *herite_fils;
|
||||||
|
|
||||||
if( uniteCourante == FOIS ) {
|
if( uniteCourante == FOIS ) {
|
||||||
elementConsome();
|
elementConsome();
|
||||||
uniteCourante = yylex();
|
uniteCourante = yylex();
|
||||||
|
|
||||||
facteur();
|
s = facteur();
|
||||||
termeBis();
|
herite_fils = cree_n_exp_op(fois, herite, s);
|
||||||
|
sreturn = termeBis(herite_fils);
|
||||||
} else if( uniteCourante == DIVISE) {
|
} else if( uniteCourante == DIVISE) {
|
||||||
elementConsome();
|
elementConsome();
|
||||||
uniteCourante = yylex();
|
uniteCourante = yylex();
|
||||||
|
|
||||||
facteur();
|
s = facteur();
|
||||||
termeBis();
|
herite_fils = cree_n_exp_op(divise, herite, s);
|
||||||
|
sreturn = termeBis(herite_fils);
|
||||||
} else if( !est_suivant( _termeBis_, uniteCourante ) ) {
|
} else if( !est_suivant( _termeBis_, uniteCourante ) ) {
|
||||||
syntaxError();
|
syntaxError();
|
||||||
|
} else {
|
||||||
|
sreturn = herite;
|
||||||
}
|
}
|
||||||
|
|
||||||
closeSection( __func__ );
|
closeSection( __func__ );
|
||||||
|
return sreturn;
|
||||||
}
|
}
|
||||||
|
|
||||||
void facteur() {
|
n_exp facteur() {
|
||||||
openSection( __func__ );
|
openSection( __func__ );
|
||||||
|
|
||||||
|
n_exp *sreturn;
|
||||||
|
|
||||||
if( uniteCourante == PARENTHESE_OUVRANTE ) {
|
if( uniteCourante == PARENTHESE_OUVRANTE ) {
|
||||||
elementConsome();
|
elementConsome();
|
||||||
uniteCourante = yylex();
|
uniteCourante = yylex();
|
||||||
|
|
||||||
expression();
|
sreturn = expression();
|
||||||
|
|
||||||
if( uniteCourante == PARENTHESE_FERMANTE ) {
|
if( uniteCourante == PARENTHESE_FERMANTE ) {
|
||||||
elementConsome();
|
elementConsome();
|
||||||
@@ -695,10 +723,11 @@ void facteur() {
|
|||||||
syntaxErrorMsg( "')' été attendu" );
|
syntaxErrorMsg( "')' été attendu" );
|
||||||
}
|
}
|
||||||
} else if( uniteCourante == NOMBRE ) {
|
} else if( uniteCourante == NOMBRE ) {
|
||||||
|
sreturn = cree_n_exp_entier(atoi(yytext));
|
||||||
elementConsome();
|
elementConsome();
|
||||||
uniteCourante = yylex();
|
uniteCourante = yylex();
|
||||||
} else if( est_premier( _appelFct_, uniteCourante ) ) {
|
} else if( est_premier( _appelFct_, uniteCourante ) ) {
|
||||||
appelFct();
|
appelFct(); // TODOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
|
||||||
} else if( est_premier( _var_, uniteCourante ) ) {
|
} else if( est_premier( _var_, uniteCourante ) ) {
|
||||||
var();
|
var();
|
||||||
} else if( uniteCourante == LIRE ) {
|
} else if( uniteCourante == LIRE ) {
|
||||||
|
|||||||
@@ -2,9 +2,11 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "analyseur_lexical.h"
|
#include "analyseur_lexical.h"
|
||||||
#include "analyseur_syntaxyque.h"
|
#include "analyseur_syntaxyque.h"
|
||||||
|
#include "affiche_arbre_abstrait.h"
|
||||||
#include "symboles.h"
|
#include "symboles.h"
|
||||||
|
|
||||||
extern int uniteCourante;
|
extern int uniteCourante;
|
||||||
|
extern int afficheSyntaxyque;
|
||||||
|
|
||||||
char yytext[100];
|
char yytext[100];
|
||||||
FILE *yyin;
|
FILE *yyin;
|
||||||
@@ -20,14 +22,22 @@ int main(int argc, char **argv) {
|
|||||||
if (!strcmp(argv[1], "-l")) {
|
if (!strcmp(argv[1], "-l")) {
|
||||||
test_yylex_internal(yyin);
|
test_yylex_internal(yyin);
|
||||||
} else if (!strcmp(argv[1], "-s")) {
|
} else if (!strcmp(argv[1], "-s")) {
|
||||||
|
afficheSyntaxyque = 1;
|
||||||
initialise_premiers();
|
initialise_premiers();
|
||||||
initialise_suivants();
|
initialise_suivants();
|
||||||
|
|
||||||
uniteCourante = yylex();
|
uniteCourante = yylex();
|
||||||
programme();
|
programme();
|
||||||
|
} else if (!strcmp(argv[1], "-a")) {
|
||||||
|
initialise_premiers();
|
||||||
|
initialise_suivants();
|
||||||
|
|
||||||
|
uniteCourante = yylex();
|
||||||
|
n_prog *p = programme();
|
||||||
|
affiche_n_prog(p);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "option Inconue");
|
fprintf(stderr, "option Inconue");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
uniteCourante = yylex();
|
uniteCourante = yylex();
|
||||||
|
|||||||
236
src/syntabs.c
Normal file
236
src/syntabs.c
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user