Skip to main content
gamedev.net
🔒 Locked

Flex

Started by Greg K 5 replies 750+ views
Original Post
Greg K
Greg K
I am having trouble with my flex scanner. This is for school so if people could just give me hints/advice instead of actual code I would appreciate it. Here is the relavent part of my scan.l file: digit [0-9] integer {digit}+ alpha [a-z]|[A-Z] alphanum {alpha}|{digit}|"_" ident {alpha}|{alpha}{alphanum}+ ... %% ... {integer} {return(INTEGER );} {ident} {return(IDENT );} The problem (I think) is with ident. When I take out that line everything compiles and works fine (except for ident of course). When it is in there I get these errors: flex scan.l "scan.l", line 63: warning, rule cannot be matched "scan.l", line 64: warning, rule cannot be matched "scan.l", line 65: warning, rule cannot be matched "scan.l", line 66: warning, rule cannot be matched "scan.l", line 67: warning, rule cannot be matched "scan.l", line 68: warning, rule cannot be matched "scan.l", line 69: warning, rule cannot be matched "scan.l", line 70: warning, rule cannot be matched "scan.l", line 71: warning, rule cannot be matched "scan.l", line 72: warning, rule cannot be matched "scan.l", line 73: warning, rule cannot be matched "scan.l", line 74: warning, rule cannot be matched gcc -c lex.yy.c Those lines start on the ident line and go on for the rest of the file. I know the other commands work (and the warnings get removed when I take out ident). Does anyone know why I get these warnings? -Greg
Greg K
Greg K
I already tried ident {alpha}{alphanum}* as well. Which should work...
-Greg
SiCrane
SiCrane
Nothing looks wrong with what you have, and when I ran your lexer specification through flex it didn't complain at all. Maybe you can show more of your scan.l file? Also, which version of flex are you using, and what command line parameters are you passing it?
Greg K
Greg K
%{#include "TokenCodes.h"%}delim           [ \t\n]whitespace      {delim}+lparenth        "("rparenth        ")"period          "."comma           ","semicolon       ";"integer         {digit}+digit           [0-9]ident           {alpha}alphanum        {alpha}|{digit}|"_"alpha           [a-z]|[A-Z]kw_begscr       "BEGSCR"kw_contains     "CONTAINS"kw_endscr       "ENDSCR"kw_window       "WINDOW"kw_at           "AT"kw_size         "SIZE"kw_txtbox       "TXTBOX"kw_displays     "DISPLAYS"kw_label        "LABEL"kw_shows        "SHOWS"kw_button       "BUTTON"kw_labelled     "LABELLED"%%{whitespace}    {;}{lparenth}      {return(LPARENTH        );}{rparenth}      {return(RPARENTH        );}{period}        {return(PERIOD          );}{comma}         {return(COMMA           );}{semicolon}     {return(SEMICOLON       );}{integer}       {return(INTEGER         );}{ident}         {return(IDENT           );}{kw_begscr}     {return(KW_BEGSCR       );}{kw_contains}   {return(KW_CONTAINS     );}{kw_endscr}     {return(KW_ENDSCR       );}{kw_window}     {return(KW_WINDOW       );}{kw_at}         {return(KW_AT           );}{kw_size}       {return(KW_SIZE         );}{kw_txtbox}     {return(KW_TXTBOX       );}{kw_displays}   {return(KW_DISPLAYS     );}{kw_label}      {return(KW_LABEL        );}{kw_shows}      {return(KW_SHOWS        );}{kw_button}     {return(KW_BUTTON       );}{kw_labelled}   {return(KW_LABELLED     );}


There is the whole scan.l file with it working. If you change
ident from
ident {alpha}
to
ident {alpha}+
or
ident {alpha}{alphanum}*
(which is what I really want) I get those errors.

Version
flex version 2.5.4
No extra commands
flex scan.l

[edit: added source tags -SiCrane] Well thank you! :) -Greg
Greg K
Greg K
and tokencodes.h just has the #defines for the constants I have used.
SiCrane
SiCrane
The problem here is that when flex sees that multiple rules will match the same input, it takes the one you list first. For example, the input BEGSCR is matched by both the rule for ident and kw_begscr. However, ident comes first in your lexer specification, so it will match BEGSCR against ident. This means kw_begscr will never match against any input.

The easiest solution is to move the ident rule to the bottom of your actions. ex:
/// snip ///{kw_displays}   {return(KW_DISPLAYS     );}{kw_label}      {return(KW_LABEL        );}{kw_shows}      {return(KW_SHOWS        );}{kw_button}     {return(KW_BUTTON       );}{kw_labelled}   {return(KW_LABELLED     );}{ident}         {return(IDENT           );}
Greg K
Greg K
wow, thank you so much. I really appreciate the speedy and helpful reply.
-Greg

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.