2012-04-23 11 views
2

私はflex/bisonを学習しています。私は以下のプログラムを書いたが、間違いがあった。フレックス/ 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

タグ[タグ:フレックス]は、実際にはAdobe Flashの取引用です。 flexはGNUプロジェクトではありませんが、tag [tag:gnu-flex]は私たちが抱えているものです。 – sarnold

+0

Adob​​e Flashのカテゴリも同様にタグをadobe-flexに変更する必要があります。 – Kaz

答えて

2

最初の目立つ問題は、yaccを使用している場合はYYSTYPEを定義してはいけないということです。 Yaccは%unionYYSTYPEと定義しています。 を使用していて、yaccを使用していない場合は、YYSTYPEを定義する必要があります。

%unionパーズスタックアイテムタイプには、intstrのメンバーのみが含まれます。例:あなたがタイプstrを持つものとしてexp文法記号を定義している、そして、あなたは、この生産を持っている:

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

mknode機能はnode *を返しますが、それはexp上の略ですので$$シンボルのタイプはchar *ですstrと入力した左側と%unionstrのメンバーはchar *です。

mknodeの引数もnode *である必要があります。しかし$1$3にはそのタイプがありません。

+0

私はフレックスとバイソンを使用しています。 – sap

+1

flexとbisonはGNUプロジェクトの 'lex'と' yacc'の実装です。彼らは他の 'lex'や' yacc'ツールにはない特別な機能を持っていますが、ここではそれほど重要ではありません。 – Kaz

+0

MinGWでFlexコンパイルの問題をどうやって解決しましたか?私は応答を見ませんでした。 – Kaz

関連する問題