LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-13-2007, 05:41 PM   #1
ascorpio
LQ Newbie
 
Registered: Feb 2007
Posts: 3

Rep: Reputation: 0
Unhappy FLEX++ skipping characters


Hi,

In using flex++ (with c++), my problem is that the lexer skips a character (be it a space or some other character) after every token form the input file. It does not skip and works fine when I do not give a "return" statement in the c++ code segment for tokens. I dont know whats going on, and how can I make the lexer not skip any characters and also use "return" in the code. I am pasting my lex file here:

%{
#include <iostream>
#include <fstream>

using namespace std;

const int OVER = 256;
const int NAME = 257;
const int SYMBOL = 258;
const int ERROR = 259;
const int SPACE = 260;

%}

space [ \t]
alpha [A-Za-z_]
digit [0-9]
alphanum {alpha}|{digit}
nonalphanum [^A-Za-z0-9_]

%%

{space} { cout<<"SPACE"<<endl; return SPACE; }

over/{nonalphanum} { cout<<"OVER: '"<<yytext<<"'"<<endl; return OVER; }

{alpha}{alphanum}* { cout<<"Id: '"<<yytext<<"'"<<endl; return NAME; }

[,;:\.+=\-()] { cout<<"Symbol: '"<<yytext<<"'"<<endl; return SYMBOL; }

. { return ERROR; }

%%

int main(int argc, char** argv)
{
int token = 0;
FlexLexer* lexer = new yyFlexLexer;
ifstream inp;

inp.open(argv[1]);

while(token = lexer->yylex(&inp))
{
cout<<"TOKEN: "<<token<<" ("<<lexer->YYText()<<")"<<endl;
}

inp.close();
}

----------------------------------------------------------------
a segment of input file:

int count;
int y;
;
default cell=cell;

----------------------------------------------------------------------
a segment of the output:
Id: 'int'
TOKEN: 257 (int)
Id: 'count'
TOKEN: 257 (count)

Id: 'int'
TOKEN: 257 (int)
Id: 'y'
TOKEN: 257 (y)

Symbol: ';'
TOKEN: 258 (

Id: 'default'
TOKEN: 257 (default)
Id: 'cell'
TOKEN: 257 (cell)
Id: 'cell'
TOKEN: 257 (cell)
-------------------------------------------------------------
Here you can see that the semicolon ( is missing in the output (so are spaces) from the first line. Consider the last line, the equal to sign is missing (=), so is the semicolon again.
When I remove the return statements from my code (eg. eturn OVER, then it works fine for the character after that particular token, but not when i give the return statement.

can someone please tell me what am I doing wrong here, or what is going on?
 
Old 02-15-2007, 10:06 PM   #2
wee-face
LQ Newbie
 
Registered: Jan 2007
Posts: 17

Rep: Reputation: 0
I've used flex before but with the c programming language, i'm not an expert on the subject but i think you should try that rule

[,;:\.+=\-()]

without the ,

Isn't this used as a seperator in the character class?

Sorry i couldn't be more help, flex is really cool though and
well useful.
 
Old 02-16-2007, 09:33 AM   #3
ascorpio
LQ Newbie
 
Registered: Feb 2007
Posts: 3

Original Poster
Rep: Reputation: 0
Unhappy

hi .. thanks for the reply ..

but the rule mentioned is fine .. comma is not a separator. another thing i discovered is that my code works very well if I just use C and flex (and not C++ and flex++) .. So i have no idea what's wrong with using c++. Is there some issue with it? Or does the code require some modificaction?
 
Old 03-10-2007, 01:50 AM   #4
Baskar
LQ Newbie
 
Registered: Mar 2007
Posts: 1

Rep: Reputation: 0
Question

Hi,
Actually this is not a answer to your question.
now i am doing a projet using flex++. so may i know that what is the commands to compile and run your flex++ program?
 
Old 03-11-2007, 02:56 PM   #5
ascorpio
LQ Newbie
 
Registered: Feb 2007
Posts: 3

Original Poster
Rep: Reputation: 0
hi baskar,

im not sure what you meant, but i guess u want to compile your lexer using "flex++ filename.l" which will generate the c++ code. and then compile this file along with your other file (probably a parser) where you are calling your lexer from using normal "g++" or other c++ compilers. I think you may have to include the flag "-lfl" to have your flex object properly linked.
 
  


Reply



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
How to modify the names of files and replace characters with other characters or symb peter88 Linux - General 2 12-10-2006 03:05 AM
a problem with flex dogbird Slackware 5 03-25-2005 10:24 PM
FC2 test 3 mouse skipping a beat, movie skipping also jang Fedora 1 10-28-2004 07:42 PM
flex in mandrake 10.0 vinnym Mandriva 0 08-14-2004 01:35 AM
anyone knows how to use flex???? tuxfood Programming 1 04-27-2004 06:17 AM

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

All times are GMT -5. The time now is 06:47 AM.

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