使用Flex和Bison创建简单编译器时出错

时间:2014-10-09 17:52:34

标签: compiler-construction bison flex-lexer

我试图从Compiler Construction using Flex and Bison Book运行简单的编译器,但是有很多错误我只是写下了书中的代码。我不是弹性和野牛的专家我试图解决它,但我不能。

here are codes我试图运行,这是错误。

root@Tom:/home/karo/Desktop# bison -dv Simple.y 
Simple.y: warning: 39 shift/reduce conflicts [-Wconflicts-sr]
root@Tom:/home/karo/Desktop# gcc -c Simple.tab.c 
In file included from Simple.y:6:0:
SM.h: In function ‘fetch_execute_cycle’:
SM.h:55:4: warning: format ‘%s’ expects a matching ‘char *’ argument [-Wformat=]
    default : printf( "%sInternal Error: Memory Dump\n" ); break;
    ^
In file included from Simple.y:7:0:
CG.h: In function ‘print_code’:
CG.h:28:3: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘int’ [-Wformat=]
   printf("%3ld: %-10s%4ld\n",i,op_name[(int) code[i].op], code[i].arg );
   ^
CG.h:28:3: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 4 has type ‘int’ [-Wformat=]
Simple.tab.c: At top level:
Simple.tab.c:158:15: error: stray ‘#’ in program
 typedef union #line 37 "Simple.y" /* yacc.c:355  */
               ^
Simple.tab.c:158:21: error: expected identifier or ‘(’ before numeric constant
 typedef union #line 37 "Simple.y" /* yacc.c:355  */
                     ^
Simple.tab.c:161:7: error: stray ‘#’ in program
 union #line 37 "Simple.y" /* yacc.c:355  */
       ^
Simple.tab.c:161:13: error: expected identifier or ‘(’ before numeric constant
 union #line 37 "Simple.y" /* yacc.c:355  */
             ^
Simple.tab.c:178:1: error: unknown type name ‘YYSTYPE’
 extern YYSTYPE yylval;
 ^
Simple.tab.c:354:3: error: unknown type name ‘YYSTYPE’
   YYSTYPE yyvs_alloc;
   ^
Simple.tab.c:683:52: error: unknown type name ‘YYSTYPE’
 yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
                                                    ^
Simple.tab.c:702:46: error: unknown type name ‘YYSTYPE’
 yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
                                              ^
Simple.tab.c:740:39: error: unknown type name ‘YYSTYPE’
 yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule)
                                       ^
Simple.tab.c:1020:44: error: unknown type name ‘YYSTYPE’
 yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)
                                            ^
Simple.tab.c:1027:16: error: expected end of line before ‘push’
   YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
                ^
Simple.tab.c:1027:16: error: expected end of line before ‘ignored’
Simple.tab.c:1027:16: error: expected end of line before ‘ignored’
Simple.tab.c:1029:16: error: expected end of line before ‘pop’
   YY_IGNORE_MAYBE_UNINITIALIZED_END
                ^
Simple.tab.c:1039:1: error: unknown type name ‘YYSTYPE’
 YYSTYPE yylval;
 ^
Simple.tab.c: In function ‘yyparse’:
Simple.tab.c:1068:5: error: unknown type name ‘YYSTYPE’
     YYSTYPE yyvsa[YYINITDEPTH];
     ^
Simple.tab.c:1069:5: error: unknown type name ‘YYSTYPE’
     YYSTYPE *yyvs;
     ^
Simple.tab.c:1070:5: error: unknown type name ‘YYSTYPE’
     YYSTYPE *yyvsp;
     ^
Simple.tab.c:1080:3: error: unknown type name ‘YYSTYPE’
   YYSTYPE yyval;
   ^
Simple.tab.c:363:46: error: ‘YYSTYPE’ undeclared (first use in this function)
      ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
                                              ^
