0
votes

So I am new to flex & bison, and I am trying to build a simple C-like lexical & syntactical analyzer, but I am getting these errors and I can't really make heads or tails from it. The errors generated from the auto generated files indicate that there is a token redefinition in y.tab.c & lex.yy.c

Here are the files: test.l

%{
#include <stdio.h>
#include <string.h>
#include "y.tab.h"

int yylex();
void yyerror(const char *s);

%}

%%
"program"                      { return PROGRAM ; } 
"function"                     { return FUNCTION ; }
"integer"                      { return INTEGER ; }
"boolean"                      { return BOOLEAN ; }
"if"                           { return IF ; }
"else"                         { return ELSE ; }
"end"                          { return END ; }
"or"                           { return OR ; }
"and"                          { return AND ; }
"div"                          { return DIV ; }
"mod"                          { return MOD ; }
"not"                          { return NOT ; }
"("                            { return LPARENTHESIS ; }
")"                            { return RPARENTHESIS ; }
";"                            { return SEMI ; }
","                            { return COMA ; }
"<="                           { return LESSEQ ; }
">="                           { return GREATEREQ ; }
"+"                            { return PLUS ; }
"-"                            { return MINUS ; }
"*"                            { return MUL ; }
"="                            { return EQUAL ; }
"!>"                           { return NEQUAL ; }
"<"                            { return LESS ; }
">"                            { return GREATER ; }
":="                           { return ASSIGN ; }
"true"                         { return TRUE ; }
"false"                        { return FALSE ; }
"{"                            { return LCURLYBRAKET ; }
"}"                            { return RCURLYBRAKET ; }
"void"                         { return VOID; }
%%

int main(int argc, char* argv) 
{
    int token;
    while ((token = yylex()) != 0) 
    {
        printf("Token: %d\n", token);
    }
    return 0;
}


void yyerror(const char *s) 
{
    printf("yyerror has: %s", s);
}

test2.y

%token PROGRAM

%token FUNCTION

%token EXTERN

%token LPARENTHESIS
%token RPARENTHESIS
%token LCURLYBRAKET
%token RCURLYBRAKET
%token SEMI
%token COMA

%token VOID
%token INTEGER
%token BOOLEAN
%token STRING
%token INTCONST
%token CHARCONST

%token BEGIN
%token END

%token IF
%token ELSE


%token RETURN
%token TRUE
%token FALSE

%token ID

%right ASSIGN
%left OR AND
%left EQUAL NEQUAL
%left GREATER GREATEREQ LESS LESSEQ
%left NOT
%left MINUS PLUS
%left DIV MOD MUL

//%start program

%%

program    : ext_decl head def com;
ext_decl   : | ext_proto ext_decl;
ext_proto  : EXTERN func_proto;
head       : VOID ID LPARENTHESIS RPARENTHESIS;
def        : | definition def;
definition : def_var | def_func | func_proto;
def_var    : data_type var_list;
data_type  : INTEGER | BOOLEAN | STRING;
var_list   : ID dummy_ex;
dummy_ex   : | COMA ID dummy_ex; 
def_func   : head_func def com;
func_proto : head_func SEMI;
head_func  : func_type ID LPARENTHESIS dummy RPARENTHESIS;
dummy      : | std_par_list;
func_type  : INTEGER | BOOLEAN | VOID;
std_par_list : typical_par dummy_par;
dummy_par    : | COMA typical_par dummy_par;
typical_par  : data_type dummy_amb ID;
dummy_amb    : | AND;
com       : BEGIN dummy_com END;
dummy_com : | command dummy_com;
command   : simple_com COMA | structured_com | complex_com;
complex_com : LCURLYBRAKET dummy_com RCURLYBRAKET;
structured_com : if_com;
simple_com : assign | func_call | return_com | null_com; 
if_com : IF LPARENTHESIS gen_ex RPARENTHESIS command dummy_else;
dummy_else  : | else_clause;
else_clause : ELSE command;
assign    : ID ASSIGN gen_ex;

func_call : ID LPARENTHESIS dummy_true_par RPARENTHESIS;
dummy_true_par : | gen_ex COMA gen_ex dummy_true_par;

return_com : RETURN dummy_ret;
dummy_ret  : | gen_ex;
null_com   : ;
gen_ex     : gen_term dummy_term;
dummy_term : | OR OR gen_term dummy_term;
gen_term   : gen_factor dummy_factor;
dummy_factor : | AND AND gen_factor dummy_factor;
gen_factor   : dummy_not gen_first_factor;
dummy_not  : | NOT;
gen_first_factor : simple_ex dummy_compare;
dummy_compare : | compare_sect;
compare_sect  : comp_op simple_ex;
comp_op   : EQUAL | NEQUAL | LESS | GREATER | LESSEQ | GREATEREQ;
simple_ex : simple_term dummy_s;
dummy_s   : | choice simple_term dummy_s;
choice : PLUS | MINUS;
simple_term : simple_factor dummy_t;
dummy_t   : | choice2 simple_factor dummy_t;
choice2   : MUL | DIV | MOD;
simple_factor : choice simple_first_term;
simple_first_term : ID | const | func_call | LPARENTHESIS gen_ex RPARENTHESIS;
const : INTCONST | CHARCONST | TRUE | FALSE;

%%

and here is the script I use to run them. make

flex test.l
bison -y -d test2.y 
gcc y.tab.c lex.yy.c

& here are the errors:

enter image description here

P.S. I know there are some conflicts, but I don't really think they have somethink to do with the errors I am getting so far. Correct me if I am wrong.

Thanks for your time.

1

1 Answers

4
votes

The token BEGIN is internally translated to a macro (eg #define BEGIN 273) In Flex there is already a macro "BEGIN" to switch start conditions.

So you get a redefine error. Solution change the name of the BEGIN token.

To avoid the warnings, you can declare the functions yylex and yyerror in the beginning of bison.

%{
int yylex();
void yyerror(char* s);
%}

To remove the shift-reduce and reduce-reduce conflicts, you have to analyse the grammar. Try bison -v f.y to create a y.output file that has some information about the conflicts, and the LaLR automata. One initial tip: left recursive productions normally reduce the number of LR conflicts. Example: in

var_list   : ID dummy_ex;
dummy_ex   : | COMA ID dummy_ex

try dummy_ex : | dummy_ex COMA ID or even better, reduce the "dummy..." ☺

var_list   : ID
           | var_list ',' ID 

Your current main is just testing the lex-analyser. That may be useful for a starting point. In the final you need to replace it with a main in bison calling yyparse()