How can I use the flex lexical scanner generator as part of my program?

How can I use the scanner that I wrote using Flex as part of the program I am developing? In particular, in a C ++ class as a class method and from a separate file using just the main method for testing.

I do not want to use the% C ++ parameter, but will compile with g ++.

To answer the question about how to test the scanner from a separate main file, I tried to execute 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 this:

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

I get:

undefined link to yyin

undefined link to yylex

What is the correct way to compile / configure the driver file? Thanks for reading and contributing!

+5
4

:

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";
}

:

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

gcc mylex.c
mylex.c g++, ++ ( C), extern "C" . , mylex.c main.cpp , .

2:

Flex- ++ extern "C" .

main.cpp

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

extern int yylex(void);
extern FILE*   yyin;

int main()
{
    yyin    = fopen("plop", "r");
    std::cout << yylex() << "\n";
}

:

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

, g++ mylex.c( mylex.cpp).

, , :

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

flex , -lfl g++.

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

, flex, g++ - yyin yylex. lex.mylexer.C , , , , , , - , , flex .

0

-2

All Articles