如何修复.flex文件的语法问题



这是我第一次使用jflex,我用我的母语(葡萄牙语(在互联网上找到了一个教程,我安装并组装了所有东西。

但是当我尝试生成"Lexer"类时,它在我的".flex"文件中显示了一个语法错误,我不知道可能发生了什么,因为一切似乎都很好。

.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文件

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

generator.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);
}
}

生成时出现错误

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)

感谢任何愿意帮忙的人,是的,我先在谷歌上搜索了一下。

在前一行中,您有

{branco}            ( return BRANCO; } 

(应该是{

正如您很快就会发现的那样,编写自己的解析器并不总是容易在正确的位置注意到错误。错误通常比您想要的晚一个令牌检测到,有时该令牌在下一行。

相关内容

  • 没有找到相关文章

最新更新