0

I want to read a file in a C++ program and pass it on to lex and yacc for parsing, but I am getting compilation errors. Here is my main.cpp file (with the first error):

extern "C"
{
    #include "parser_iface.h"
    #include "parser.h"
    #include "lexer.h"
}

#include <stdio.h>
#include <iostream>

int yyparse(yyscan_t scanner);

int main(void)
{
    yyscan_t scanner;
    extern FILE *yyin;

    if (yylex_init(&scanner))
        return NULL;
    yyin = fopen("test_file.cws", "r");  // <- error C2039: 'yyin_r' : is not a member of '_iobuf'
    if(yyin == NULL)
    {
         std::cout << "Error!" << std::endl;
    }
    else 
    {
        std::cout << "Parsing" << std::endl;
        yyparse(scanner);
    }
    yy_delete_buffer(0, scanner);
    yylex_destroy(scanner);
    printf("Press ENTER to close. ");
    getchar();
    return 0;
}

The top of my lex file (.l) is as follows:

%{
    #include "parser_iface.h"
    #include "parser.h"
    #include <stdio.h>
%}

%option outfile="lexer.c" header-file="lexer.h"
%option warn reentrant noyywrap never-interactive nounistd bison-bridge

The top of my yacc file (.y) is as follows:

%{
    #include "parser_iface.h"
    #include "parser.h"
    #include "lexer.h"
    int yyerror(yyscan_t scanner, const char *msg);
    int curr_mod;
%}

%code requires
{
    #ifndef YYSCAN_T
        #define YYSCAN_T
        typedef void* yyscan_t;
    #endif
}

%output  "parser.c"
%defines "parser.h"
%define api.pure
%lex-param   { yyscan_t scanner }
%parse-param { yyscan_t scanner }

I am using win_flex and win_bison.

gornvix
  • 3,154
  • 6
  • 35
  • 74

1 Answers1

2

yyin is currently defiled as a weird macro and is not supposed to be used. Use a reentrant API instead:

FILE * src = fopen("test_file.cws", "r");
yyset_in(src, scanner);
user58697
  • 7,808
  • 1
  • 14
  • 28
  • "*defiled* as a weird macro"? Was that intentional? It made me laugh. – Fred Larson Jan 13 '15 at 22:43
  • This is only the case because he is using a pure (reentrant) scanner. (Mind you, pure scanners are a good thing.) It's documented, too. `yyin` is defined as a macro for use only inside scanner actions. – rici Jan 14 '15 at 00:55