请帮忙!我是一个新手,我正在尝试使用flex-bison编程解析器,我需要使用记录类型,我不知道为什么我应该使用它第一次和第二次当我尝试使用它时冲突在类型eventsndi我想,它给了我这个错误列表
这是 global.h
文件中类型的定义struct Point{
int *x;
int *y;
};
typedef struct Point Point;
#define YYSTYPE Point
extern YYSTYPE yylval;
知道一个数字可能有3到4个段,这就是要解析的文本
Figure{
Segment{
Point(1,30)
Point(20,30)
}
Segment{
Point(20,30)
Point(2,10)
}
Segment{
Point(2,10)
Point(1,30)
}
}
这就是我试图做的事情
mes_tokens.l
%{
#include <stdio.h>
#include <string.h>
#include "global.h"
#include "y.tab.h"
extern YYSTYPE yylval;
struct Point *tmp;
%}
%option noyywrap
blanks [ \t\n]+
accolade_close \}
comma \,
number [0-9]+
%x ABS ORD
%%
{blanks} { /* ignore */ }
"Point(" {BEGIN(ABS);}
<ABS>{number} {
tmp= (struct Point*) malloc(sizeof(struct Point));
tmp->x=atoi(yytext); }
<ABS>{comma} {BEGIN(ORD); }
<ORD>{number} {
tmp->y=atoi(yytext);
//yylval= ( Point *) malloc(sizeof(Point));
yylval.x= tmp->x;
yylval.y= tmp->y;
return POINT;
}
<ORD>\) { BEGIN(INITIAL);}
"Figure{" { return(FIGURE); }
"Segment{" { return(SEGMENT); }
{accolade_close} { return(ACCOLADE_CLOSE); }
. { yyerror("Unknown char"); }
%%
figure.y
%{
#include "global.h"
#include "y.tab.h"
#include <stdio.h>
#include<stdlib.h>
int yyparse();
int yylex();
int yyerror(char *s);
%}
%union {
struct Point *point;
}
%token <Point> FIGURE SEGMENT POINT ACCOLADE_CLOSE
%start Input
%%
Input:FIGURE ListSegment ACCOLADE_CLOSE
;
ListSegment: Seg Seg Seg Seg
| Seg Seg Seg
;
Seg: SEGMENT POINT POINT ACCOLADE_CLOSE { printf("Segment Point1
(%d,%d)\n",$2->x, $2->y);}
%%
int yyerror(char *s) {
printf("yyerror : %s\n ",s);
return 0;
}
int main(void) {
yyparse();
return 0;
}
答案 0 :(得分:2)
您定义它的方式,yylval
是Point
。不是 point
成员。下定决心。
正常的做法是将yyunion
定义为拥有 point
成员,该成员同意.y文件中的代码。