我在调试 CUP 语法时遇到了困难。
所以我在 CUP 中有以下语法:
/* Integer operators */
precedence left SUM_OP, SUBS_OP;
precedence left PROD_OP, DIV_OP;
/* Boolean operators */
precedence left EQ_OP, LT_OP, GT_OP, LET_OP, GET_OP;
precedence left OR_OP;
precedence left AND_OP;
start with statements;
statements ::= statement:s
| statement:s SEPARATOR
| SEPARATOR // Empty statement
| statement:s SEPARATOR statements:ss
;
statement ::= IF expression:e SEPARATOR statement:s
| IF expression:e statement:s
| IF expression:e SEPARATOR then_statement:then ELSE SEPARATOR statement:els
| IF expression:e then_statement:then ELSE SEPARATOR statement:els
| IF expression:e SEPARATOR then_statement:then ELSE statement:els
| IF expression:e then_statement:then ELSE statement:els
| WHILE expression:e SEPARATOR statement:s
| WHILE expression:e statement:s
| non_if_statement:s
;
then_statement ::= IF expression:e SEPARATOR then_statement:then ELSE SEPARATOR then_statement:els
| IF expression:e then_statement:then ELSE SEPARATOR then_statement:els
| IF expression:e SEPARATOR then_statement:then ELSE then_statement:els
| IF expression:e then_statement:then ELSE then_statement:els
| WHILE expression:e SEPARATOR then_statement:s
| WHILE expression:e then_statement:s
| non_if_statement:s
;
non_if_statement ::= START_BLOCK statements:s END_BLOCK
| declaration:d
| assignment:a
;
// The statement vs then_statement is for disambiguation purposes
// Solution taken from http://goldparser.org/doc/grammars/example-if-then-else.htm
/* Variable manipulation statements */
declaration ::= type:t IDENTIFIER:id
| type:t IDENTIFIER:id ASSIGN_OP expression:rhs
;
assignment ::= variable:lhs ASSIGN_OP expression:rhs
;
/* Variable manipulation auxiliar sintactic elements */
type ::= T_INT
| T_BOOL
| type:t T_ARRAY
;
variable ::= IDENTIFIER:id
| variable:id LBRACKET expression:idx RBRACKET
;
/* Integer or bool expressions */
expression ::= variable:v
| LPAREN expression:e RPAREN
// Int expressions
| INTEGER_LITERAL:c
| expression:op1 SUM_OP expression:op2
| expression:op1 SUBS_OP expression:op2
| expression:op1 PROD_OP expression:op2
| expression:op1 DIV_OP expression:op2
// Bool expressions
| BOOL_LITERAL:c
| expression:op1 OR_OP expression:op2
| expression:op1 AND_OP expression:op2
| NOT_OP expression:op1
| expression:op1 EQ_OP expression:op2
| expression:op1 LT_OP expression:op2
| expression:op1 GT_OP expression:op2
| expression:op1 LET_OP expression:op2
| expression:op1 GET_OP expression:op2
;
词法分析器将以下标记提供给 CUP 分析器:
int id:i = intLiteral ;
{
if id:i == intLiteral id:i = intLiteral ;
}
while id:i < intLiteral ;
{
id:i = id:i + intLiteral ;
}
if id:i <= intLiteral ;
{
bool id:a ;
bool id:b = boolLiteral ;
}
else ;
{
int id:j = intLiteral ;
}
if id:i >= intLiteral ;
{
id:i = id:i - intLiteral ;
{
id:i = intLiteral + intLiteral ;
}
}
else if id:i > intLiteral id:i = intLiteral ;
else id:i = intLiteral
(在哪里;
并SEPARATOR
划分{ }
块。
当我运行它时,我得到以下输出:
int
id:i
type ::= T_INT
=
intLiteral
;
expression ::= INTEGER_LITERAL
declaration ::= type IDENTIFIER ASSIGN_OP expression
non_if_statement ::= declaration
statement ::= non_if_statement
{
if
id:i
==
variable ::= IDENTIFIER
expression ::= variable
intLiteral
id:i
expression ::= INTEGER_LITERAL
expression ::= expression EQ_OP expression
=
variable ::= IDENTIFIER
intLiteral
;
expression ::= INTEGER_LITERAL
assignment ::= variable ASSIGN_OP expression
non_if_statement ::= assignment
statement ::= non_if_statement
statement ::= IF expression statement
}
statements ::= statement SEPARATOR
while
Error in line 7, column 1 : Syntax error
Error in line 7, column 1 : Couldn't repair and continue parse
(带有单个单词的行表示对导致打印标记的词法分析器的调用。带有 CUP 规则的行表示该规则被匹配。第 7 行是带有 while 语句的行。)
似乎块是失败的原因;当我从提供给语法的内容中删除所有块时,一切都会按我的预期进行解析。
但是,我看不出为什么没有正确解析这些块。
关于可能是什么问题或如何进一步测试的任何想法?
编辑:如果您需要我可能已忽略回答的详细信息,完整代码可在此 repo中找到