0

I'm trying to use JFlex with the following input file:

%class Lexer

%line
%column

%init{
yybegin(YYINITIAL);
%init}

%{
        Copied directly to Java file.
%}

delim       =   \r|\n|\r\n
not_newline =   .
whitespace  =   {delim} | [ \t\n\r]
any     =   {not_newline} | {delim} | {quote}
upp_letter  =   [A-Z]
low_letter  =   [a-z]
digit       =   [0-9]
quote       =   [\”]
backslash   =   [\\]
escape      =   {backslash}{any}
LPAR        =   [(]
RPAR        =   [)]
COMMA       =   [,]
letter      =   {upp_letter} | {low_letter}
ID      =   {letter}({letter}|{digit})*
INT     =   {digit}+
STRING      =   {quote}({letter} | {digit} | {escape})*{quote}


%%

<YYINITIAL> {

    {ID}        { return ID }
    {INT}       { return INT }
    {LPAR}      { return symbol(sym.LPAR); }
    {RPAR}      { return symbol(sym.RPAR); }
    {COMMA}     { return symbol(sum.COMMA); }
    {STRING}    { return STRING }

    {whitespace}    {}

}

[^]         { throw new Error(“Illegal character <“+yytext()+”>”); }

(It's not 100% finished yet, I'm just trying to see if I have any errors)

Anyway, when I try use JFlex it is giving me the following error:

Reading "lexer2.flex"

Error in file "lexer2.flex" (line 35): 
Unexpected character 
<YYINITIAL> {
^
1 error, 0 warnings.

I thought was where the scanner starts and it is always declared by default? Am I missing something?

Thank you for your help.

nbro
  • 15,395
  • 32
  • 113
  • 196
Rachel
  • 1
  • 3

1 Answers1

1

You have your .flex file formatted wrong. As defined by the JFlex manual, you have to organize the file like this:

UserCode 
%% 
Options and declarations 
%% 
Lexical rules

You currently have no UserCode, so you would start off your file with a %%, indicating that the file starts right off the bat with Options and declarations. So the start of the file would look like this:

%%
%class Lexer

%line
%column
kabb
  • 2,474
  • 2
  • 17
  • 24