Ajout analyse sémantique :

- Verif declaration var & tab
- Verif appel var
This commit is contained in:
2016-03-08 15:57:03 +01:00
parent 2d23d0dc3a
commit 634440714c
10 changed files with 566 additions and 45 deletions

View File

@@ -4,6 +4,32 @@
#include "syntabs.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);
#endif

View File

@@ -0,0 +1,36 @@
#ifndef __affiche_ARBRE_ABSTRAIT__
#define __affiche_ARBRE_ABSTRAIT__
#include "syntabs.h"
#include "dico.h"
void affiche_t_table(n_prog *n);
void affiche_t_l_instr(n_l_instr *n);
void affiche_t_instr(n_instr *n);
void affiche_t_instr_si(n_instr *n);
void affiche_t_instr_tantque(n_instr *n);
void affiche_t_instr_faire(n_instr *n);
void affiche_t_instr_pour(n_instr *n);
void affiche_t_instr_affect(n_instr *n);
void affiche_t_instr_appel(n_instr *n);
void affiche_t_instr_retour(n_instr *n);
void affiche_t_instr_ecrire(n_instr *n);
void affiche_t_l_exp(n_l_exp *n);
void affiche_t_exp(n_exp *n);
void affiche_t_varExp(n_exp *n);
void affiche_t_opExp(n_exp *n);
void affiche_t_intExp(n_exp *n);
void affiche_t_lireExp(n_exp *n);
void affiche_t_appelExp(n_exp *n);
void affiche_t_l_dec(n_l_dec *n);
void affiche_t_dec(n_dec *n);
void affiche_t_foncDec(n_dec *n);
void affiche_t_varDec(n_dec *n);
void affiche_t_tabDec(n_dec *n);
void affiche_t_var(n_var *n);
void affiche_t_var_simple(n_var *n);
void affiche_t_var_indicee(n_var *n);
void affiche_t_appel(n_appel *n);
#endif

47
header/dico.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef __DICO__
#define __DICO__
#define _GNU_SOURCE 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "syntabs.h"
#define maxDico 100
#define C_VARIABLE_GLOBALE 1
#define C_VARIABLE_LOCALE 2
#define C_ARGUMENT 3
#define T_ENTIER 1
#define T_TABLEAU_ENTIER 2
#define T_FONCTION 3
typedef struct {
char *identif;
int classe; // portée
int type;
int adresse;
int complement; // taille d'un tableau, nombre d'arguments d'une fonction
} desc_identif;
typedef struct {
desc_identif tab[maxDico];
int base;
int sommet;
} dico_;
void entreeFonction(void);
void sortieFonction(void);
int ajouteIdentificateur(char *identif, int classe, int type, int adresse, int complement);
int rechercheExecutable(char *identif);
int rechercheDeclarative(char *identif);
void affiche_dico(void);
dico_ dico;
extern int contexte;
extern int adresseGlobalCourante;
extern int adresseLocaleCourante;
extern int adresseArgumentCourant;
#endif

View File

@@ -1,35 +1,8 @@
#include <stdio.h>
#include "affiche_arbre_abstrait.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;
/*-------------------------------------------------------------------------*/

339
src/affiche_table_symbole.c Normal file
View File

