簡體   English   中英

如何使用 my.flex 文件解決此語法問題?

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

我是第一次使用 jflex,我正在按照我在互聯網上以我的母語(葡萄牙語)找到的教程進行操作,我安裝並組裝了所有東西。

但是當我嘗試生成“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;}

令牌.java 文件

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

}

生成器.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; } 

(應該是{

正如您很快就會發現編寫自己的解析器一樣,在正確的位置發現錯誤並不總是那么容易。 該錯誤通常比您可能需要的一個標記晚檢測到,有時該標記位於下一行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM