【问题标题】:Why do Symbol's fields (left and right) always return 0?为什么 Symbol 的字段(左右)总是返回 0?
【发布时间】:2019-09-10 14:28:00
【问题描述】:

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

我一直在使用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

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

【问题讨论】:

    标签: java jflex cup


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 2021-01-24
      • 2016-09-16
      • 2020-05-25
      • 2013-02-26
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 2013-02-14
      相关资源
      最近更新 更多