First of all, I'm very new to flex and bison and I really can't seem to solve this problem.
I have created both flex and bison files and it the compilation works fine here are both my flex and bison files
(ps the comments are in french) Flex:
%{
// Définitions en language C
#include"minilang.tab.h"
extern int yylval;
extern int nbrligne;
%}
// les définitions des expressions régulières
/*
Définitions de la partie
"Liste Declarations" du language MiniLang
qui inclut les déclarations du language Minilang
*/
chiffre [0-9]
vide [ \t]+|" "+
saut_ligne [\n]+
// les Nombres (valeurs)
integer [- | +]?([1-9][0-9]*|0)
float [- | +]?([1-9][0-9]*|0)\.[0-9]*[1-9]
bool ("true"|"false"|"TRUE"|"FALSE")
constant integer|float
// Définitions
varint "INT"|"int"
varfloat "FLOAT"|"float"
varbool "bool"|"BOOL"
const "CONST"|"const"
comment "{"[^}]*"}"
// Déclarations des éléments du language
// IDF à revoir
idf ([A-Z]([_]?[a-z0-9])*){1,11}
affectation "="
semicolon ";"
vg ","
plus "++"
minus "--"
beginmc "begin"|"BEGIN"
end "end"|"END"
/*
Définitions de la partie
"List Instructions" du language MiniLang
qui inclut les instructions du language Minilang
*/
// Affectation
op "+"|"-"|"*"|"/"|"&&"|"||"
// Condition if
if "if"|"IF"|"If"
comp "=="|"<"|"<="|"<>"|">"|">="
// For loop
for "for"|"FOR"
// Common
paropen "("
parclose ")"
curlopen "{"
curlclose "}"
%%
// Expression Régulière { Action C}
{chiffre} {return token_chiffre;}
{vide}
{saut_ligne} {nbrligne++;}
{integer} { yylval = atoi(yytext); return token_integer;}
{float} { yylval = atof(yytext); return token_float;}
{bool} {return token_bool;}
{varint} {return token_varint;}
{varfloat} {return token_varfloat;}
{varbool} {return token_varbool;}
{const} {return token_const;}
{comment} {return token_comment;}
{idf} {return token_idf;}
{affectation} {return token_affectation;}
{semicolon} {return token_semicolon;}
{vg} {return token_vg;}
{plus} {return token_plus;}
{minus} {return token_minus;}
{beginmc} {return token_begin;}
{end} {return token_end;}
{op} {return token_op;}
{if} {return token_if;}
{comp} {return token_comp;}
{for} {return token_for;}
{paropen} {return token_paropen;}
{parclose} {return token_parclose;}
{curlopen} {return token_curlopen;}
{curlclose} {return token_curlclose;}
{constant} {return token_constant;}
. {printf("\nErreur lexicale a la ligne %d ",nbrligne);}
%%
and bison:
%{
#include <stdio.h>
#include<stdlib.h>
int nbrligne=0;
int yylex();
void yyerror(const char *s);
%}
// Token definitions
%token token_chiffre
%token token_vide
%token token_integer
%token token_float
%token token_bool
%token token_varint
%token token_varfloat
%token token_varbool
%token token_const
%token token_comment
%token token_idf
%token token_affectation
%token token_semicolon
%token token_vg
%token token_plus
%token token_minus
%token token_begin
%token token_end
%token token_op
%token token_if
%token token_comp
%token token_for
%token token_paropen
%token token_parclose
%token token_curlopen
%token token_curlclose
%token token_constant
%%
Prog: DecList token_begin InstList token_end|;
DecList: Declaration DecList|Declaration | token_comment DecList | token_comment;
Declaration: ConstIntDec | ConstFloatDec | ConstBoolDec | IntDec | FloatDec | BoolDec;
ConstIntDec: token_const token_varint MultiIdfInt token_semicolon;
ConstFloatDec: token_const token_varfloat MultiIdfFloat token_semicolon;
ConstBoolDec: token_const token_varbool MultiIdfBool token_semicolon;
IntDec: token_varint MultiIdfInt token_semicolon;
FloatDec: token_varfloat MultiIdfFloat token_semicolon;
BoolDec: token_varbool MultiIdfBool token_semicolon;
MultiIdfInt: token_idf token_vg MultiIdfInt | token_idf | token_idf token_affectation token_integer MultiIdfInt ;
MultiIdfFloat: token_idf token_vg MultiIdfFloat | token_idf | token_idf token_affectation token_integer MultiIdfFloat ;
MultiIdfBool: token_idf token_vg MultiIdfBool | token_idf | token_idf token_affectation token_integer MultiIdfBool ;
InstList: Instruction InstList | Instruction | token_comment InstList | token_comment;
Instruction: Boucle | Affectation | Condition;
Affectation: token_idf token_affectation Exp token_semicolon | Incrementation;
Incrementation: token_constant token_plus | token_constant token_minus;
Exp: token_idf token_op Exp | token_idf | ExpConst;
ExpConst: token_integer token_op ExpConst | token_float token_op ExpConst | token_bool token_op ExpConst
| token_bool
| token_constant;
Condition: token_if token_paropen ExpCond token_parclose token_curlopen InstList token_curlclose;
ExpCond: token_idf token_comp token_idf
| token_idf token_comp token_constant
| token_idf token_comp token_bool
| token_constant token_comp token_idf
| token_bool token_comp token_idf
| token_constant token_comp token_constant
| token_bool;
Boucle: token_for token_paropen Affectation token_vg ExpCond token_vg Incrementation token_parclose token_curlopen InstList token_curlclose;
%%
#include"lex.yy.c"
int main() {
yyparse();
return yylex();
}
void yyerror(const char *s){ printf("\nERROR %d\n",nbrligne); }
int yywrap(){ return 1; }
// int yywrap(void){
// return 1;
// }
And here are the commands I ran to compile both of them and execute the compiler
flex minilang.l
bison -d minilang.y
gcc -o compiler minilang.tab.c
test.minilang is a file that I created that is supposed to be the same language this compiler is supposed to interpret here are his contents
int K_ms;
BEGIN
K_ms=16;
END
the error produced with this code is "ERROR 1" which means it happened on the first line and I don't understand where is the error in my code the language is supposed to look like this:
// List of Variable Declarations
BEGIN
// List of Instructions
END
vide
rule contains a redundancy. You should not return comments to the parser. Treat them like whitespace, and don't mention them in productions. – user207421yyleng
). What he's saying is that you shouldn't try to restrict it with a pattern. – rici