2012-04-23 67 views
2

我正在學習flex/bison。我寫了下面的程序,但得到錯誤。Flex/Bison錯誤:請求會員`str'的東西不是結構或聯盟

%{ 
#include <stdio.h> 

typedef struct node 
{ 
    struct node *left; 
    struct node *right; 
    char *token; 

}node; 

node *mknode(node *left, node *right, char *token); 
void printtree(node *tree); 

#define YYSTYPE struct node * 

%} 


%union 
{ 
    char* str; 
    int num; 
} 


%start lines 
%token <str> WORD 
%token <str> PLUS MINUS TIMES DIVIDE POWER 
%token <str> LEFT_PARENTHESIS RIGHT_PARENTHESIS 
%token <str> END 

%left PLUS MINUS 
%left TIMES DIVIDE 
%right POWER 

%type <str> exp term factor line lines 
%% 

lines:  /* empty */ 
      | lines line; 

line:  exp END     { printtree($1); printf("\n");} 
      ; 

exp:  term     {$$=$1; } 
      | exp PLUS term   { $$=mknode($1,$3,"+"); } 
      | exp MINUS term  { $$=mknode($1,$3,"-"); } 
      ; 

term:  factor     { $$=$1; } 
      |term TIMES factor  { $$=mknode($1,$3,"*"); } 
      ; 

factor:  WORD     { $$=mknode(0,0, (char*)yylval); } 
      |LEFT_PARENTHESIS exp RIGHT_PARENTHESIS 
            { $$=$2; } 
      ; 
%% 

int main(void) 
{ 
    return yyparse(); 
} 

node *mknode(node *left, node *right, char *token) 
{ 
    /* malloc the node */ 
    node *newnode = (node *)malloc(sizeof(node)); 
    char *newstr = (char *)malloc(strlen(token)+1); 
    strcpy(newstr, token); 
    newnode->left = left; 
    newnode->right = right; 
    newnode->token = newstr; 
    return(newnode); 
} 

void printtree(node *tree) 
{ 
    int i; 

    if (tree->left || tree->right) 
    printf("("); 

    printf(" %s ", tree->token); 

    if (tree->left) 
    printtree(tree->left); 
    if (tree->right) 
    printtree(tree->right); 

    if (tree->left || tree->right) 
    printf(")"); 
} 

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

柔性:

C:\flexbison>recomp example2 
example2.y:40.6: warning: empty rule for typed nonterminal, and no action 
conflicts: 5 reduce/reduce 
example2.y:43: error: request for member `str' in something not a structure or union 
example2.y:46: error: request for member `str' in something not a structure or union 
example2.y:46: error: request for member `str' in something not a structure or union 
example2.y:47: error: request for member `str' in something not a structure or union 
example2.y:47: error: request for member `str' in something not a structure or union 
example2.y:47: error: request for member `str' in something not a structure or union 
example2.y:48: error: request for member `str' in something not a structure or union 
example2.y:48: error: request for member `str' in something not a structure or union 
example2.y:48: error: request for member `str' in something not a structure or union 
example2.y:51: error: request for member `str' in something not a structure or union 
example2.y:51: error: request for member `str' in something not a structure or union 
example2.y:52: error: request for member `str' in something not a structure or union 
example2.y:52: error: request for member `str' in something not a structure or union 
example2.y:52: error: request for member `str' in something not a structure or union 
example2.y:55: error: request for member `str' in something not a structure or union 
example2.y:56: error: request for member `str' in something not a structure or union 
example2.y:58: error: request for member `str' in something not a structure or union 
example2.y:58: error: request for member `str' in something not a structure or union 

任何想法,爲什麼我得到這個錯誤:

%{ 

    #include <stdlib.h> 
    #include "y.tab.h" 

%} 

%% 

[a-zA-Z0-9]+   { yylval.str = strdup(yytext); return WORD; } 
         /* cast pointer to int for compiler warning */ 
[ \t]     ; 
"+"      return(PLUS); 
"-"      return(MINUS); 
"*"      return(TIMES); 
"/"      return(DIVIDE); 
"^"      return(POWER); 
"("      return(LEFT_PARENTHESIS); 
")"      return(RIGHT_PARENTHESIS); 
\n      return(END); 


%% 

int yywrap (void) {return 1;} 

我得到的錯誤?

+0

標籤[tag:flex]實際上是針對Adobe Flash的交易。即使flex不是GNU項目,tag [tag:gnu-flex]就是我們所堅持的。 – sarnold 2012-04-23 00:51:29

+0

也許Adobe Flash類別也應該將其標記更改爲adobe-flex。 – Kaz 2012-04-23 01:12:37

回答

2

第一個明顯的問題是,如果您使用的是yacc,則不應該定義YYSTYPE。 Yacc從您的%union定義YYSTYPE。如果您使用的是lex,但不使用yacc,則需要定義YYSTYPE

您的%union解析棧項目類型僅包含intstr成員。例如:定義了exp語法符號爲具有類型str,然後你有這樣生產:

exp:  term     {$$=$1; } 
      | exp PLUS term   { $$=mknode($1,$3,"+"); } 
      | exp MINUS term  { $$=mknode($1,$3,"-"); } 
      ; 

mknode函數返回node *,但$$符號的類型是char *,因爲它代表了exp上您鍵入的作爲str的左側和%unionstr成員是char *

mknode的參數也必須是node *。但是$1$3沒有這種類型。

+0

我使用flex和野牛。 – sap 2012-04-23 01:03:23

+1

flex和bison是GNU項目的'lex'和'yacc'的實現。它們具有在其他'lex'和'yacc'工具中找不到的特殊功能,但這不是很相關。 – Kaz 2012-04-23 01:04:59

+0

MinGW上的Flex編譯問題是如何解決的?我沒有看到迴應。 – Kaz 2012-04-23 01:13:21

相關問題