I'm trying to write my first parser with Flex & Bison. When parsing numbers, I'm trying to save their values into the yylval
structure. The problem is, yylval
is null when the lexer reaches a number, which causes a segmentation fault.
(Related point of confusion: why is it that in most Flex examples (e.g. here), yylval
is a structure, rather than a pointer to a structure? I couldn't get yylval
to be recognized in test.l without %option bison-bridge
, and that option made yylval
a pointer. Also, I tried initializing yylval
in main
of test.y, but yylval = malloc(...)
gives a type mismatch-- as if yylval
is not a pointer...?)
test.l
%{
#include <stdio.h>
#include <stdlib.h>
#include "svg.tab.h"
%}
%option bison-bridge
%option noyywrap
%%
[0-9]+ { yylval->real = atof(yytext); return REAL; }
. { return *yytext; }
%%
test.y:
%{
#include <stdio.h>
void yyerror(char *);
%}
%union {
double real;
}
%token <real> REAL
%%
...
Build command:
bison -d test.y && flex test.l && gcc lex.yy.c test.tab.c