/*#include "ptg_gen.h"*/ struct nliste { int val; struct nliste * ptr; }; typedef /*struct nliste * */ int ptrnliste; ptrnliste liste0() {return 0;} ptrnliste liste1(int i) { struct nliste * p; p=malloc(sizeof (struct nliste)); p->val=i; p->ptr=0; return p;} ptrnliste concat(ptrnliste p, ptrnliste q) { struct nliste *P=p; return (p==0)? q : liste2(P->val,concat(P->ptr,q)); } ptrnliste liste2(int i, ptrnliste q) { struct nliste * p; p=malloc(sizeof (struct nliste)); p->val=i; p->ptr=q; return p;} ptrnliste copy (ptrnliste p) { struct nliste *P=p; if (p==0) return 0; return liste2(P->val,copy(P->ptr)); } void pliste(struct nliste * p) { if (p) {printf( "liste element: %i\n",p->val); if (p->ptr) pliste(p->ptr); else printf("\n"); } } int INC(int i) { return i+1; } int SAME(int i) {return i; } typedef struct{ int decalage,niveau,index;} varinfo; typedef struct symtab { struct symtab *pere; varinfo * vars; int taille; } symtab; void debug(symtab * table) {varinfo *p; printf("Symbol table \n"); if (table==0) {printf("empty\n");return;} p=table->vars; while (p->index){ printf("variable number %i at level %i offset %i\n",p->index, p->niveau,p->decalage); p++; } printf("Symbol table completed\n"); } void inserer(int index, int niveau, int decalage, symtab *T) {int debut,point; varinfo *p; p=T->vars; if (T==0) {printf("Error in Inserer; table is NULL\n");return;} debut=hache(index,T->taille); for (point=debut;;point=(point+1)%(T->taille)) { if (p[point].index==index) {printf("Error in Inserer; duplicate\n"); return ; } if ((point+1)%(T->taille)==debut) {printf("Error in Inserer; full\n"); return ; } if (p[point].index==0){ p[point].index=index; p[point].niveau=niveau; p[point].decalage=decalage; return; } } } symtab *construire (symtab * p, int niv, struct nliste *pliste, int dec) { symtab *res; int i=0; res=malloc(sizeof (symtab)); res->pere=p; res->taille=longueur(pliste)*5/4+1; res->vars=malloc(sizeof(varinfo)*((longueur(pliste)*5/4)+1)); for (i=0;itaille;i++) res->vars[i].index=0; while (pliste) { inserer(pliste->val,niv,dec++,res); pliste=pliste->ptr; /*res->vars[i].index=pliste->val; res->vars[i].niveau=niv; res->vars[i].decalage=dec+i++;*/ } return res; } int hache(int i, int s){ return i%s; } varinfo * recherche(symtab * T, int index) { varinfo *p; int debut,point; /*debug(T);*/ if (T==0) return 0; p=T->vars; debut=hache(index,T->taille); for (point=debut;;point=(point+1)%(T->taille)) { if (p[point].index==index) return &p[point]; if ((point+1)%(T->taille)==debut) break; } return recherche(T->pere,index); } void Afficher (int index, varinfo * trouve) { if (trouve) PTGOut(PTGVariable(index, trouve->niveau, trouve->decalage)); else PTGOut(PTGError(index)); } int longueur(struct nliste *p) { return (p==0)?0 : 1+longueur(p->ptr);}