Flex 和 Bison 未创建头文件 (.h)

问题描述 投票:0回答:1

因此,对于我的最终项目,我需要使用 flex 和 bison 编写一个解析器。我已将其下载到我的路径中,并且能够运行所有命令。唯一的问题是,当我运行以下命令时,.y 文件不会创建我在 .l 词法分析器文件中链接两者所需的 .h 头文件。我的文件名是 lexer.l 和 parser.y,它们的详细信息如下。我的问题是,什么可能导致头文件无法创建?

命令

flex lexer.l //<--- Build the lexer file
bison -d -t parser.y //<--- Build the bison file
gcc lex.yy.c //<--- Create the .yy.c file
.\a.exe //<--- Start the parse

lexer.l

/*
========================
=========F=L=E=X========
========================
-The lexer (.l) file is in charge of taking in a string and parsing it into TOKENS.
    It generates the lex.yy.c which is a bunch of complicated C code that is created
    through building our lexer file.
*/

/* =====HEADERS=====*/
%{

%}
/*==========REGULAR=EXPRESSIONS+=========*/



/*==========RULES===========*/

%%

[0-9]+ { yylval.num = atoi(yytext); return NUMBER; }
\n { return EOL; }
. {}

%%
  
/*==========C=FUNCTIONS==========*/

yywrap() {}

解析器.y

%{

%}

/* ==========TOKEN=TYPES==========*/

%union {
    int num;
    char sym;
}

/* ===========DEFINE=OUR=TOKENS========== */

%token EOL
%token PLUS
%token<num> NUMBER
%type<num> exp

/* ==========GRAMMAR========== */
%%

input: 
    exp EOL { printf("%d\n", $1); }
|   EOL;

exp: 
    NUMBER { $$ = $1 }
|   exp PLUS exp { $$ = $1 + $3; }
;

%%

int main() {
    yyparse();

    return 0; // Return 0 on successful execution
}

我一直认为这是因为我使用了错误的命令造成的,但似乎真的只有一种方法可以做到这一点。我应该能够跑

bison -d parser.y
我认为这可能是由于野牛文件无效引起的,但我从这里的教程中复制了它:https://www.youtube.com/watch?v=fFRxWtRibC8

bison flex-lexer
1个回答
0
投票
如果你给它

-d

 选项,
Bison 就会创建你需要的头文件,但要在 .l 文件中使用它,你需要一个
#include

/* =====HEADERS=====*/
%{
#include "parser.tab.h"
%}

您还需要编译 bison 创建的

parser.tab.c
文件:

gcc parser.tab.c lex.yy.c
© www.soinside.com 2019 - 2024. All rights reserved.