Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Bison and Flex without global or static variable

Tags:

i am working in a small language/IDE. And I need to know how to configure flex and bison to work together but without using any global or static variable. I need to pass to bison my AST pointer. I also need that bison pass my AST to flex as well. It's a thread environment but i dont need any thread sync. And I need a separete yylineno variable for each yyparse() call. I read about %define api.pure , %parse-param and %option reentrant. But i don't know how to put them to work together... thx in advance...

I tried this:

scanner.l:

%{

#include <iostream>
#include <sstream>
#include "parser.tab.h"
#define YY_DECL extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner)
extern void yyerror(yyscan_t scanner, NBloco * bloco, const char *s);

%}

%option noyywrap
%option yylineno
%option reentrant 
%option bison-bridge

%%
//...scanner code

parser.y:

%{
#include <iostream>
#include "AST.h"

#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif

extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);
extern "C" FILE *yyin;
extern int yylineno;
void yyerror(yyscan_t scanner, NBloco * bloco, const char *s);
%}

%union{ 
//union code
}

%define api.pure full
%lex-param   { yyscan_t scanner }
%parse-param { yyscan_t scanner }
%parse-param { NBlock* block}

//tokens...
//types...

%%

//parser code...

And I got this:

parser.y:13:22: warning: 'yylex' initialized and declared 'extern' [enabled by default] extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);


parser.y:13:22: error: 'YYSTYPE' was not declared in this scope


parser.y:13:32: error: 'lvalp' was not declared in this scope extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);


parser.y:13:48: error: expected primary-expression before 'scanner'
 extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);

                                            ^
parser.y:13:55: error: expression list treated as compound expression in initializer [-fpermissive]
 extern "C" int yylex(YYSTYPE * lvalp, yyscan_t scanner);

                                                   ^
parser.tab.c: In function 'int yyparse(yyscan_t, NBloco*)':
parser.tab.c:932:39: error: 'yylex' cannot be used as a function
 # define YYLEX yylex (&yylval, scanner)


parser.tab.c:1618:16: note: in expansion of macro 'YYLEX'
       yychar = YYLEX;

// more errors...

I also to fix the yylineno to be per file... i am basing on http://benlynn.blogspot.com.br/2013/12/reentrant-parsers-with-flex-and-bison.html and http://www.lemoda.net/c/reentrant-parser/index.html