javalexical-analysisjflex

How do I fix this syntax issue with my .flex file?


Is my first time using jflex, I'm following a tutorial i found on the internet in my native language (portuguese), I installed and assembled everything.

But when I try to generate the "Lexer" class, it shows a syntax error in my ".flex" file, I don't know what might be happening because it all seems to be ok.

.flex file

//NOME_VARIAVEL,INT,DEC,COMENTARIO,BRANCO,PALAVRA_CHAVE,ERRO
package Compilador;
import static Compilador.Token.*;
%%
%{
    private void imprimir (String token,String lexema){
            System.out.println(lexema +" ===>> " + token);
    }
%}
%class Lexer
%type Token
nomeVariavel = [_a-zA-Z][_zA-z0-9]*
inteiro = [0-9]+
decimal = [0-9]+["."]+[0-9]+
blocoComentario = "/*" ~"*/"
branco = [\t|\n|\r]+
linhaComentario = [branco]*"//" .*
palavrasChave = "if" | "class" | "int" | "while" | "for" | "do" | "float"
%%

{palavrasChave}     { imprimir("PALAVRA_CHAVE : ", yytext()); return PALAVRA_CHAVE;  } 
{nomeVariavel}      { imprimir("VARIAVEL : ", yytext()); return NOME_VARIAVEL;  }
{inteiro}           { imprimir("NUMERO INTEIRO : ", yytext()); return INT;  }
{decimal}           { imprimir("NUMERO DECIMAL : ", yytext()); return DEC;  }
{blocoComentario}   { imprimir("COMENTARIO : ", yytext()); return COMENTARIO;    }
{linhaComentario}   { imprimir("COMENTARIO : ", yytext()); return COMENTARIO; }
{branco}            ( return BRANCO; } 
.   {imprimir("<<< CARACTER INVALIDO!!! >>>   ",yytext()); return ERROR;}
<<EOF>>     {return null;}

Token.java file

package compilador;
public enum Token{
   NOME_VARIAVEL, INT, DEC, COMENTARIO, BRANCO, PALAVRA_CHAVE, ERROR;

}

generator.flex file

package compilador;

import java.io.*;

public class GeraLexer {
    public static void main(String[] args) throws IOException  {
     String arquivo ="<path redacted for reasons, but it is finding the file>";
     geraLexer(arquivo);
    }

    public static void geraLexer(String arq){
        File file = new File(arq);
        jflex.Main.generate(file);
    }
}

error presented when generating

Reading "<path redacted for reasons, but it is finding the file>"

Error in file "<path redacted for reasons, but it is finding the file>" (line 28): 
Syntax error.
.   {imprimir("<<< CARACTER INVALIDO!!! >>>   ",yytext()); return ERROR;}
 ^
Exception in thread "main" jflex.GeneratorException: Generation aborted
    at jflex.Main.generate(Main.java:139)
    at compilador.GeraLexer.geraLexer(GeraLexer.java:13)
    at compilador.GeraLexer.main(GeraLexer.java:8)
Java Result: 1
CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos)

Appreciate anyone willing to help, yes, I googled first.


Solution

  • In the previous line, you have

    {branco}            ( return BRANCO; } 
    

    The ( should be a {.

    As you will discover soon writing your own parser, it is not always easy to notice an error in the right place. The error is often detected one token later than you might want, and sometimes that token is on the next line.