Parsing 谁在yacc语法中修改$$的类型
我想在下面的语法中将$$定义为一个结构,我已将yylval声明为str,但在使用gcc编译.c文件时出错Parsing 谁在yacc语法中修改$$的类型,parsing,yacc,lex,Parsing,Yacc,Lex,我想在下面的语法中将$$定义为一个结构,我已将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 de
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;
}
%{
#包括
#包括
#包括
类型定义结构{
int-val;
int cpt;
}str;
stryylval;
%}
%开始
%令牌号
%%
开始:编号“+”编号“\n”
;
%%
内部主(空)
{
yyparse();
返回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';
" " ;
%%
%选项noyywrap
%{
#包括
#包括
#包括
#包括“y.tab.h”
%}
%%
[0-9]+ {
yylval=atoi(yytext);
返回号码;
}
“+”返回“+”;
\n返回'\n';
" " ;
%%
您不能定义自己的yylval
。生成的代码已经定义了这一点。使用%union
指令间接定义类型。如果这不合适,那么您可以重新定义宏YYSTYPE
,它将扩展为任意类型说明符。例如:
struct my_semantic_attributes {
int foo;
/* ... */
};
#define YYSTYPE struct my_semantic_attributes
您不能定义自己的
yylval
。生成的代码已经定义了这一点。使用%union
指令间接定义类型。如果这不合适,那么您可以重新定义宏YYSTYPE
,它将扩展为任意类型说明符。例如:
struct my_semantic_attributes {
int foo;
/* ... */
};
#define YYSTYPE struct my_semantic_attributes
是yylval=atoi(yytext);精细我们的yylval.val=atoi(yytext);我相信
yylval=atoi(yytext)如果您定义YYSTYPE int(这是相当有限的),则可以使用code>。如果我使用#定义YYSTYPE str会怎么样。在这种情况下,yylval
的类型为str
,当然,函数“yylex”中的yylval.val=
.tp.l:是这样的:tp.l:12:错误:请求非结构或unionis yylval=atoi(yytext)中的成员“val”;精细我们的yylval.val=atoi(yytext);我相信yylval=atoi(yytext)代码>是可能的,如果你定义YYSTYPE int(这是相当有限的)。如果我使用#定义YYSTYPE str。在这种情况下,yylval
是类型str
,当然yylval.val=
.tp.l:在函数“yylex”中:tp.l:12:错误:请求非结构或联合中的成员“val”