Include generated flex and bison code

I am working with Flex and Bison in C ++. I am learning to use these tools and the best way to get started is with a simple calculator. After creating an application (executable) from my calc.y and calc.l files, I can run the .exe file and use it, but now I want to include it in the C ++ file so I can use it in my application, but I cannot ... I believe this is my fault because I am including a bad generated file or creating bad code to import.

main.cpp

#include <iostream>

extern "C" {
    #include "y.tab.h"
}

int main ( int argc, char *argv[] ) {
    yyparse();
    printf(elementos);
    return 0;
}

      

calc.l

%{
#include "y.tab.h"
#include <stdlib.h>
void yyerror(char *);
%}

%%

[0-9]+  {
    yylval = atoi(yytext);
    return INTEGER;
}

[-+()\n]    {
    return *yytext;
}

[ \t]   ;

.       {
    yyerror("Invalid character.");
}

%%

int yywrap(void) {
    return 1;
}

      

calc.y

%{
    #include <stdio.h>
    int yylex(void);
    void yyerror(char *);
    int sym[26];
    int elementos = 0;
%}

%token INTEGER VARIABLE
%left '+' '-'
%left '*' '/'

%%

program:
        program expr '\n' { printf("%d\n", $2 ); }
    |
;

statement:
        expr                { printf("%d\n", $1); }
    |   VARIABLE '=' expr   { sym[$1] = $3; }
;

expr:
        INTEGER             { $$ = $1; }
    |   expr '+' expr       { $$ = $1 + $3; elementos = elementos + 1;}
    |   expr '-' expr       { $$ = $1 - $3; }
    |   expr '*' expr       { $$ = $1 * $3; }
    |   expr '/' expr       { $$ = $1 / $3; }
    |   '(' expr ')'        { $$ = $2; }
;

%%

void yyerror(char *s) {
    fprintf(stderr, "%s\n", s);
}


int main(void) {
    yyparse();
    return 0;
}

      

Y.tab.h is generated by bison. When I try to compile main.cpp I get an error:

: gcc main.cpp -o main.exe

result: main.cpp: In function 'int main(int, char**)': main.cpp:8:10: error: 'yyparse' was not declared in this scope main.cpp:9:9: error: 'elementos' was not declared in this scope

How can I fix this?

I am using gcc version 4.7.2, bison 2.4.1 and 2.5.4 on windows 8.1.

Thank!

EDIT

Y.tab.h file:

/* Tokens.  */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
   /* Put the tokens into the symbol table, so that GDB and other debuggers
      know about them.  */
   enum yytokentype {
     INTEGER = 258,
     VARIABLE = 259
   };
#endif
/* Tokens.  */
#define INTEGER 258
#define VARIABLE 259




#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef int YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif

extern YYSTYPE yylval;

      

There is no "elementos" variable, but looking at the generated y.tab.c file I found it was defined!

+3


source to share


1 answer


You have a number of problems:



  • Bison and Flex generate C code, which then needs to be compiled and linked to your program. Your question does not indicate that you did this.

  • If you want to use the elementos variable in your main.cpp file, you need to declare it. It might be defined somewhere else, but the compiler doesn't know what when compiling main.cpp. Add this line inside the outer "C" part:extern int elementos;

  • You have two different main functions.

  • In main.cpp, you #include iostream, but then use printf from stdio.

  • The printf call is incorrect. It needs a format string.

  • Bison shows several warnings that you probably need to read and do if you want your program to work.

+2


source







All Articles