Ajout analyse sémantique :
- Verif declaration var & tab - Verif appel var
This commit is contained in:
@@ -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
339
src/affiche_table_symbole.c
Normal 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 );
|
||||
}
|
||||
/*-------------------------------------------------------------------------*/
|
||||
@@ -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
97
src/dico.c
Normal 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");
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user