1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| %{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "bis.tab.h"
int yyerror (char*);
int nbrligne=1;
typedef struct elem * liste;
typedef struct elem{char *entite; char *type; int ligneA; liste svt}elem;
liste table_symboles = NULL;
/*recherche*/
liste recherche(char *entite) {
liste p;
/* recherche dans la table des symboles */
p = table_symboles;
while (p) {
if ( strcmp( p->entite,entite)==0)
{fprintf(stderr,"%s\n","Variable '%s' deja declarée à la linge %d\n", entite ,p->ligneA);
return p;
}
p = p->svt;
}
return NULL;
}
/*verification*/
void verif(char *entite){
liste p= recherche(entite);
if (p==NULL)
printf("IDENT INCONNU:%s",entite);
else
printf("IDENT CONNU:%s",entite);
}
/*insert*/
void insert(char *entite,char *type){
liste p= recherche(entite);
/* creation d'un noeud */
if (p == NULL) {
p = (liste) malloc(sizeof(elem));
strncpy(p->entite, entite, 32);
p->entite[31] = '\0'; /* si strlen(entite) > 31 */
strcpy(p->type,type);
p->ligneA=nbrligne;
p->svt=NULL;
/* insertion du noeud en tête de liste (LIFO) */
p->svt = table_symboles;
table_symboles = p;
}
}
/*affichage*/
void afficherts(){
liste p = table_symboles;
printf("table\n");
while(p != NULL){
fprintf(stderr,"%s\n","idf: %s type: %s declaré en ligne :%d",p->entite, p->type,p->ligneA);
printf("\n");
p = p->svt;
}
}
%}
lettre [a-zA-Z]
chiffre [0-9]
IDF (({lettre}+)({lettre}*|{chiffre}*|(-?))({lettre}+|{chiffre}+))
constE ({chiffre}+)
constEsignee [\+|\-]{constE}
constR {chiffre}+.?{chiffre}+
constRsignee [\+|\-]{constR}
coms $[.]$
%%
"INT" return (INT);
"REAL" return (REAL);
"BOOLEAN" return (BOOLEAN);
"STRING" return (STRING);
"CHAR" return (CHAR);
"," return(VERGULE);
";" return(POINTVERGULE);
"=" return (EGAL);
"<>" return (DIFF);
":=" return(AFFECT);
"." return (POINT);
"CODE" return(CODE);
"START" return(START);
"END" return(END);
"CONST" return (CONST);
"WHILE" return (WHILE);
"EXECUTE" return (EXECUTE);
"WHENE" return (WHENE);
"DO" return (DO);
"OTHERWISE" return (OTHERWISE);
"+" return (SOM);
"-" return (SOUS);
"*" return (MUL);
"/" return (DIV);
{lettre} {return (IDF);}
{lettre}- {yyerror("erreur :un idf ne se termine pas par -");}
{IDF} if (yyleng>20) {yyerror("erreur:IDF trop long");} else {return (IDF) ;}
{IDF}- {yyerror("erreur :un idf ne se termine pas par -");}
{constE} if (yyleng>6) {printf("erreur:constanteE trop long");} else {return (CONSTE);}
{constEsignee} if (yyleng>6) { yyerror("erreur:constanteE trop long");} else {return (CONSTES);}
{constR} if (yyleng>12) {yyerror("erreur:constanteR trop long");} else {return (CONSTR);}
{constRsignee} if (yyleng>12) {yyerror("erreur:constanteE trop long");} else {return (CONSTRS);}
{coms} {return (COMS);}
%%
int yywrap()
{
return 1;
}
int yyerror ( char* m ) {
printf ( "%s\n", m );
return 2;
} |
Partager