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

  • Home
  • SEARCH
  • 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 4232376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:05:12+00:00 2026-05-21T02:05:12+00:00

How can I use a scanner I’ve written using Flex as part of a

  • 0

How can I use a scanner I’ve written using Flex as part of a program I’m designing? Specifically, within a c++ class as a method of the class, and from a separate file with just a main method to perform testing.

I don’t wish to use the %option c++, but will compile with g++.

To answer the problem of how to test the scanner from a separate file’s main I attempted with the following code:

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

using namespace std;

extern "C" {
    extern int yylex();
}

extern FILE* yyin;

int main(int argc, char *argv[]) {
    if (argc > 1)
        yyin = fopen(argv[1], "r");
    yylex();
    return 0;
}

I compile like so:

flex mylexer.l++
g++ lex.mylexer.C myDriver.C -o myLexer

I get:

undefined reference to yyin

undefined reference to yylex

What is the correct way to compile/setup the driver file?
Thank you for reading and contributing anything!

  • 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-21T02:05:12+00:00Added an answer on May 21, 2026 at 2:05 am

    The simplist example I have is:

    mylex.l

    %option noyywrap
    
    %%
    
    :       return ':';
    
    %%
    

    main.cpp

    #include <stdio.h>
    #include <iostream>
    
    extern "C"
    {
        extern int yylex(void);
        extern FILE*   yyin;
    }
    
    int main()
    {
        yyin    = fopen("plop", "r");
        std::cout << yylex() << "\n";
    }
    

    Then to build:

    > flex -o mylex.c mylex.l
    > gcc -c mylex.c
    > g++ -c main.cpp
    > g++ main.o mylex.o
    

    Notice the gcc to compile the mylex.c
    If you compile mylex.c with g++ it will be compiled as C++ (not C) and your extern “C” declarations in main would be wrong. Thus you need to compile the mylex.c and main.cpp with different compilers then link them together in separate steps.

    Version 2:

    Alternatively you can compile the flex code as C++ and remove the extern “C” from main.

    main.cpp

    #include <stdio.h>
    #include <iostream>
    
    extern int yylex(void);
    extern FILE*   yyin;
    
    int main()
    {
        yyin    = fopen("plop", "r");
        std::cout << yylex() << "\n";
    }
    

    Now Build like this:

    > flex -o mylex.c mylex.l
    > g++ -c mylex.c
    > g++ -c main.cpp
    > g++ main.o mylex.o
    

    Notice this time I used g++ to compile mylex.c (which you could call mylex.cpp now).

    Now that you are using the same compiler it can be a one liner:

    > flex -o mylex.c mylex.l
    > g++ mylex.c main.cpp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You can use more than one css class in an HTML tag in current
You can use a standard dot notation or a method call in Objective-C to
i think i can use Scanner to read a .txt file but how can
I use the Scanner class for reading multiple similar files. I would like to
Anyone that could help me with the java.util.Scanner class? I can't figure out exactly
I Use VSTwain Component To Scan Images With Scanner.How Can I Solve Flowing Error?
please can anyone help me with the port scanner program to scan ports on
I can use SELECT * FROM table WHERE id IN (478,278,190,890,123) to return a
I can use properties of an Excel Worksheet to tell if the worksheet is
You can use ftplib for full FTP support in Python. However the preferred way

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.