0

我想将定义 $$ 更改为以下语法中的结构我已将 yylval 声明为 str,但是在使用 gcc 编译 .c 文件时出现错误

gcc *.c -ly 
tp.l: In function ‘yylex’:
tp.l:12: error: request for member ‘sum’ in something not a structure or union
y.tab.c:1035: error: conflicting types for ‘yylval’
tp.y:11: note: previous declaration of ‘yylval’ was here

yacc 文件:

 %{
        #include <ctype.h>
        #include <stdio.h>
        #include <stdlib.h>

        typedef struct {
            int val;
            int cpt;
        } str;

        str yylval;

%}
        %start  start 
        %token  number

%%
    start : number'+'number'\n'
    ;

%%

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

lex文件:

%option noyywrap

%{
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include"y.tab.h"
%}

%%
[0-9]+  {
            yylval = atoi(yytext); 
            return number;
        }

"+"     return '+';
\n      return '\n';
" " ;

%%
4

1 回答 1

3

您无法定义自己的yylval. 生成的代码已经定义了这一点。使用%union指令间接定义类型。如果这不合适,那么您可以做的是重新定义YYSTYPE扩展为任意类型说明符的宏。例如:

struct my_semantic_attributes {
  int foo;
  /* ... */
};

#define YYSTYPE struct my_semantic_attributes
于 2012-05-17T16:11:45.670 回答