LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2004, 03:36 AM   #1
chuanyung
LQ Newbie
 
Registered: Jan 2004
Posts: 25

Rep: Reputation: 15
Question flex c++ code about nest file??


Dear all,

In flex manual, I generate c code to handle "include" command, it's ok.
But if Igenerate c++ class code and try to handle "include" command,
it seems that nothing happens.
Why?

----------------------------------------------------------------------------
----------------------------------
%{
#include <fstream.h>
#include "FlexLexer.h"
#define MAX_INCLUDE_DEPTH 10
YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
int include_stack_ptr=0;
%}

%option c++
%x stINCLUDE

%%

[ \t]+ ;
include {BEGIN stINCLUDE;}
<stINCLUDE>[ \t] ;
<stINCLUDE>[^ \t\n]+ { BEGIN INITIAL;cout << "file name=" << YYText() <<
endl;
if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
cout << "Includes nested too deeply" << endl;
exit(1);
}
include_stack[include_stack_ptr++]=YY_CURRENT_BUFFER;
ifstream myyyin(YYText(),ios::in);
if (myyyin.fail()) {
cout << "fail to open file " << YYText() << endl;
}
else {
cout << "open file: " << YYText() << " successfully" <<
endl;
istream ismyyyin(myyyin.rdbuf());

yy_switch_to_buffer(yy_create_buffer(&ismyyyin,YY_BUF_SIZE));
}
}

<<EOF>> { if (--include_stack_ptr <0 ) {
yyterminate();
}
else {
yy_delete_buffer(YY_CURRENT_BUFFER);
yy_switch_to_buffer(include_stack[include_stack_ptr]);
}
}
[a-zA-Z]+ {cout << YYText() << endl;}
.|\n ;
%%

int main()
{
cout << "start " << endl;
ifstream input_file("test.txt",ios::in);
if (input_file.fail()) {
cout << "fail to open file" << endl;
return -1;
}
istream input_parser(input_file.rdbuf());
yyFlexLexer parser(&input_parser);
parser.yylex();
input_file.close();
cout << "finish "<< endl;
return 0;
}
----------------------------------------------------------------------------
-------------------------------
Thanks for your help.

Regards,
cylin.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"bad interpreter : no such file or directory" when configure "flex" acer_peri Linux - Software 10 11-10-2010 01:19 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
How to get a file from a website (c++ code) ? zvonSully Programming 5 05-10-2005 06:15 AM
I am getting an error trying to nest Dynebolic, And a graphic anomaly McBidgeyFuggalo Linux - Newbie 0 09-03-2004 12:30 AM
[Flex] How can I get a file offset during scanning? chuanyung Programming 2 03-07-2004 07:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration