i am currently doing an assignment where i have to make a mini compiler for a programing language using flex and bison when testing the parser generated by flex and bison with a program test it keeps giving me syntax error while the test i wrote should have a correct syntax
here is the flex code
%{
#include"synt.tab.h"
extern nb_ligne;
extern col;
int i;
%}
lettre [a-zA-Z]
chiffre[0-9]
IDF {lettre}({lettre}|{chiffre})*
CST_INT ([+-]?[1-9]{chiffre}*)|0
CST_REAL {CST_INT}"."({chiffre}+)
CST_CHAR \'.\'
CST_CHAINE \".+\"
CST_LOGIC TRUE|FALSE
COMMENT "%"(([^";"]|\n)*";")
%%
{COMMENT} {
for(i=0;i<strlen(yytext);i++){if(yytext[i] == '\n' ){nb_ligne ++; col = 1; } col++; }}
PROGRAM col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_program;
ROUTINE col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_routine;
END col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_end ;
ENDR col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_endr ;
LOGICAL col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_logical;
CHARACTER col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_character;
INTEGER col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_integer ;
REAL col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_real;
DIMENSION col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_dimension;
READ col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_read ;
WRITE col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_write ;
IF col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_if;
THEN col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_then;
ELSE col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_else ;
ENDIF col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_endif ;
DOWHILE col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_dowhile;
ENDDO col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_enddo ;
CALL col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_call ;
EQUIVALENCE col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_equivalence;
OR col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_or ;
AND col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_and ;
GT col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_gt ;
GE col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_ge ;
EQ col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_eq ;
NE col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_ne ;
LE col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_le ;
LT col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_lt ;
"+" col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_add;
"-" col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_sub ;
"*" col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_mult ;
"/" col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_div ;
"=" col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_aff;
"(" col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_par_ouv;
")" col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_par_fer;
";" col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_pvg ;
"," col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_vg ;
"." col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return mc_pt ;
{CST_LOGIC} col+=strlen(yytext); printf("l'entite lexicale reconnue est %s mc\n",yytext); return cst_logic;
{IDF} { col+=strlen(yytext);
if(yyleng>10) printf("erreur: ligne %d et la colonne %d idf long\n",nb_ligne,col);
else{ printf("l'entite lexicale reconnue est %s IDF\n",yytext); return idf;}}
{CST_INT} { col+=strlen(yytext);
if(atoi(yytext)<=32767 && atoi(yytext)>= -32768){ printf ("l'entite lexicale reconnue est %s cst int\n",yytext);}
else { printf(" erreur: la ligne %d et la colonne %d depassement de la capacite\n ",nb_ligne,col); return cst_int;}}
{CST_REAL} { col+=strlen(yytext);
if(atoi(yytext)<=32767 && atoi(yytext)>= -32768){ printf ("l'entite lexicale reconnue est %s cst real\n",yytext);}
else { printf(" erreur: la ligne %d et la colonne %d depassement de la capacite\n",nb_ligne,col); return cst_real;}}
{CST_CHAR} {col+=strlen(yytext); printf ("l'entite lexicale reconnue est %s cst char\n",yytext); return cst_char;}
{CST_CHAINE} {col+=strlen(yytext); printf ("l'entite lexicale reconnue est %s cst chaine\n",yytext); return cst_chaine;}
[ \t]
\n { col=1; nb_ligne++; }
. printf("erreur lexical a la ligne %d et la col %d\n",nb_ligne,col);
%%
and here is the bison code
%{
int nb_ligne=1; int col=1;
%}
%token mc_program mc_routine mc_end mc_endr mc_logical mc_character mc_integer mc_real mc_dimension mc_read mc_write mc_if mc_then mc_else mc_endif mc_dowhile mc_enddo mc_call mc_equivalence mc_or mc_and mc_gt mc_ge mc_eq mc_ne mc_lt mc_le mc_add mc_sub mc_mult mc_div mc_aff mc_par_ouv mc_par_fer mc_pvg mc_vg mc_pt cst_logic idf cst_int cst_real cst_char cst_chaine
%start S
%%
S: LIST_FONC mc_program idf DEC LIST_INST mc_end {printf("prog syntaxiquement correct");YYACCEPT;}
;
LIST_FONC: DEC_FONC LIST_FONC|DEC_FONC
;
DEC_FONC:
TYPE mc_routine idf mc_par_ouv LIST_PAR mc_par_fer DEC LIST_INST AFF mc_endr
;
TYPE:
mc_integer | mc_real | mc_character | mc_logical
;
LIST_PAR:
VAR mc_vg LIST_IDF|VAR
;
VAR:
idf|TAB|MAT
;
TAB: idf mc_par_ouv EXP mc_par_fer
;
MAT: idf mc_par_ouv EXP mc_vg EXP mc_par_fer
;
VAL_RET:cst_chaine|cst_char|EXP
;
DEC:
DEC DEC_VAR |DEC DEC_TAB |DEC DEC_MAT |DEC DEC_CH | DEC_VAR |DEC_TAB|DEC_MAT| DEC_CH
;
DEC_VAR:
TYPE LIST_IDF mc_pvg
;
LIST_IDF: idf mc_vg LIST_IDF |idf
;
DEC_TAB:
TYPE idf mc_dimension mc_par_ouv cst_int mc_par_fer mc_pvg
;
DEC_MAT:
TYPE idf mc_dimension mc_par_ouv cst_int mc_vg cst_int mc_par_fer mc_pvg
;
DEC_CH:
TYPE idf mc_mult cst_int mc_pvg
;
LIST_INST: LIST_INST INST| INST
;
INST:
AFF | ES | COND | BOUCLE | EQUIVALENCE
;
AFF:
idf mc_aff EXP mc_pvg | idf mc_aff cst_chaine mc_pvg | idf mc_aff cst_char mc_pvg| idf mc_aff cst_logic mc_pvg| idf mc_aff mc_call idf mc_par_ouv LIST_IDF mc_par_fer mc_pvg| idf mc_aff VAL_RET
;
EXP:
TERM | TERM mc_add EXP | TERM mc_sub EXP
;
TERM: FACT | FACT mc_mult TERM | FACT mc_div TERM
;
FACT: idf| cst_int | cst_real
;
ES:
mc_read mc_par_ouv idf mc_par_fer mc_pvg;| mc_write mc_par_ouv cst_chaine mc_vg idf mc_vg cst_chaine mc_par_fer mc_pvg
;
COND: mc_if mc_par_ouv EXP_COND mc_par_fer mc_then LIST_INST mc_else LIST_INST mc_endif
;
EXP_COND:
mc_par_ouv TERM_LOG mc_pt mc_or mc_pt EXP_COND mc_par_fer | mc_par_ouv TERM_LOG mc_pt mc_and mc_pt EXP_COND mc_par_fer|TERM_LOG
;
TERM_LOG: mc_par_ouv EXP mc_pt mc_eq EXP mc_par_fer |mc_par_ouv EXP mc_pt mc_ne EXP mc_par_fer |mc_par_ouv EXP mc_pt mc_le EXP mc_par_fer |mc_par_ouv EXP mc_pt mc_lt mc_pt EXP mc_par_fer |mc_par_ouv EXP mc_pt mc_ge EXP mc_par_fer |mc_par_ouv EXP mc_pt mc_gt EXP mc_par_fer
;
BOUCLE: mc_dowhile mc_par_ouv EXP_COND mc_par_fer LIST_INST mc_enddo
;
EQUIVALENCE: mc_equivalence mc_par_ouv LIST_IDF mc_par_fer mc_vg mc_par_ouv LIST_IDF mc_par_fer mc_pvg
%%
main()
{
yyparse();
}
yywrap()
{}
int yyerror(char *msg)
{ printf(" Erreur syntaxique a ligne : %d a la colonne %d ", nb_ligne,col);
return 1;
}
and here is a code sample that i tested with i tested more than this
INTEGER ROUTINE fonc(A,B)
A=B+1;
fonc=A;
ENDR
PROGRAM
projet
INTEGER d Dimension;
INTEGER C;
CHARACTER chaine;
CHARACTER char;
C=call fonc(c,d(2));
END
and here is the result result the semantics don't have to be right since i haven't done that part yet
i think its due the grammar being wrong since the lexer works fine on its own, how do i make sure my grammar is correct as i am a beginner