2014-10-09 105 views
1

我試圖從Compiler Construction using Flex and Bison書中運行簡單編譯器,但有很多錯誤,我只是寫下書中的代碼。我不是flex和野牛的專家,我試圖解決它,但我不能。使用Flex和Bison創建簡單編譯器時出現錯誤

here are codes我試圖運行,這是錯誤。

[email protected]:/home/karo/Desktop# bison -dv Simple.y 
Simple.y: warning: 39 shift/reduce conflicts [-Wconflicts-sr] 
[email protected]:/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);} 
            ^

我該如何解決?

+0

你不能指望這裏的人爲你解析你的編譯器消息。我也不熟悉Flex/Bison,但對我來說,其中許多錯誤都是不言自明的。如果這些錯誤來自自動生成的代碼,則Flex/Bison版本與您使用的編譯器不兼容,或者使用不正確的選項。 – 2014-10-09 20:21:41

+0

@ 500-InternalServerError:這是一個野牛的錯誤。請參閱https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=733942。如果這是您的失望,您可能需要重新考慮。 – rici 2014-10-11 07:28:47

+0

不是我的 - 我幾乎從不投下任何東西。 – 2014-10-11 10:46:56

回答

3

這是野牛3.0.2報告的錯誤,這是從非標準使用

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

當你做到這一點,野牛3.0.2不正確地插入#line指令到生成的代碼之前<union-name>,通常不在一行的開頭。

如果您刪除名稱,那麼事情將起作用,但您的工會將被稱爲union YYSTYPE而不是union semrec,這可能會成爲一個問題。我建議如下:在union聲明

%union 
     semrec { 
      // .. members .. 
} 

所有其他錯誤(可能)來自語法錯誤。

聯合體名稱之前的新行應解決問題。雖然我沒有安裝3.0.2,但沒有任何承諾。我明天會試着再看一遍。

+0

我刪除了這個名字就像你說的那樣,並且Simple.y中有一個輸入錯誤,我修復了它,現在沒關係,謝謝 – Karo 2014-10-11 13:38:25