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 6063069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:07:25+00:00 2026-05-23T09:07:25+00:00

I have a problem while compiling my code ( undefined reference to ‘yylex’ ).

  • 0

I have a problem while compiling my code (undefined reference to 'yylex'). Let’s start off with the snippet and I’ll describe the details below:

Flex:

%{
#include <stdlib.h>
extern int yylex();
%}
%%
"=" {return EQ;}
"!="    {return NE;}
"<" {return LT;}
">" {return GT;}
":="    {return ASSIGN;}
";" {return SEMICOLON;}
"IF"    {return IF;}
"THEN"{return THEN;}

"END" {return END;}
[_a-z]+ {yylval.text = strdup(yytext); return IDENTIFIER;}
[ \t]+
[0-9]+          {
                yylval.var = atoi (yytext);
                return NUMBER;
                }
[-+/^*'%'()]    {
                return *yytext;
                }
\n              return RESULT;
%%

Bison:

%{
  extern "C"
  {
    int yyparse();
    int yylex(void);
    void yyerror(char *s){}
    int yywrap(void){return 1;}
  }

  #include <iostream>
  #include <vector>
  #include <string>
  #include <stdlib.h>
  #include <map>

  using namespace std;

  vector <string> instructions;
  map <> vars;
%}

%union{
  char* text;
  int var;
}


%token EQ
%token NE
%token ASSIGN
%token SEMICOLON
%token IF
%token THEN
%token <text> IDENTIFIER
%token <var> NUMBER
%token <var> RESULT

%left '+' '-'
%left '*' '/' '%'
%right '^'

%%

exp: NUMBER
| IDENTIFIER
| IDENTIFIER "+" IDENTIFIER
| IDENTIFIER "-" IDENTIFIER
;
%%

int main(void){
  yyparse();
} 

And here’s the bash script I use to compile & run the program:

#!/bin/bash
clear
rm launcher lex.yy.cpp *.tab.cpp *.tab.hpp
bison  -d -o list.tab.cpp *.y
flex -o lex.yy.cpp *.l
g++ -o launcher *.cpp -lfl

Just for clear visibility, I’m posting here only the most important part of the code, because the rest is not really necessary here. Anyway, if someone would like to see the whole code, it’s posted here: http://pastebin.com/1rS2FBJj. It’s a bigger chunk of code and takes more place though.

When I try to compile all files to *.c files and then use gcc, it is all fine. But when I attempt to switch the compiler to g++ and therefore the files to cpp ones, I keep getting this error:

list.tab.cpp: In function ‘int yyparse()’:
list.tab.cpp:1397: warning: deprecated conversion from string constant to ‘char*’
list.tab.cpp:1540: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccdqpQVx.o: In function `yyparse':
list.tab.cpp:(.text+0x252): undefined reference to `yylex'
collect2: ld returned 1 exit status

Could anyone give me a hint please, how would be the best to approach it?

  • 1 1 Answer
  • 0 Views
  • 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-23T09:07:26+00:00Added an answer on May 23, 2026 at 9:07 am

    In your Flex file, you declare this:

    extern int yylex();
    

    When compiled as C++, that declares a function with C++ linkage. In your Bison file, you declare this:

    extern "C" {
      int yylex();
    }
    

    That gives it C linkage. They’re two different functions. You define the C++ version (or, rather, Flex defines it for you), but you declare the C version, and the C version is the one the compiler thinks you try to call (in the Bison-generated code). The linker sees that the C version is used, but it cannot find the definition.

    Pick one linkage and use it consistently. (I’d choose C++, since it allows omission of the “extern” stuff from the code entirely.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a problem while writing C++ code that needs to compile
I've been thinking about this problem for a while and have yet to come
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
Environment: VS2005 C++ using STLPort 5.1.4. Compiling the following code snippet: std::string copied =
I have a big problem with this code and I have no idea how
I have this problem while drawing a square in canvas, made out of two
i have a problem when i run a page.jsp : Exception while calling encodeEnd
I'm having a bit of an odd problem while trying to compile some code
I have the following warnings while compiling an openFrameworks 007 project on Visual Studio
Basically my problem stems from a desire to have the textbox portion be white,

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.