@@ -0,0 +1,339 @@
#include <stdio.h>
#include "affiche_table_symbole.h"
#include "syntabs.h"
#include "util.h"
int contexte = C_VARIABLE_GLOBALE;
int adresseGlobalCourante = 0;
int adresseLocaleCourante = 0;
int adresseArgumentCourant = 0;
extern dico_ dico;
/*-------------------------------------------------------------------------*/
void affiche_t_table(n_prog *n)
{
char *fct = "prog";
affiche_t_l_dec(n->variables);
affiche_t_l_dec(n->fonctions);
}
/*-------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------*/
void affiche_t_l_instr(n_l_instr *n)
{
char *fct = "l_instr";
if(n){
affiche_t_instr(n->tete);
affiche_t_l_instr(n->queue);
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_instr(n_instr *n)
{
if(n){
if(n->type == blocInst) affiche_t_l_instr(n->u.liste);
else if(n->type == affecteInst) affiche_t_instr_affect(n);
else if(n->type == siInst) affiche_t_instr_si(n);
else if(n->type == tantqueInst) affiche_t_instr_tantque(n);
else if(n->type == faireInst) affiche_t_instr_faire(n);
else if(n->type == pourInst) affiche_t_instr_pour(n);
else if(n->type == appelInst) affiche_t_instr_appel(n);
else if(n->type == retourInst) affiche_t_instr_retour(n);
else if(n->type == ecrireInst) affiche_t_instr_ecrire(n);
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_instr_si(n_instr *n)
{
char *fct = "instr_si";
affiche_t_exp(n->u.si_.test);
affiche_t_instr(n->u.si_.alors);
if(n->u.si_.sinon){
affiche_t_instr(n->u.si_.sinon);
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_instr_tantque(n_instr *n)
{
char *fct = "instr_tantque";
affiche_t_exp(n->u.tantque_.test);
affiche_t_instr(n->u.tantque_.faire);
}
/*-------------------------------------------------------------------------*/
void affiche_t_instr_faire(n_instr *n)
{
char *fct = "instr_faire";
affiche_t_instr(n->u.faire_.faire);
affiche_t_exp(n->u.faire_.test);
}
/*-------------------------------------------------------------------------*/
void affiche_t_instr_pour(n_instr *n)
{
char *fct = "instr_pour";
affiche_t_instr(n->u.pour_.init);
affiche_t_exp(n->u.pour_.test);
affiche_t_instr(n->u.pour_.faire);
affiche_t_instr(n->u.pour_.incr);
}
/*-------------------------------------------------------------------------*/
void affiche_t_instr_affect(n_instr *n)
{
char *fct = "instr_affect";
affiche_t_var(n->u.affecte_.var);
affiche_t_exp(n->u.affecte_.exp);
}
/*-------------------------------------------------------------------------*/
void affiche_t_instr_appel(n_instr *n)
{
char *fct = "instr_appel";
affiche_t_appel(n->u.appel);
}
/*-------------------------------------------------------------------------*/
void affiche_t_appel(n_appel *n)
{
char *fct = "appel";
//affiche_t_texte( n->fonction, trace_abs);
affiche_t_l_exp(n->args);
}
/*-------------------------------------------------------------------------*/
void affiche_t_instr_retour(n_instr *n)
{
char *fct = "instr_retour";
affiche_t_exp(n->u.retour_.expression);
}
/*-------------------------------------------------------------------------*/
void affiche_t_instr_ecrire(n_instr *n)
{
char *fct = "instr_ecrire";
affiche_t_exp(n->u.ecrire_.expression);
}
/*-------------------------------------------------------------------------*/
void affiche_t_l_exp(n_l_exp *n)
{
char *fct = "l_exp";
if(n){
affiche_t_exp(n->tete);
affiche_t_l_exp(n->queue);
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_exp(n_exp *n)
{
if(n->type == varExp) affiche_t_varExp(n);
else if(n->type == opExp) affiche_t_opExp(n);
else if(n->type == intExp) affiche_t_intExp(n);
else if(n->type == appelExp) affiche_t_appelExp(n);
else if(n->type == lireExp) affiche_t_lireExp(n);
}
/*-------------------------------------------------------------------------*/
void affiche_t_varExp(n_exp *n)
{
char *fct = "varExp";
affiche_t_var(n->u.var);
}
/*-------------------------------------------------------------------------*/
void affiche_t_opExp(n_exp *n)
{
char *fct = "opExp";
/*if(n->u.opExp_.op == plus) affiche_t_texte("plus", trace_abs);
else if(n->u.opExp_.op == moins) affiche_t_texte("moins", trace_abs);
else if(n->u.opExp_.op == fois) affiche_t_texte("fois", trace_abs);
else if(n->u.opExp_.op == divise) affiche_t_texte("divise", trace_abs);
else if(n->u.opExp_.op == egal) affiche_t_texte("egal", trace_abs);
else if(n->u.opExp_.op == diff) affiche_t_texte("diff", trace_abs);
else if(n->u.opExp_.op == inf) affiche_t_texte("inf", trace_abs);
else if(n->u.opExp_.op == infeg) affiche_t_texte("infeg", trace_abs);
else if(n->u.opExp_.op == ou) affiche_t_texte("ou", trace_abs);
else if(n->u.opExp_.op == et) affiche_t_texte("et", trace_abs);
else if(n->u.opExp_.op == non) affiche_t_texte("non", trace_abs);
if( n->u.opExp_.op1 != NULL ) {
affiche_t_exp(n->u.opExp_.op1);
}
if( n->u.opExp_.op2 != NULL ) {
affiche_t_exp(n->u.opExp_.op2);
}*/
}
/*-------------------------------------------------------------------------*/
void affiche_t_intExp(n_exp *n)
{
char texte[ 50 ]; // Max. 50 chiffres
sprintf(texte, "%d", n->u.entier);
//affiche_t_element( "intExp", texte, trace_abs );
}
/*-------------------------------------------------------------------------*/
void affiche_t_lireExp(n_exp *n)
{
char *fct = "lireExp";
}
/*-------------------------------------------------------------------------*/
void affiche_t_appelExp(n_exp *n)
{
char *fct = "appelExp";
affiche_t_appel(n->u.appel);
}
/*-------------------------------------------------------------------------*/
void affiche_t_l_dec(n_l_dec *n)
{
char *fct = "l_dec";
if( n ){
affiche_t_dec(n->tete);
affiche_t_l_dec(n->queue);
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_dec(n_dec *n)
{
if(n){
if(n->type == foncDec) {
affiche_t_foncDec(n);
}
else if(n->type == varDec) {
affiche_t_varDec(n);
}
else if(n->type == tabDec) {
affiche_t_tabDec(n);
}
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_foncDec(n_dec *n)
{
char *fct = "foncDec";
//affiche_t_texte( n->nom, trace_abs );
affiche_t_l_dec(n->u.foncDec_.param);
affiche_t_l_dec(n->u.foncDec_.variables);
affiche_t_instr(n->u.foncDec_.corps);
}
/*-------------------------------------------------------------------------*/
void affiche_t_varDec(n_dec *n)
{
//affiche_t_element("varDec", n->nom, trace_abs);
if(rechercheDeclarative(n->nom) >= 0) {
printf("Variable %s déjà déclaré\n", n->nom);
exit(1);
}
if (contexte == C_VARIABLE_GLOBALE) {
ajouteIdentificateur(n->nom, contexte, T_ENTIER, adresseGlobalCourante, -1);
adresseGlobalCourante += 4;
} else if (contexte == C_VARIABLE_LOCALE) {
ajouteIdentificateur(n->nom, contexte, T_ENTIER, adresseLocaleCourante, -1);
adresseLocaleCourante += 4;
} else if (contexte == C_ARGUMENT) {
ajouteIdentificateur(n->nom, contexte, T_ENTIER, adresseArgumentCourant, -1);
adresseArgumentCourant += 4;
} else {
printf("Wtf ? %s\n", n->nom);
exit(1);
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_tabDec(n_dec *n)
{
char texte[100]; // Max. 100 chars nom tab + taille
sprintf(texte, "%s[%d]", n->nom, n->u.tabDec_.taille);
if(rechercheDeclarative(n->nom) >= 0) {
printf("Tableau %s déjà déclaré\n", n->nom);
exit(1);
}
if (contexte == C_VARIABLE_GLOBALE) {
ajouteIdentificateur(n->nom, contexte, T_ENTIER, adresseGlobalCourante, n->u.tabDec_.taille);
adresseGlobalCourante += 4*n->u.tabDec_.taille;
} else {
printf("Wtf ? %s\n", n->nom);
exit(1);
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_var(n_var *n)
{
if(n->type == simple) {
affiche_t_var_simple(n);
}
else if(n->type == indicee) {
affiche_t_var_indicee(n);
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_var_simple(n_var *n)
{
int i = rechercheExecutable(n->nom);
if( i < 0) {
printf("Variable %s non déclaré\n", n->nom);
exit(1);
} else if (dico.tab[i].type == T_TABLEAU_ENTIER) {
printf("Indice tableau %s est attendu\n", n->nom);
exit(1);
}
}
/*-------------------------------------------------------------------------*/
void affiche_t_var_indicee(n_var *n)
{
char *fct = "var_indicee";
affiche_t_exp( n->u.indicee_.indice );
}
/*-------------------------------------------------------------------------*/

View File

@@ -3,6 +3,7 @@
#include "analyseur_lexical.h"
#include "analyseur_syntaxyque.h"
#include "affiche_arbre_abstrait.h"
#include "affiche_table_symbole.h"
#include "symboles.h"
extern int uniteCourante;
@@ -19,22 +20,24 @@ int main(int argc, char **argv) {
exit(1);
}
initialise_premiers();
initialise_suivants();
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 if (!strcmp(argv[1], "-t")) {
uniteCourante = yylex();
n_prog *p = programme();
affiche_t_table(p);
} else {
fprintf(stderr, "option Inconue");
exit(1);

97
src/dico.c Normal file
View File

@@ -0,0 +1,97 @@
#include "dico.h"
/*-------------------------------------------------------------------------*/
void entreeFonction(void){
dico.base = dico.sommet;
contexte = C_VARIABLE_LOCALE;
adresseLocaleCourante = 0;
adresseArgumentCourant = 0;
}
/*-------------------------------------------------------------------------*/
void sortieFonction(void){
dico.sommet = dico.base;
dico.base = 0;
contexte = C_VARIABLE_GLOBALE;
}
/*-------------------------------------------------------------------------*/
int ajouteIdentificateur(char *identif, int classe, int type, int adresse, int complement)
{
dico.tab[dico.sommet].identif = strdup(identif);
dico.tab[dico.sommet].classe = classe;
dico.tab[dico.sommet].type = type;
dico.tab[dico.sommet].adresse = adresse;
dico.tab[dico.sommet].complement = complement;
dico.sommet++;
if(dico.sommet == maxDico){
fprintf(stderr, "attention, plus de place dans le dictionnaire des \
identificateurs, augmenter maxDico\n");
exit(1);
}
/* affiche_dico(); */
return dico.sommet - 1;
}
/*-------------------------------------------------------------------------*/
int rechercheExecutable(char *identif)
{
int i;
for(i = dico.sommet - 1; i >= 0; i--){
if(!strcmp(identif, dico.tab[i].identif))
return i;
}
return -1;
}
/*-------------------------------------------------------------------------*/
int rechercheDeclarative(char *identif)
{
int i;
for(i = dico.base; i < dico.sommet; i++){
if(!strcmp(identif, dico.tab[i].identif))
return i;
}
return -1;
}
/*-------------------------------------------------------------------------*/
void affiche_dico(void)
{
int i;
printf("------------------------------------------\n");
printf("base = %d\n", dico.base);
printf("sommet = %d\n", dico.sommet);
for(i=0; i < dico.sommet; i++){
printf("%d ", i);
printf("%s ", dico.tab[i].identif);
if(dico.tab[i].classe == C_VARIABLE_GLOBALE)
printf("GLOBALE ");
else
if(dico.tab[i].classe == C_VARIABLE_LOCALE)
printf("LOCALE ");
else
if(dico.tab[i].classe == C_ARGUMENT)
printf("ARGUMENT ");
if(dico.tab[i].type == T_ENTIER)
printf("ENTIER ");
else if(dico.tab[i].type == T_TABLEAU_ENTIER)
printf("TABLEAU ");
/* else if(dico.tab[i].type == _ARGUMENT) */
/* printf("ARGUMENT "); */
else if(dico.tab[i].type == T_FONCTION)
printf("FONCTION ");
printf("%d ", dico.tab[i].adresse);
printf("%d\n", dico.tab[i].complement);
}
printf("------------------------------------------\n");
}

View File

@@ -1,7 +1,7 @@
#include<stdio.h>
#include<stdlib.h>
#include"util.h"
#include"syntabs.h"
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include "syntabs.h"
n_appel *cree_n_appel(char *fonction, n_l_exp *args)
{

View File

@@ -1,13 +1,13 @@
------------------------------------------
base = 2
sommet = 2
0 $tab GLOBALE TABLEAU 0 -1
0 $tab GLOBALE TABLEAU 0 10
1 initialiser GLOBALE FONCTION 0 0
------------------------------------------
------------------------------------------
base = 3
sommet = 5
0 $tab GLOBALE TABLEAU 0 -1
0 $tab GLOBALE TABLEAU 0 10
1 initialiser GLOBALE FONCTION 0 0
2 afficher GLOBALE FONCTION 0 1
3 $n ARGUMENT ENTIER 0 -1
@@ -16,7 +16,7 @@ sommet = 5
------------------------------------------
base = 4
sommet = 7
0 $tab GLOBALE TABLEAU 0 -1
0 $tab GLOBALE TABLEAU 0 10
1 initialiser GLOBALE FONCTION 0 0
2 afficher GLOBALE FONCTION 0 1
3 echanger GLOBALE FONCTION 0 2
@@ -27,7 +27,7 @@ sommet = 7
------------------------------------------
base = 5
sommet = 9
0 $tab GLOBALE TABLEAU 0 -1
0 $tab GLOBALE TABLEAU 0 10
1 initialiser GLOBALE FONCTION 0 0
2 afficher GLOBALE FONCTION 0 1
3 echanger GLOBALE FONCTION 0 2
@@ -40,7 +40,7 @@ sommet = 9
------------------------------------------
base = 6
sommet = 6
0 $tab GLOBALE TABLEAU 0 -1
0 $tab GLOBALE TABLEAU 0 10
1 initialiser GLOBALE FONCTION 0 0
2 afficher GLOBALE FONCTION 0 1
3 echanger GLOBALE FONCTION 0 2

View File

@@ -13,7 +13,7 @@ MYCOMPILO="../compilateur-l"
MYCOMPILOLEX="${MYCOMPILO} -l"
MYCOMPILOSYNT="${MYCOMPILO} -s"
MYCOMPILOASYNT="${MYCOMPILO} -a"
#MYCOMPILOTAB="${MYCOMPILO} -t"
MYCOMPILOTAB="${MYCOMPILO} -t"
#MYCOMPILOMIPS="${MYCOMPILO} -m"
################################################################################
@@ -70,8 +70,8 @@ function test_fichier_ok() {
diff_prog ${XMLDIFF} $input synt
${MYCOMPILOASYNT} input/$input.l > output/$input.asynt
diff_prog ${XMLDIFF} $input asynt
#${MYCOMPILOTAB} input/$input.l > output/$input.tab
#diff_prog "${REGDIFF}" $input tab
${MYCOMPILOTAB} input/$input.l > output/$input.tab
diff_prog "${REGDIFF}" $input tab
#${MYCOMPILOMIPS} input/$input.l > output/$input.mips
#diff_prog "${REGDIFF}" $input mips
else