【发布时间】: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