【问题标题】:Bus error: 10 in bison and flex on mac os总线错误:10 in bison and flex on mac os
【发布时间】:2018-04-25 05:30:47
【问题描述】:

这里是有问题的代码:

calc.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

此命令无法正常工作。我正在关注error

Bus error: 10

谁能解释为什么会这样?

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

请需要帮助。

【问题讨论】:

  • 我无法在我的拱门上重现同样的行为。没有错误,这是预期的工作。也许你应该包含你的 bison、flex 和 gcc 版本。
  • @Stargateur 。我如何生成app.?我已经尝试过gcc lex.yy.c calc.tab.c -o appvalgrind gcc lex.yy.c calc.tab.c -o app。但这行不通。
  • 你的 gcc 版本是多少? gcc -v
  • @Stargateur 。 $ gcc -v Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Bus error: 10
  • 嗯……用你的包管理器升级你的 gcc……我无法想象你还在 gcc-4.2.1。也许这会修复错误。

标签: c macos bison flex-lexer


【解决方案1】:

您需要决定 VARIABLE 是 sym[$1] 还是只是 sym[].myouve 在语法中使用这两种方式的索引。根据您的词法分析器判断,它是索引。事实上,我认为sym[] 根本没有必要。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2012-09-21
    • 2015-04-25
    • 2011-09-21
    相关资源
    最近更新 更多