1

I started studying jflex. When i try to generate output using jflex for the following code I keep getting an error

Error in file "\abc.flex" (line 29): 
Unexpected character

[ \t\n]+                ;
^
1 error, 0 warnings.

Generation aborted.

Code trying to run

letter   [a-zA-Z]
digit    [0-9]
intlit      [0-9]+
%{
#include <stdio.h>   
# define BASTYPTOK 257   /*following are output from yacc*/
# define IDTOK 258       /*yacc assigns token numbers */
# define LITTOK 259
# define CINTOK 260
# define INSTREAMTOK 261
# define COUTTOK 262
# define OUTSTREAMTOK 263
# define WHILETOK 264
# define IFTOK 265
# define ADDOPTOK 266
# define MULOPTOK 267
# define RELOPTOK 268
# define NOTTOK 269
# define STRLITTOK 270

main()  /*this replaces the main in the lex library*\
{  int  p;
    while (p= yylex())
              printf("%d is \"%s\"\n", p, yytext);
              /*yytext is where lex stores the lexeme*/}
%}

%%
[ \t\n]+                ;
"//".*"\n"              ;
{intlit}                {return(LITTOK);}
cin                     {return(CINTOK);}
"<<"                    {return(INSTREAMTOK);}
\<|"=="                 {return(RELOPTOK);}
\+|\-|"||"              {return(ADDOPTOK);}
"="                     {return(yytext[0]);}
"("                     {return(yytext[0]);}
")"                     {return(yytext[0]);}
.                       {return (yytext[0]); /*default action*/}

%%

Can someone please help me figure out, what is causing the issue. The pattern is also unindented properly. thanks for your help.

Seki
  • 11,135
  • 7
  • 46
  • 70
ashish2k3
  • 211
  • 1
  • 2
  • 5

1 Answers1

1

That's valid flex input but it's not valid jflex. Since the included code is in C rather than Java, it's not clear to me why you would want to use jflex, but if your intent is to port the scanner to Java you might want to read the JFlex manual section on porting.

In particular, the sections in JFlex input are quite different from flex:

        flex                                 JFlex

definitions and declarations            user code
%%                                      %%
rules                                   declarations
%%                                      %%
user code                               definitions and rules

So your definitions and rules are in the correct section for a flex file, but not for a JFlex file. (JFlex just copies the first section to the output, so it doesn't recognize the various syntax errors resulting from putting flex declarations where JFlex expects valid user code.)

Also, JFlex definitions are of the form name = pattern rather than name pattern, so once you get the order of the file sorted out, you'll also need to add the equals signs. And. of course, rewrite the C code in Java.

rici
  • 234,347
  • 28
  • 237
  • 341