jflex-de / jflex

The fast scanner generator for Java™ with full Unicode support
http://jflex.de
Other
586 stars 114 forks source link

Comma rule cannot be matched #562

Closed arvindell closed 5 years ago

arvindell commented 5 years ago

I'm trying to make "," return an enum element called Coma, below is a fragment of my jflex code.

package lexer;
import static lexer.Tokens.*;

%%
%class Lexer
%type Tokens
%line
%unicode

L=[a-zA-Z]+
D=[0-9]+
espacio=[ ,\t,\r,\n]+

%{
    public int getLine() {
    return yyline;
    }
%}

%{
    public String lexeme;
%}
%%

PROG {lexeme=yytext(); return Prog;}
{espacio} {/*Ignore*/}
"[" {lexeme=yytext(); return AbreCorchete;}
"," {lexeme=yytext(); return Coma;}
":=" {lexeme=yytext(); return DosPuntosIgual;}

{L}({L}|{D})* {lexeme=yytext(); return Identificador;}
{D}+ {lexeme=yytext(); return Enteros;}
({D}+"."{D}+) {lexeme=yytext(); return Reales;}
"."{D}+ {lexeme=yytext(); return Reales;}

However, when I generate the java class, I get the following warning:

Warning in file "src\lexer\Lexer.flex" (line 64): Rule can never be matched: "," {lexeme=yytext(); return Coma;} 167 states before minimization, 130 states in minimized DFA Old file "src\lexer\Lexer.java" saved as "src\lexer\Lexer.java~" Writing code to "src\lexer\Lexer.java"

Is my syntax incorrect? I come here because I haven't found a similar issue, I just need to be pointed in the right direction 😄

lsf37 commented 5 years ago

This message is generated because the rule can indeed not be matched. The rule for {espacio} will always take precedence, because it is defined as [ ,\t,\r,\n]+, which includes ,. The character class syntax is without comma, i.e. you probably wanted [ \t\r\n]+ instead, which will not match comma.

arvindell commented 5 years ago

Didn't think of that, thanks!