Question

I am building a parser but I have some errors that I could not solve them, I am nuw to bison and flex, please help me solve them and understand why they are happening here is my errors that I get:

   lexical.l:3:20: error: common.h: No such file or directory
In file included from lexical.l:5:
bison.tab.h:81: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before âyylvalâ
bison.tab.c:1155: error: conflicting types for âyylvalâ
bison.tab.h:81: note: previous declaration of âyylvalâ was here
bison.y: In function âyyparseâ:
bison.y:96: error: incompatible types when assigning to type âSTYPEâ from type âNODEPTRâ

here is my parser file bison.y:

%{
#include <stdio.h>
#include "bison.tab.h"
#include "common.h"
//int yylex();
void yyerror (char const *);

typedef struct STYPE {
    NODEPTR pointer;
} STYPE;

#define YYSTYPE STYPE

%}



/* Bison declarations. */

%token ELSE REAL INTEGER XWRITE WHILE END DO IF THEN XPROGRAM FUNCTION XRETURN XREAD VAR FOR XBEGIN CALL ID NUM  
%token RELOP ADDOP MULOP ASSIGN AND OR NOT  
%left '-' '+'
%left '*' '/'
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
Was it helpful?

Solution

If you #define YYSTYPE in your bison file, you need to #define YYSTYPE also in your flex file, because bison doesn't put the #define into the generated header file. You need to do this before you #include the generated header file.

Bison doesn't put the #define in the generated header because it has no way of knowing whether you did, since you might do it in an included file. In fact, if you're going to #define YYSTYPE, you should do it in a common header file, and #include the common header file in both bison and flex programs (as above, before you include the bison-generated header file).

Also, when you're regenerating the generated code, remember to always generate the bison program first because the flex program depends on the generated header file. That's the opposite order to the way you are doing it.

Just to make all this a bit clearer, here's an example:

 common.h:

   struct MyType {
     /* ... /
   };

   #define YYSTYPE struct MyType;


 lexer.l:

   %{
      /* All your standard includes go here */
      /* Must go in this order */
      #include "common.h"
      #include "bison.tab.h"

   %}

 bison.y:

   %{
      /* Whatever library includes you need */
      #include "common.h"
      /* Don't include bison.tab.h; it will get inserted automatically */
   %}

OTHER TIPS

To fix the yytext error add this to bison.y :-

extern char *yytext

To fix the yyerror error make your prototype at the top of bison.y match the defn below :-

int yyerror(const char *message);

Fixing the yylval error requires a little more work and I don't understand this well enough to help. I'd suggest trying a simple hello,world type lexer,parser and move forward from there.

Here is the common.h I used :-

typedef struct STYPE {
    int pointer;
} STYPE;

And the header of lexer :-

%{
#include "common.h"
#define YYSTYPE STYPE
#include <stdio.h>
#include"bison.tab.h"
void showToken(char*);
%}

And the header of parser :-

%{
#include <stdio.h>
#include "common.h"
extern char *yytext;
#define YYSTYPE STYPE
%}

This gives me one error and a few warnings but those are due to the undefined functions. Note that I have moved the declaration of STYPE to the top of lexer and parser

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top