Categories: java, lexical-analysis, jflex

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

1 answer

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;} file

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

generator.flex file

package compilador;  import*;  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(     at compilador.GeraLexer.geraLexer(     at compilador.GeraLexer.main( Java Result: 1 CONSTRUÍDO COM SUCESSO (tempo total: 0 segundos) 

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

All answers to this question, which has the identifier 61239106

The best answer:

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.

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data