Compilation bison解析器编译未知错误

Compilation bison解析器编译未知错误,compilation,compiler-errors,bison,flex-lexer,parser-generator,Compilation,Compiler Errors,Bison,Flex Lexer,Parser Generator,我正在构建一个解析器,但我有一些无法解决的错误,我是bison和flex的新手,请帮助我解决这些错误,并理解它们发生的原因。我得到的错误是: 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 âyy

我正在构建一个解析器,但我有一些无法解决的错误,我是bison和flex的新手,请帮助我解决这些错误,并理解它们发生的原因。我得到的错误是:

   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â
这是我的解析器文件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
%{
#包括
#包括“bison.tab.h”
#包括“common.h”
//int-yylex();
无效错误(字符常量*);
类型定义结构类型{
NODEPTR指针;
}花柱;
#定义YYSTYPE STYPE
%}
/*野牛宣言*/
%令牌ELSE实整数XWRITE而END DO IF THEN XPROGRAM函数XRETURN XBEGIN CALL ID NUM的XREAD VAR
%令牌RELOP ADDOP MULOP ASSIGN与否
%左''''+'
%左'*''/'
%非ASSOC低于其他
%非ASSOC ELSE

要修复yytext错误,请将此添加到bison.y:-

extern char *yytext
要修复yyerror错误,请使原型位于bison.y顶部,并与以下定义匹配:-

int yyerror(const char *message);
修复yylval错误需要更多的工作,我对这一点理解不够,无法提供帮助。我建议尝试一个简单的hello,world-type词法分析器,然后继续

以下是我使用的常用方法:-

typedef struct STYPE {
    int pointer;
} STYPE;
和lexer的头部:-

%{
#include "common.h"
#define YYSTYPE STYPE
#include <stdio.h>
#include"bison.tab.h"
void showToken(char*);
%}
%{
#包括“common.h”
#定义YYSTYPE STYPE
#包括
#包括“bison.tab.h”
void showToken(char*);
%}
和解析器的标头:-

%{
#include <stdio.h>
#include "common.h"
extern char *yytext;
#define YYSTYPE STYPE
%}
%{
#包括
#包括“common.h”
外部字符*文本;
#定义YYSTYPE STYPE
%}
这给了我一个错误和一些警告,但这些都是由于未定义的函数。注意,我已经将STYPE的声明移到了lexer和parser的顶部

如果您在bison文件中定义了YYSTYPE,那么您还需要在flex文件中定义YYSTYPE,因为bison不会将#define放入生成的头文件中。在包含生成的头文件之前,需要执行此操作

Bison不会将#define放在生成的头中,因为它无法知道您是否这样做,因为您可能会在包含的文件中这样做。事实上,如果您要定义YYSTYPE,您应该在一个公共头文件中执行,并且在bison和flex程序中包含公共头文件(如上所述,在包含bison生成的头文件之前)

另外,在重新生成生成的代码时,请记住始终首先生成bison程序,因为flex程序依赖于生成的头文件。这与你的做法相反

为了让这一切更清楚一点,这里有一个例子:

 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 */
   %}

请发布您的flex文件和用于生成.c文件的命令。感谢您的帮助,但它没有改变任何关于yytext和其他错误的内容!我希望您已经通过重新运行bison和lex#define YYSTYPE STYPE重新生成了.c文件。您已经将其定义为一个整数,但我将其定义为指向在common.h文件中定义的结构的指针。确定我将其设置为int,因为我没有common.h。使用任何你有的东西,只要确保common.h在顶部。我认为你不应该在lex文件中包含common.h,因为它不使用它!我照你说的做了,得到了这样一个结果:'code'bison.tab.c:1150:error:yylval-bison.tab.h:81:note:yylval的前一个声明在这里是bison.y:In函数–yypasse–bison.y:91:error:当从类型–NODEPTR–code指定给类型–STYPE–时,不兼容的类型,具体做了什么?你把#defines?@flashdisk放在哪里了?我试图让顺序更明确一些。顺便说一句,没有必要在bison.y中包含bison.tab.h,这可能不是一个好主意,但是如果你坚持要这样做,你必须在之前定义YYSTYPE,也要在bison.y文件中包含它。为什么没有必要包含它?如果我这么做了,为什么我要再做一次time@user3125280:这是真的;这不是很明确。但是,
bison
并不试图解释序言(
{%…%}
);它只是将其插入生成的实现中。它不会将其插入生成的头文件()。在现代bison版本中,您可以使用
%code requires
指令将代码插入实现和头文件(),也可以使用
%define api.value.type