Flex & Bison Segmenation fault
I to make a compiler and I use flex and bison for that. I'm implementing the boolean comparision. But I have an error when bison goes into my token COMP.
I have declared like that :
("==")|(">=")|("<=")|("!=")|(">")|("<")
{
sscanf(yytext,"%s",yylval.svalcmp); return COMP;
}
In bison my gramar is :
ExpBool :
Exp COMP Exp { printf("TEST= %s n",$2); /*comp_exp_temp($2);*/}
| Exp BOPE Exp
| NEGATION Exp
;
My union :
%union {
char cval;
char * sval;
char * svalt;
char * svalcmp;
char svalas;
char * svalds;
int signedint;
int usint;
}
and token decaring :
%token <svalcmp> COMP
Bison performs the first Exp but when it reads the COMP, I get a Segmenation fault.
Anyone has an idea ?
I usually use a different style in the lexer, so can't validate that the (...)|(...)... syntax is correct. However, what is your union associated with yylval? Is svalcmp a buffer or just a pointer? If it's a buffer, you probably should constrain the scanf format by a length. If it's just a pointer, you've probably hosed memory, and maybe hit a null pointer crash.
If you bring it up in GDB, attach a backtrace, and print yylval
链接地址: http://www.djcxy.com/p/41746.html上一篇: 可重入Flex和Bison的问题