如何解决我的 .flex 文件的语法问题?
How do I fix this syntax issue with my .flex file?
我是第一次使用 jflex,我正在按照我在 Internet 上用我的母语(葡萄牙语)找到的教程进行操作,我安装并组装了所有东西。
但是当我尝试生成 "Lexer" class 时,它在我的“.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; }
(
应该是 {
。
正如您很快就会发现编写自己的解析器一样,在正确的位置注意到错误并不总是那么容易。错误通常比您希望的晚一个标记被检测到,有时该标记在下一行。
我是第一次使用 jflex,我正在按照我在 Internet 上用我的母语(葡萄牙语)找到的教程进行操作,我安装并组装了所有东西。
但是当我尝试生成 "Lexer" class 时,它在我的“.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; }
(
应该是 {
。
正如您很快就会发现编写自己的解析器一样,在正确的位置注意到错误并不总是那么容易。错误通常比您希望的晚一个标记被检测到,有时该标记在下一行。