由于Antlr中的隐式定义,我无法解析文件

发布于 2025-02-06 20:05:09 字数 4228 浏览 2 评论 0原文

当我尝试在Antlr4上解析文件时,我有问题。我正在使用Intellij上的Antlr4插件,并且该程序总是给我提供这些警告,这使我无法使用插件解析任何文件或文本。如果使用控制台,我也会遇到相同的错误。这就是我得到的:

warning(125): ParserPF.g4:6:9: implicit definition of token ENTERO in parser
warning(125): ParserPF.g4:6:23: implicit definition of token FLOAT in parser
warning(125): ParserPF.g4:7:16: implicit definition of token ID in parser
warning(125): ParserPF.g4:9:14: implicit definition of token OPERADOR in parser
warning(125): ParserPF.g4:11:27: implicit definition of token IGUAL in parser
warning(125): ParserPF.g4:15:8: implicit definition of token ABREPARENTESIS in parser
warning(125): ParserPF.g4:15:31: implicit definition of token COMA in parser
warning(125): ParserPF.g4:15:45: implicit definition of token CIERRAPARENTESIS in parser
warning(125): ParserPF.g4:17:23: implicit definition of token CIERRASENTENCIA in parser
warning(125): ParserPF.g4:29:20: implicit definition of token FUNC in parser
warning(125): ParserPF.g4:29:105: implicit definition of token ENDFUNC in parser
warning(125): ParserPF.g4:32:38: implicit definition of token LOGICO in parser
warning(125): ParserPF.g4:37:5: implicit definition of token IF in parser
warning(125): ParserPF.g4:37:65: implicit definition of token ELSE in parser
warning(125): ParserPF.g4:37:84: implicit definition of token ENDIF in parser
warning(125): ParserPF.g4:38:7: implicit definition of token BEGIN in parser
warning(125): ParserPF.g4:38:25: implicit definition of token END in parser
warning(125): ParserPF.g4:39:8: implicit definition of token WHILE in parser
warning(125): ParserPF.g4:39:70: implicit definition of token ENDWHILE in parser
warning(125): ParserPF.g4:40:9: implicit definition of token RETURN in parser

我已经尝试了所有事情,但它仍然不起作用,我开始对此失去理智。 ANTLR可能是一个错误吗?我的语法(Lexer and Parser)看起来像:

我的Lexer:

lexer grammar LexerPF;
//Palabras reservadas
BEGIN : 'Begin';
END: 'End';
IF : 'If';
THEN : 'Then';
ELSE : 'Else';
ENDIF : 'EndIf';
WHILE : 'While';
ENDWHILE : 'EndWhile';
FUNC : 'Func';
ENDFUNC : 'EndFunc';
RETURN : 'Return';
PRINT : 'Print';

//Operadores
IGUAL : '=';
OPERADOR : '+' | '-' | '*' | '/' | '%' | '**';
LOGICO : '<' | '>';
SQRT : 'sqrt';
ABREPARENTESIS : '(';
CIERRAPARENTESIS : ')';
COMA : ',';
CIERRASENTENCIA : ';';

//Identificador
ID : [a-zA-Z][a-zA-Z0-9]*;

//Números
ENTERO : [1-9][0-9]* | [0-9];
FLOAT : [1-9][0-9]* '.' [0-9]+;


NEWLINE: ('\r\n'|'\n'|'\r')+ -> skip;
WS: [ \n\t\r]+ -> skip;

我的解析器:

parser grammar ParserPF;

numero : ENTERO #int | FLOAT #float;
identificador : ID;
numID : numero #num | identificador #id;
expr :  numID OPERADOR numID
        | numID (OPERADOR numID)*;
asignacion : identificador IGUAL expr #assignExpr
            | identificador IGUAL numID #assignNumID
            | identificador IGUAL llamadaAfuncion #assignCallFunction;

array : ABREPARENTESIS numID ( COMA numID )* CIERRAPARENTESIS;

sentencia:  asignacion CIERRASENTENCIA  #assignSentence
            | llamadaAfuncion CIERRASENTENCIA  #callFunctionSentence
            | if_ CIERRASENTENCIA  #ifSentence
            | while_ CIERRASENTENCIA  #whileSentence
            | return_ CIERRASENTENCIA  #returnSentence
            | begin CIERRASENTENCIA  #beginSentence
            | declaracionfuncion CIERRASENTENCIA #declareSentence;

programa: sentencia*; //Regla principal


llamadaAfuncion: identificador ABREPARENTESIS numID* ( COMA numID )* CIERRAPARENTESIS;
declaracionfuncion: FUNC identificador ABREPARENTESIS numID* ( COMA numID )* CIERRAPARENTESIS sentencia* ENDFUNC;


condicion   : (ABREPARENTESIS* numero LOGICO numero CIERRAPARENTESIS*) #numAnum
            | (ABREPARENTESIS* identificador LOGICO numero CIERRAPARENTESIS*) #idAnum
            | (ABREPARENTESIS* identificador LOGICO identificador CIERRAPARENTESIS*) #idAid
            | ABREPARENTESIS* numero LOGICO identificador CIERRAPARENTESIS* #numAid
            | llamadaAfuncion #boolean;
if_: IF (ABREPARENTESIS condicion CIERRAPARENTESIS)  sentencia* (ELSE  sentencia*)? ENDIF;
begin: BEGIN  sentencia* END;
while_: WHILE (ABREPARENTESIS condicion CIERRAPARENTESIS)  sentencia* ENDWHILE;
return_: RETURN (expr|identificador|llamadaAfuncion|condicion);

I have a problem when I try to parse a file on ANTLR4. I'm using the ANTLR4 plugin on IntelliJ and the program always throws me these warnings which makes me unable to parse any file or text using the plugin. If I use the console I also get the same errors. This is what I'm getting:

warning(125): ParserPF.g4:6:9: implicit definition of token ENTERO in parser
warning(125): ParserPF.g4:6:23: implicit definition of token FLOAT in parser
warning(125): ParserPF.g4:7:16: implicit definition of token ID in parser
warning(125): ParserPF.g4:9:14: implicit definition of token OPERADOR in parser
warning(125): ParserPF.g4:11:27: implicit definition of token IGUAL in parser
warning(125): ParserPF.g4:15:8: implicit definition of token ABREPARENTESIS in parser
warning(125): ParserPF.g4:15:31: implicit definition of token COMA in parser
warning(125): ParserPF.g4:15:45: implicit definition of token CIERRAPARENTESIS in parser
warning(125): ParserPF.g4:17:23: implicit definition of token CIERRASENTENCIA in parser
warning(125): ParserPF.g4:29:20: implicit definition of token FUNC in parser
warning(125): ParserPF.g4:29:105: implicit definition of token ENDFUNC in parser
warning(125): ParserPF.g4:32:38: implicit definition of token LOGICO in parser
warning(125): ParserPF.g4:37:5: implicit definition of token IF in parser
warning(125): ParserPF.g4:37:65: implicit definition of token ELSE in parser
warning(125): ParserPF.g4:37:84: implicit definition of token ENDIF in parser
warning(125): ParserPF.g4:38:7: implicit definition of token BEGIN in parser
warning(125): ParserPF.g4:38:25: implicit definition of token END in parser
warning(125): ParserPF.g4:39:8: implicit definition of token WHILE in parser
warning(125): ParserPF.g4:39:70: implicit definition of token ENDWHILE in parser
warning(125): ParserPF.g4:40:9: implicit definition of token RETURN in parser

I have tried everything but it still doesn't work and I'm starting to lose my mind with this. Could it be a bug with ANTLR? My grammar (lexer and parser) looks like this:

My lexer:

lexer grammar LexerPF;
//Palabras reservadas
BEGIN : 'Begin';
END: 'End';
IF : 'If';
THEN : 'Then';
ELSE : 'Else';
ENDIF : 'EndIf';
WHILE : 'While';
ENDWHILE : 'EndWhile';
FUNC : 'Func';
ENDFUNC : 'EndFunc';
RETURN : 'Return';
PRINT : 'Print';

//Operadores
IGUAL : '=';
OPERADOR : '+' | '-' | '*' | '/' | '%' | '**';
LOGICO : '<' | '>';
SQRT : 'sqrt';
ABREPARENTESIS : '(';
CIERRAPARENTESIS : ')';
COMA : ',';
CIERRASENTENCIA : ';';

//Identificador
ID : [a-zA-Z][a-zA-Z0-9]*;

//Números
ENTERO : [1-9][0-9]* | [0-9];
FLOAT : [1-9][0-9]* '.' [0-9]+;


NEWLINE: ('\r\n'|'\n'|'\r')+ -> skip;
WS: [ \n\t\r]+ -> skip;

My parser:

parser grammar ParserPF;

numero : ENTERO #int | FLOAT #float;
identificador : ID;
numID : numero #num | identificador #id;
expr :  numID OPERADOR numID
        | numID (OPERADOR numID)*;
asignacion : identificador IGUAL expr #assignExpr
            | identificador IGUAL numID #assignNumID
            | identificador IGUAL llamadaAfuncion #assignCallFunction;

array : ABREPARENTESIS numID ( COMA numID )* CIERRAPARENTESIS;

sentencia:  asignacion CIERRASENTENCIA  #assignSentence
            | llamadaAfuncion CIERRASENTENCIA  #callFunctionSentence
            | if_ CIERRASENTENCIA  #ifSentence
            | while_ CIERRASENTENCIA  #whileSentence
            | return_ CIERRASENTENCIA  #returnSentence
            | begin CIERRASENTENCIA  #beginSentence
            | declaracionfuncion CIERRASENTENCIA #declareSentence;

programa: sentencia*; //Regla principal


llamadaAfuncion: identificador ABREPARENTESIS numID* ( COMA numID )* CIERRAPARENTESIS;
declaracionfuncion: FUNC identificador ABREPARENTESIS numID* ( COMA numID )* CIERRAPARENTESIS sentencia* ENDFUNC;


condicion   : (ABREPARENTESIS* numero LOGICO numero CIERRAPARENTESIS*) #numAnum
            | (ABREPARENTESIS* identificador LOGICO numero CIERRAPARENTESIS*) #idAnum
            | (ABREPARENTESIS* identificador LOGICO identificador CIERRAPARENTESIS*) #idAid
            | ABREPARENTESIS* numero LOGICO identificador CIERRAPARENTESIS* #numAid
            | llamadaAfuncion #boolean;
if_: IF (ABREPARENTESIS condicion CIERRAPARENTESIS)  sentencia* (ELSE  sentencia*)? ENDIF;
begin: BEGIN  sentencia* END;
while_: WHILE (ABREPARENTESIS condicion CIERRAPARENTESIS)  sentencia* ENDWHILE;
return_: RETURN (expr|identificador|llamadaAfuncion|condicion);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文