View Single Post
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Lexer- Tokenizer problem

 
0
  #1
Aug 28th, 2007
hi, i use the flex tool {http://www.gnu.org/software/flex/manual/} to generate a tokenizer ,but i have the following problem {it has to do with the way that flex tokenizes the input::

FILE : flex.l

  1. %{
  2. #define WEB 0
  3. #define SPACE 1
  4. #define STRING 2
  5. %}
  6.  
  7. string_component [0-9a-zA-Z \t\.!#$%^&()*@_]
  8.  
  9. %%
  10.  
  11. "daniweb" {return WEB;}
  12. [ \t\n] {return SPACE;}
  13. {string_component}+ {return STRING;}
  14.  
  15. %%
  16.  
  17. #include <iostream>
  18.  
  19. using namespace std;
  20.  
  21. int main()
  22. {
  23. cout<<yylex()<<endl;
  24. cout<<yylex()<<endl;
  25.  
  26. return 0;
  27. }
  28.  
  29. int yywrap(void){return 1;}

Example file:
  1. test_string daniweb

What i want is to have the above string tokenized as
STRING SPACE WEB
instead flex recognizes it as STRING, because it tries to match the longest input....

How can i fix this problem?
all ideas are welcomed....

PS:: to compile:
  1. flex flex.l
  2. g++ lex.yy.c
  3. ./a.out <example
Reply With Quote