gpt4 book ai didi

java - 为什么 Symbol 的字段(左和右)总是返回 0?

转载 作者:行者123 更新时间:2023-12-02 00:49:26 27 4
gpt4 key购买 nike

我正在使用 CUP 和 JFLEX 开发 pascal 编译器。要求之一是从错误中恢复并显示错误所在。

我一直在使用CUP的syntax_errorunrecovered_syntax_error方法

这是我的 parser.cup 中的解析器代码

parser code
{:
public static Nodo padre;
public int cont = 0;

public void syntax_error(Symbol s) {
System.out.println("Error sintáctico. No se esperaba el siguiente token: <" + s.value + ">. Línea: " + (s.left + 1) + ", Columna: " + (s.right + 1));
}

public void unrecovered_syntax_error(Symbol s) throws java.lang.Exception {
System.out.println("Error sintáctico cerca del token: <" + s.value + ">. Línea: " + (s.left + 1) + ", Columna: " + (s.right + 1));
}
:}

这是我的 CFG 的一部分,我只关注我声明错误产生的地方

programa ::=
inicioPrograma:inicProg declaraciones_const:declConst declaraciones_tipo:declTipo declaraciones_var:declVar declaraciones_subprogramas:declSubp proposicion_compuesta:propComp DOT
;

proposicion_compuesta ::=
BEGIN proposiciones_optativas END;

proposiciones_optativas ::=
lista_proposiciones
| /* lambda */;

lista_proposiciones ::=
proposicion SEMICOLON
| proposicion SEMICOLON lista_proposiciones;

proposicion ::=
variable ASSIGNMENT expresion
{:
System.out.println("hola");
:}
| proposicion_procedimiento
| proposicion_compuesta
| IF expresion THEN proposicion
| IF expresion THEN proposicion ELSE proposicion
| WHILE expresion DO proposicion
| FOR ID ASSIGNMENT expresion TO expresion DO proposicion
| FOR ID ASSIGNMENT expresion DOWNTO expresion DO proposicion
| REPEAT proposicion UNTIL expresion
| READ LPAR ID RPAR
| WRITE LPAR CONST_STR RPAR
| WRITE LPAR CONST_STR COMMA ID RPAR
| error;

这是我的 Main.java

import java.io.*;

public class Main {
static public void main(String argv[]) {
/* Start the parser */
try {
parser p = new parser(new LexicalElements(new FileReader(argv[0])));
p.parse();
} catch (Exception e) {
e.printStackTrace();
}

}
}

我相信当出现新错误时 s.right 和 s.left 应该更改其值,但是当我有两个错误时,例如:

program ejemplo;

begin
a 1;
b := 2;
while x+2
a:= 1;
end.

它应该返回

Error sintáctico. No se esperaba el siguiente token: <1>. Línea: [numberX], Columna: [numberY]
Error sintáctico. No se esperaba el siguiente token: <a>. Línea: [numberW], Columna: [numberZ]

其中 numberX 和 numberY 可以彼此相等,numberW 和 numberZ 可以彼此相等,但一对不能等于另一对。

然而,它返回了我

Error sintáctico. No se esperaba el siguiente token: <1>. Línea: 1, Columna: 1
Error sintáctico. No se esperaba el siguiente token: <a>. Línea: 1, Columna: 1

如果有人帮助我理解为什么会发生这种情况和/或如何解决它,我将非常感激。

最佳答案

我无法真正回答您的问题:为什么 Symbol 的字段(左和右)总是返回 0?因为您没有向我们展示您的扫描仪规范代码;符号在扫描仪中初始化。

但是,如果您想要每个符号的左值和右值分别等于其行和列,则必须从扫描仪设置该信息。解析器不会跟踪符号在源代码中的位置。

我假设您正在使用 jflex 作为扫描仪生成器,如果是的话;以下是如何使用 %line%column 选项返回包含行和列的符号的示例:

import java_cup.runtime.*;
%%
%cup

//switches line counting on (the current line is accessed via yyline)
%line
//switches column counting on (the current column is accessed via yycolumn)
%column
IntegerRegex= [0-9]+
%%
<YYINITIAL>
"keyword" {
return new Symbol(sym.KEYWORD, yyline, yycolumn);
}

{IntegerRegex} {
return new Symbol(sym.INTEGER_LITERAL, yyline, yycolumn, Integer.valueOf(yytext()));
}
</YYINITIAL>

关于java - 为什么 Symbol 的字段(左和右)总是返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57872971/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com