gpt4 book ai didi

c - 总线错误: 10 in bison and flex on mac os

转载 作者:行者123 更新时间:2023-11-30 19:32:18 25 4
gpt4 key购买 nike

这是有问题的代码:

计算.y

%{
#include <stdio.h>
void yyerror(char *);
int yylex(void);

int sym[26];
%}
%token INTEGER VARIABLE
%left '+' '-'
%left '*' '/'

%%

program:
program statement '\n'
| /* NULL */
;

statement:
expression { printf("%d\n", $1); }
| VARIABLE '=' expression { sym[$1] = $3; }
;

expression:
INTEGER
| VARIABLE { $$ = sym[$1]; }
| expression '+' expression { $$ = $1 + $3; }
| expression '-' expression { $$ = $1 - $3; }
| expression '*' expression { $$ = $1 * $3; }
| expression '/' expression { $$ = $1 / $3; }
| '(' expression ')' { $$ = $2; }
;

%%

void yyerror(char *s){
fprintf(stderr, "%s\n", s);
}

int main(void){
yyparse();
}
<小时/>

计算.l

%{
#include "calc.tab.h"
#include <stdlib.h>
void yyerror(char *);
%}

%%
[a-z] {
yylval = *yytext - 'a';
return VARIABLE;
}

[0-9]+ {
yylval = atoi(yytext);
return INTEGER;
}

[-+()=/*\n] { return *yytext; }

[\t] ;

. yyerror("Unkown Character");

%%

int yywrap(void) {
return 1;
}

当我使用以下命令运行上面的代码时,它运行良好。

$ bison -d calc.y
$ flex calc.l

但是,当它像这样运行时:

$ gcc lex.yy.c calc.tab.c -o app

这个命令不能很好地工作。我收到以下错误:

Bus error: 10

谁能解释一下为什么会发生这种情况?

或者,我该如何解决这个错误?

请需要帮助。

最佳答案

您需要决定 VARIABLE 是 sym[$1] 还是只是 sym[] 的索引。myou 在语法中使用了这两种方式。从你的词法分析器来看,它是索引。事实上,我根本不认为 sym[] 有任何必要。

当您生成 .c 文件编译它们时,您没有遇到总线错误。当您执行您的应用程序时,您就得到了它。

关于c - 总线错误: 10 in bison and flex on mac os,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47248988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com