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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:41:50+00:00 2026-05-26T17:41:50+00:00

i would like to use the code generated by lex in another code that

  • 0

i would like to use the code generated by lex in another code that i have , but all the examples that i have seen is embedding the main function inside the lex file not the opposite.

is it possible to use(include) the c generated file from lex into other code that to have something like this (not necessarily the same) ?

#include<something>
int main(){
    Lexer l = Lexer("some string or input file");
    while (l.has_next()){
        Token * token = l.get_next_token();
        //somecode
    }
    //where token is just a simple object to hold the token type and lexeme
    return 0;
}
  • 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-26T17:41:51+00:00Added an answer on May 26, 2026 at 5:41 pm

    This is what I would start with:
    Note: this is an example of using a C interface
    To use the C++ interface add %option c++ See below

    Test.lex

    IdentPart1      [A-Za-z_]
    Identifier      {IdentPart1}[A-Za-z_0-9]*
    WHITESPACE      [ \t\r\n]
    
    %option noyywrap
    
    %%
    
    {Identifier}      {return 257;}
    {WHITESPACE}      {/* Ignore */}
    .                 {return 258;}
    
    %%
    
    // This is the bit you want.
    // It is best just to put this at the bottom of the lex file
    // By default functions are extern. So you can create a header file with
    // these as extern then included that header file in your code (See Lexer.h)
    void* setUpBuffer(char const* text)
    {
        YY_BUFFER_STATE buffer  = yy_scan_string(text);
        yy_switch_to_buffer(buffer);
    
        return buffer;
    }
    
    void tearDownBuffer(void* buffer)
    {
        yy_delete_buffer((YY_BUFFER_STATE)buffer);
    }
    

    Lexer.h

    #ifndef LOKI_A_LEXER_H
    #define LOKI_A_LEXER_H
    
    #include <string>
    
    extern int   yylex();
    extern char* yytext;
    extern int   yyleng;
    
    // Here is the interface to the lexer you set up above
    extern void* setUpBuffer(char const* text);
    extern void  tearDownBuffer(void* buffer);
    
    
    class Lexer
    {
        std::string         token;
        std::string         text;
        void*               buffer;
        public:
        Lexer(std::string const& t)
            : text(t)
        {
            // Use the interface to set up the buffer
            buffer  = setUpBuffer(text.c_str());
        }
        ~Lexer()
        {
            // Tear down your interface
            tearDownBuffer(buffer);
        }
        // Don't use RAW pointers
        // This is only a quick and dirty example.
        bool  nextToken()
        {
            int val = yylex();
            if (val != 0)
            {
                token = std::string(yytext, yyleng);
            }
            return val;
        }
        std::string const& theToken() const {return token;}
    };
    
    #endif
    

    main.cpp

    #include "Lexer.h"
    #include <iostream>
    
    int main()
    {
        Lexer l("some string or input file");
    
    
        // Did not like your hasToken() interface.
        // Just call nextToken() until it fails.
        while (l.nextToken())
        {
            std::cout << l.theToken() << "\n";
            delete token;
        }
        //where token is just a simple object to hold the token type and lexeme
        return 0;
    }
    

    Build

    > flext test.lex
    > g++ main.cpp  lex.yy.c
    > ./a.out
    some
    string
    or
    input
    file
    >
    

    Alternatively you can use the C++ interface to flex (its experimental)

    test.lext

    %option c++
    
    
    IdentPart1      [A-Za-z_]
    Identifier      {IdentPart1}[A-Za-z_0-9]*
    WHITESPACE      [ \t\r\n]
    
    %%
    
    {Identifier}      {return 257;}
    {WHITESPACE}      {/* Ignore */}
    .                 {return 258;}
    
    %%
    
    // Note this needs to be here
    // If you define no yywrap() in the options it gets added to the header file
    // which leads to multiple definitions if you are not careful.
    int yyFlexLexer::yywrap()   { return 1;}
    

    main.cpp

    #include "MyLexer.h"
    #include <iostream>
    #include <sstream>
    
    int main()
    {
        std::istringstream  data("some string or input file");
        yyFlexLexer l(&data, &std::cout);
    
    
        while (l.yylex())
        {
            std::cout << std::string(l.YYText(), l.YYLeng()) << "\n";
        }
        //where token is just a simple object to hold the token type and lexeme
        return 0;
    }
    

    build

    > flex --header-file=MyLexer.h test.lex
    > g++ main.cpp lex.yy.cc
    > ./a.out
    some
    string
    or
    input
    file
    >
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few lines of PowerShell code that I would like to use
I would like to use GDB to step though the C++ code that makes
I would like to use a component that exposes the datasource property, but instead
I have my pre generated AES key what i would like to use in
I would like to use CommonLibrary .NET in my project but I have legacy
I would like to use some code written in python (it uses built in
I would like to use a language that I am familiar with - Java,
I have a challenging problem where I would like to use reflection on my
I am writing a numerical code, in which I would like to use a
Can I make my .htaccess be generated with php? I would like to use

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.