简体   繁体   English

如何使用 my.flex 文件解决此语法问题?

[英]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.我是第一次使用 jflex,我正在按照我在互联网上以我的母语(葡萄牙语)找到的教程进行操作,我安装并组装了所有东西。

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.但是当我尝试生成“Lexer”class 时,它在我的“.flex”文件中显示了一个语法错误,我不知道会发生什么,因为一切似乎都很好。

.flex file .flex 文件

//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令牌.java 文件

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

}

generator.flex file生成器.flex 文件

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.感谢任何愿意提供帮助的人,是的,我先用谷歌搜索。

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.该错误通常比您可能需要的一个标记晚检测到,有时该标记位于下一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM