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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:59:17+00:00 2026-06-09T16:59:17+00:00

I’m trying to write a simple parser in lex and doing it in c++.

  • 0

I’m trying to write a simple parser in lex and doing it in c++. I started off by following the directions here

WIKILINK    \133{2}[^\135]+\135{2}
%option noyywrap
%option c++
%{
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>

#include <FlexLexer.h>

class xxFlexLexer : public yyFlexLexer
{
    public:
    virtual int yylex()
    {
        std::cout << "hello" << std::endl;
    }
private:
            vector<string> listOfLinks;
};

using namespace std;
%}

%%
{WIKILINK}    {
                            int size = YYLeng();
                            string link(YYText());
                            link.erase(YYLeng()-3, 2);
                            link.erase(0, 2);
                            listOfLinks.push_back(link);
                            }
%%

int main()
{
    ifstream in;
    ofstream out;

    in.open("in.txt");
    out.open("out.txt");

    yyFlexLexer lexer(&in, &out);
    lexer.yylex();
}

When i try to compile the program lex.yy.cc, I get the following errors –

In file included from tmp.cpp:12:0:
/usr/include/FlexLexer.h:112:7: error: redefinition of ‘class yyFlexLexer’
/usr/include/FlexLexer.h:112:7: error: previous definition of ‘class yyFlexLexer’
tmp.cpp: In member function ‘virtual int yyFlexLexer::yylex()’:
tmp.cpp:33:29: error: ‘listOfLinks’ was not declared in this scope

I don’t understand this error. What is happening?

  • 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-06-09T16:59:18+00:00Added an answer on June 9, 2026 at 4:59 pm

    Looking on the web I found this FlexLexer.h include file https://www.w3.org/2005/05/22-SPARQL-MySQL/sparql/FlexLexer.h and it appears that the signature for yylex() has an argument FLEXFIX.

    virtual int yylex(FLEXFIX) = 0;
    
    // Call yylex with new input/output sources.
    int yylex(FLEXFIX, istream* new_in, ostream* new_out = 0 )
        {
        switch_streams( new_in, new_out );
        return yylex(FLEXFIX2);
        }
    

    However other versions of the file such as https://www.w3.org/2008/04/SPARQLfed/win/FlexLexer.h do not.

    virtual int yylex() = 0;
    
    // Call yylex with new input/output sources.
    int yylex( FLEX_STD istream* new_in, FLEX_STD ostream* new_out = 0 )
        {
        switch_streams( new_in, new_out );
        return yylex();
        }
    

    By the way your yylex() function is not returning an int value.

    See also @Gir comment with a link to a bug report Bug 67277 – Error compiling libclassparser_la.all_cc.cc: “class yyFlexLexer” redefined.

    The problem is that the FlexLexer.h header has a strange way of
    handling multiple inclusions of itself. tokenizer.l (tokenizer.cc)
    and ClassParser.cc both include this header indirectly, and in
    separate compilation they work fine; but when compiling concatenated
    source, a “#define yyFlexLexer yyFlexLexer” declaration (inserted by
    flex) carries through from tokenizer.cc to ClassParser.cc, and causes
    havoc on the second inclusion. (In the error message quoted above, the
    two FlexLexer.h lines are the second and first inclusions,
    respectively.)

    The/A solution is to “#undef yyFlexLexer” at the end of tokenizer.l,
    and I will attach a patch that does this with an explanation why.

    The comments in the FlexLexer.h include file mention using what is in the bug report for multiple instances and includes. Not sure why it would be needed in something this straightforward and simple though.

    // This file defines FlexLexer, an abstract class which specifies the
    // external interface provided to flex C++ lexer objects, and yyFlexLexer,
    // which defines a particular lexer class.
    //
    // If you want to create multiple lexer classes, you use the -P flag
    // to rename each yyFlexLexer to some other xxFlexLexer.  You then
    // include <FlexLexer.h> in your other sources once per lexer class:
    //
    //  #undef yyFlexLexer
    //  #define yyFlexLexer xxFlexLexer
    //  #include <FlexLexer.h>
    //
    //  #undef yyFlexLexer
    //  #define yyFlexLexer zzFlexLexer
    //  #include <FlexLexer.h>
    //  ...
    

    Looking at the link you provided, I wonder if you need to remove the FlexLexer.h include since you have only the one parser.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.