2013-06-15 27 views
7

ile derlerken `_yyerror 'tanımsız başvurusu Mini Pascal benzeri bir dil için derleyici yapmaya çalışıyorum. Bunun için Flex ve Bison kullanıyorum ve bu hata ile geldim.Flex ve Bison

Benim Flex dosyası:

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

%% 

[1-9][0-9]*  { 
       yylval.i = atoi(yytext); 
       return INT; 
} 

program  return PROGRAM; 
or   return OR; 
and   return AND; 
not   return NOT; 
if   return IF; 
else  return ELSE ; 
while  return WHILE; 
"+"   return PLUS; 
"-"   return MINUS; 
"*"   return MUL; 
"/"   return DIV; 
"["   return LSB; 
"]"   return RSB; 
"{"   return LCB; 
"}"   return RCB; 
"("   return LEFTPAR; 
")"   return RIGHTPAR; 
":="  return ASSIGN; 
"=="  return ISEQUAL; 
"<"   return LTHAN; 
">"   return GTHAN; 
"<>"  return NOTEQUAL; 
"<="  return LESSEQUAL; 
">="  return GREATEREQUAL; 

[a-zA-z][a-z0-9]* { 
        yylval.s = (char*)malloc(strlen(yytext)*sizeof(char)); 
        strcopy(yylval.s,yytext); 
        return ID; 
}    

[ \t\n]+       /* eat up whitespace */   

.         yyerror("Unknown Character"); 

%% 
int yywrap(void) { 
    return 1; 
} 

Benim Bison dosyası:

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

%union { 
    int i; 
    char *s; 
}; 

%token <i> INTEGERNUM 

%token PROGRAM; 
%token OR; 
%token AND; 
%token NOT; 
%token IF; 
%token ELSE; 
%token WHILE; 
%token PLUS; 
%token MINUS; 
%token MUL; 
%token DIV; 
%token LSB; 
%token RSB; 
%token LCB; 
%token RCB; 
%token LEFTPAR; 
%token RIGHTPAR; 
%token ID; 
%token INT; 
%token ASSIGN; 
%token ISEQUAL; 
%token LTHAN; 
%token GTHAN; 
%token NOTEQUAL; 
%token LESSEQUAL; 
%token GREATEREQUAL; 

%% 

program: 
     PROGRAM ID block 
     ; 

block: 
     LCB sequence RCB 
     ; 

sequence: 
     statement ';' sequence 
     | statement ';' 
     ; 

bracketsSeq: 
     LCB sequence RCB 
     ; 

brackOrStat:   
     bracketsSeq 
     | statement 
     ; 

statement: 
     assignmentStat 
     |ifStat 
     |whileStat 
     | 
     ; 

assignmentStat: 
     ID ':=' expression 

ifStat: 
     IF LEFTPAR condition RIGHTPAR brackOrStat elsepart 
     ; 

elsepart: 
     ELSE brackOrStat 
     | 
     ; 

whileStat: 
     WHILE LEFTPAR condition RIGHTPAR brackOrStat 
     ; 

expression: 
     optionalSign expression 
     |expression addOper expression 
     |term 
     ; 

term: 
     term mulOper term 
     |factor 
     ; 

factor: 
     INT 
     |LEFTPAR expression RIGHTPAR 
     |ID 
     ; 

condition: 
     condition AND condition 
     |boolterm 
     ; 

boolterm: 
     boolterm OR boolterm 
     |boolfactor 
     ; 

boolfactor: 
     NOT LSB condition RSB 
     |LSB condition RSB 
     |expression relationalOper expression 
     ; 

relationalOper: 
     ISEQUAL 
     |LTHAN 
     |GTHAN 
     |NOTEQUAL 
     |LESSEQUAL 
     |GREATEREQUAL 
     ; 

addOper: 
     PLUS 
     |MINUS 
     ; 

mulOper: 
     MUL 
     |DIV 
     ; 

optionalSign: 
     addOper 
     | 
     ; 

%% 

int main(int argc, char **argv) 
      { 
      printf("TEST\n"); 

      }  

İdam adımları dizi oldu:

$ ./bison.exe -dy comp.y 
$ ./flex.exe comp.l 
$ gcc -c -w lex.yy.c 
$ gcc -c -w comp.tab.c 
$ gcc comp.tab.o lex.yy.o -o ex 
comp.tab.o:comp.tab.c:(.text+0x4cd): undefined reference to `_yyerror' 
comp.tab.o:comp.tab.c:(.text+0x61c): undefined reference to `_yyerror' 
lex.yy.o:lex.yy.c:(.text+0x34a): undefined reference to `_strcopy' 
lex.yy.o:lex.yy.c:(.text+0x362): undefined reference to `_yyerror' 
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: lex.yy.o: bad reloc address    0x828 in section `.rdata' 
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: final link failed: Invalid    operation 
collect2: ld returned 1 exit status 
$ 

ne şekilde herhangi bir tavsiye ilan etmek ve nerede çünkü yanlış bir şey ilan ettiğim gibi görünüyor!

+0

Sağlanan bir kütüphaneye sahip olabilirsiniz, '-ly' (' liby.a' veya 'liby.so') yyerror() 've' main() 'ın bir sürümü. Daha sonra bağlantı komut satırına '-ly' eklersiniz. –

cevap

14

yyerror bildirmek için yeterli değil. Bir tanım vermelisiniz. Sahip diğer sorun da esnek dosyasında strcpy yanlış yazılmış olmasıdır

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

:

bison manual minimal uygulaması olarak aşağıdaki göstermektedir.

Daha doğru bir şekilde, kopyalama hataları tarafından bildirilen diğer sorun, kopyalama kodunuzun yanlış olması nedeniyle yanlış yazılmış strcpy. Dizeleri sonlandırması gereken NUL bayttan sorumlu değildir. strcpy, bu baytı kopyalayacaktır, sonuçta, ayrılmamış bir depolama alanına 0 yazacaktır. strdup'u kullanmanın çok daha kolay olduğunu göreceksiniz. (Ve onlarla bittiğinde free dizeleri gerektiğini unutmayın.)