挠曲/野牛错误:在一些不是一个结构或联合请求构件`STR”(Flex/Bison Error:req

2019-07-29 02:44发布

我正在学习的Flex /野牛。 我写了下面的程序,但得到的错误。

%{
#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);
}

柔性:

%{

  #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;}

我得到的错误:

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

任何想法,为什么我得到这个错误?

Answer 1:

第一个明显的问题是,你不应该定义YYSTYPE如果你使用YACC。 Yacc的定义YYSTYPE从您%union 。 您需要定义YYSTYPE如果你使用lex ,但没有使用yacc

%union解析栈项只类型包含intstr成员。 例如:定义了exp语法符号为具有类型str ,然后你有这样生产:

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

mknode函数返回node * ,但类型$$符号char * ,因为它代表的是exp的左侧,其中键入作为strstr的成员%unionchar *

到的参数mknode也必须是node * 。 但是, $1$3没有这样的类型。



文章来源: Flex/Bison Error:request for member `str' in something not a structure or union