Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 417453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:34:54+00:00 2026-05-12T18:34:54+00:00

I already looked for my answer but I didn’t get any quick response for

  • 0

I already looked for my answer but I didn’t get any quick response for a simple example.

I want to compile a flex/bison scanner+parser using g++ just because I want to use C++ classes to create AST and similar things.

Searching over internet I’ve found some exploits, all saying that the only needed thing is to declare some function prototypes using extern “C” in lex file.

So my shady.y file is

%{
#include <stdio.h>
#include "opcodes.h"
#include "utils.h"

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

int counter = 0;

extern "C"
{
        int yyparse(void);
        int yylex(void);  
        int yywrap()
        {
                return 1;
        }

}

%}

%token INTEGER FLOAT
%token T_SEMICOL T_COMMA T_LPAR T_RPAR T_GRID T_LSPAR T_RSPAR
%token EOL

%token T_MOV T_NOP


%% 

... GRAMMAR OMITTED ...

%%

main(int argc, char **argv)
{
    yyparse();
}

while shady.l file is

%{
    #include "shady.tab.h"
%}

%%

"MOV"|"mov" { return T_MOV; }
"NOP"|"nop" { return T_NOP; }

";" { return T_SEMICOL; }
"," { return T_COMMA; }
"(" { return T_LPAR; }
")" { return T_RPAR; }
"#" { return T_GRID; }
"[" { return T_LSPAR; }
"]" { return T_RSPAR; }
[1-9][0-9]? { yylval = atoi(yytext); return INTEGER;}
[0-9]+"."[0-9]+ | "."?[0-9]? { yylval.d = atof(yytext); return FLOAT; }
\n { return EOL; }
[ \t] { /* ignore whitespace */ }
. { printf("Mystery character %c\n", *yytext); }

%%

Finally in the makefile I use g++ instead of gcc:

shady: shady.l shady.y
bison -d shady.y -o shady.tab.c
flex shady.l
g++ -o $@ shady.tab.c lex.yy.c -lfl

flex and bison work correctly but upon linking I get the following error:

Undefined symbols:
  "_yylex", referenced from:
  _yyparse in ccwb57x0.o

Of course if I try to change anything about the function in bison file it says that yylex is not declared in the scope of yyparse.

Am I trying to solve simply something that is more complex than it seems? Actually I don’t need a closed structure to have access to parse and lexer in a object oriented manner, I just want to make it work.

I just want to be able to use C++ in bison file (to create AST) and to call yyparse() from C++ objects..

Thanks in advance

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:34:54+00:00Added an answer on May 12, 2026 at 6:34 pm

    You need the extern "C" {} for yylex to be in shady.l:

    %{
        extern "C"
        {
            int yylex(void);
        }
    
        #include "shady.tab.h"
    %}
    
    %%
    
    "MOV"|"mov" { return T_MOV; }
    "NOP"|"nop" { return T_NOP; }
    
    ...etc...
    

    Also, after adding a dummy grammar rule, I was able to build and run this with just:

      559  flex shady.l
      560  bison -d shady.y
      561  g++ shady.tab.c lex.yy.c 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've looked trough the already asked questions but couldn't find an answer. I've some
I already looked at this topic , but I need the answer flipped around.
I've looked at a number of answers already on stackoverflow, but none of the
I've already looked around but couldn't find the exact solution/problem I'm having right now.
Already finished implementing the player. I want to implement the progress bar. But I
I've already looked myself but it seems my Google-fu is not strong today. I'm
I have looked at many posts but unable to find an answer to my
I've looked for this around the internet without getting any good answer so far,
Hi people i have looked around but cant seem to find the answer i'm
This should be a fairly simple question to answer. I looked around and couldn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.