Search code examples
compiler-errorscompiler-constructionbisonflex-lexer

problems with flex and bison


I'm trying to create a compiler in flex and bison, but unfortunately I'm finding some problem.

When I try to compile the compiler gives me these types of error:

flex.lex.c:286:37: error: expected ‘)’ before ‘->’ token
 #define YY_CURRENT_BUFFER_LVALUE yyg->yy_buffer_stack[yyg->yy_buffer_stack_top]
flex.lex.c:139:19: note: in expansion of macro ‘YY_CURRENT_BUFFER_LVALUE’ #define yylineno (YY_CURRENT_BUFFER_LVALUE->yy_bs_lineno)
dichiarazioni.h:2:12: note: in expansion of macro ‘yylineno’
extern int yylineno; /* from lexer */
flex.lex.c:134:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token #define yyin yyg->yyin_r
dichiarazioni.h:5:14: note: in expansion of macro ‘yyin’
 extern FILE *yyin;
make: *** [<incorporato>: flex.lex.o] Error 1

Then I show you three part of file of my program (flex.l bison.y and dichiarazioni.h).

flex.l:

  %option header-file="flex.lex.h"
  %option reentrant bison-bridge
  %{
    #include "bison.tab.h"
    #include "dichiarazioni.h"
  %}

   %%

    "char"  { yylval->fn = 10; return TYPE; }
    "int"   { yylval->fn = 11; return TYPE; }
    "if"|"IF"               { return IF; }
    "else"|"ELSE"           { return ELSE; }

    "inc"                   { yylval->fn = 24; return OPERATORE; }
    "dec"                   { yylval->fn = 25; return OPERATORE; }
    "set"                   { yylval->fn = 26; return OPERATORE; } 

     etc etc
     %%

bison.y:

 %{
 #include <stdio.h>
 #include <stdlib.h>
 %}

 %{
 #include "flex.lex.h"
 #include "dichiarazioni.h"
 %}

 %define api.value.type {union YYSTYPE}

%token NUMBER
%token <fn> TYPE
%token SEMI
%token ALPHA
%token ELIF
%token <fn> CMP
%token <fn> OPERATORE
%token <fn> PRINT
.......

%type <fn> term NUMBER
%type <op> operazioni
%type <c> ALPHA string
.....
%start main

%%

string: ALPHA 
;
.....
%%

dichiarazioni.h:

extern int yylineno; /* from lexer */
void yyerror(char *s, ...);
extern int yyparse();
extern FILE *yyin;
//extern int yylex();

union YYSTYPE{
    int fn;
    char* c;
    struct operazioni* op;
    ....
 };

Solution

  • You're using %option bison-bridge without using %pure-parser in the bison parser, which won't work -- bison-bridge is for connecting with the (non standard) reentrant API used by bison's %pure-parser option.

    Either get rid of %option bison-bridge from the .l file or add %pure-parser to the .y file. If you do the latter, you'll also need to change your .h file as well (yyerror/yyparse change, yyin and yylineno go away).