Simple.tab.c:1157:44: note: in expansion of macro ‘YYSTACK_BYTES’
           (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
                                            ^
Simple.tab.c:363:46: note: each undeclared identifier is reported only once for each function it appears in
      ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
                                              ^
Simple.tab.c:1157:44: note: in expansion of macro ‘YYSTACK_BYTES’
           (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
                                            ^
Simple.y:60:26: error: request for member ‘id’ in something not a structure or union
   | INTEGER id_seq IDENTIFIER '.' { install( $3 ); }
                          ^
Simple.y:63:26: error: request for member ‘id’ in something not a structure or union
   | id_seq IDENTIFIER ',' { install( $2 );  }
                          ^
Simple.y:69:41: error: request for member ‘id’ in something not a structure or union
   | READ IDENTIFIER   { context_check( READ_INT, $2 ); }
                                         ^
Simple.y:71:39: error: request for member ‘id’ in something not a structure or union
   | IDENTIFIER ASSGNOP exp  { context_check( STORE, $1 ); }
                                       ^
Simple.y:72:17: error: request for member ‘lbls’ in something not a structure or union
   | IF exp   { $1 = (struct lbs *) newlblrec(); $1->for_jmp_false = reserve_loc();}
                 ^
Simple.y:72:64: error: request for member ‘lbls’ in something not a structure or union
   | IF exp   { $1 = (struct lbs *) newlblrec(); $1->for_jmp_false = reserve_loc();}
                                                                ^
Simple.y:73:17: error: request for member ‘lbls’ in something not a structure or union
     THEN commands   { $1->for_goto = reverse_loc(); }
                 ^
Simple.y:74:29: error: request for member ‘lbls’ in something not a structure or union
     ELSE    { back_patch( $1->for_jmp_false, JMP_FALSE,gen_label() );}
                             ^
Simple.y:76:28: error: request for member ‘lbls’ in something not a structure or union
     FI    { back_patch($1->for_goto,GOTO,gen_label());}
                            ^
Simple.y:77:16: error: request for member ‘lbls’ in something not a structure or union
   | WHILE    { $1 = (struct lbs *) newlblrec(); $1->for_goto = gen_label(); }
                ^
Simple.y:77:62: error: request for member ‘lbls’ in something not a structure or union
   | WHILE    { $1 = (struct lbs *) newlblrec(); $1->for_goto = gen_label(); }
                                                              ^
Simple.y:78:17: error: request for member ‘lbls’ in something not a structure or union
    exp   { $1->for_jmp_false = reserve_loc(); }
                 ^
Simple.y:81:33: error: request for member ‘lbls’ in something not a structure or union
     END    { gen_code( GOTO, $1->for_goto );back_patch( $1->for_jmp_false,JMP_FALSE,gen_label() );}
                                 ^
Simple.y:81:74: error: request for member ‘lbls’ in something not a structure or union
     END    { gen_code( GOTO, $1->for_goto );back_patch( $1->for_jmp_false,JMP_FALSE,gen_label() );}
                                                                          ^
Simple.y:83:34: error: request for member ‘intval’ in something not a structure or union
 exp :  NUMBER     { gen_code( LD_INT, $1 );}
                                  ^
Simple.y:84:39: error: request for member ‘id’ in something not a structure or union
  | IDENTIFIER    { context_check( LD_VAR, $1 );}
                                       ^

我该如何解决?

1 个答案:

答案 0 :(得分:3)

这是bison 3.0.2中报告的错误,它来自非标准用法

 %union <union-name> {
    <union members
 }

执行此操作时,bison 3.0.2会错误地将#line指令插入到<union-name>之前生成的代码中,而union YYSTYPE通常不在行的开头。

如果您删除了该名称,那么事情就会奏效,但您的联盟将被称为union semrec而不是 %union semrec { // .. members .. } ,这可能会成为一个问题。我建议如下:

union

所有其他错误(可能)来自{{1}}声明中的语法错误。

联合名称前的换行符应解决问题。我没有安装3.0.2方便,所以没有承诺。我明天会再试一次。