簡體   English   中英

誰在yacc語法中修改$$類型

[英]Who modify the type of $$ in a yacc grammar

我想在以下語法中將定義$$更改為結構,我已將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';
" " ;

%%

您不能定義自己的yylval 生成的代碼已經定義了這一點。 使用%union指令間接定義類型。 如果不合適,那么您可以做的是重新定義宏YYSTYPE ,它將擴展為任意類型說明符。 例如:

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

#define YYSTYPE struct my_semantic_attributes

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM