【问题标题】:Bison: Production ignoring required syntaxBison:生产忽略所需的语法
【发布时间】:2021-12-24 02:59:48
【问题描述】:

我的 .y 文件中有以下规则:

statement:
  expression |
  REDUCE operator reductions ENDREDUCE |
  IF expression THEN statement_ ELSE statement_ ENDIF |
  CASE expression IS cases OTHERS ARROW statement_ ENDCASE 
  ;

cases:
  case cases | 
  ;

case:
  WHEN INT_LITERAL ARROW statement_ 
  ;

cases 语句是 case 语句的列表。在 case 之后,OTHERS ARROW statement_ 部分是ENDCASE 标记之前的默认值(如编程中的 switch/case)。但是,在测试时,它并不认为没有它是语法错误:

./compile < tests/syntax5.txt

   1  // Multiple errors
   2  
   3  function main a integer returns real;
syntax error, unexpected INTEGER, expecting ':'

   4      b: integer is * 2;
syntax error, unexpected MULOP

   5      c: real is 6.0;
   6  begin
   7      if a > c then
   8          b + / 4.;
syntax error, unexpected MULOP

   9      else
  10          case b is
  11              when => 2;
syntax error, unexpected ARROW, expecting INT_LITERAL

  12              when 2 => c;
  13          endcase;
  14      endif;
  15  end;
      
Lexical Errors: 0
Syntax Errors: 4
Semantic Errors: 0
Duplicate Identifier Errors: 0
Undeclared Errors: 0
Total Errors: 4

我是不是设置错了?

【问题讨论】:

  • 很难在没有看到您的错误产生的情况下判断(minimal reproducible example 一如既往地是最有用的),但我怀疑这是错误恢复的产物。尝试输入缺少的子句是唯一的错误。
  • 嗨,你是对的;它与错误恢复有关。

标签: grammar bison context-free-grammar


【解决方案1】:

抱歉,这是错误恢复。我在case 规则中添加了error ';'

case:
  WHEN INT_LITERAL ARROW statement_ |
  error ';'
  ;

现在看到错误:

1  // Multiple errors
   2  
   3  function main a integer returns real;
syntax error, unexpected INTEGER, expecting ':'

   4      b: integer is * 2;
syntax error, unexpected MULOP

   5      c: real is 6.0;
   6  begin
   7      if a > c then
   8          b + / 4.;
syntax error, unexpected MULOP

   9      else
  10          case b is
  11              when => 2;
syntax error, unexpected ARROW, expecting INT_LITERAL

  12              when 2 => c;
  13          endcase;
syntax error, unexpected ENDCASE, expecting WHEN or OTHERS

  14      endif;
  15  end;
      
Lexical Errors: 0
Syntax Errors: 5
Semantic Errors: 0
Duplicate Identifier Errors: 0
Undeclared Errors: 0
Total Errors: 5

【讨论】:

    猜你喜欢
    • 2021-07-08
    • 1970-01-01
    • 2017-04-06
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2021-01-01
    • 2017-09-25
    相关资源
    最近更新 更多