Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lex generated source code in another file

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;
}
like image 355
Ahmed Kotb Avatar asked Nov 04 '11 17:11

Ahmed Kotb


1 Answers

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
>
like image 105
Martin York Avatar answered Sep 25 '22 03:09

Martin York