使用 Happy 解析 switch 语句
因此,我尝试解析包含这样的 switch 语句的代码
function (a : Boolean) equals (b : Boolean) : Boolean {
switch (a) {
case true:
switch (b) {
case true:
return (true);
case false:
return (false);
}
case false:
switch (b) {
case true:
return (false);
case false:
return (true);
}
}
};
,
switch
: "switch" expression "{" cases "}" {
Switch $2 $4
}
;
cases
: case cases {
($1 : $2)
}
| case {
[$1]
}
;
case
: "case" pattern ":" caseStatements {
Case $2 $4
}
;
caseStatements
: caseStatement ";" caseStatements {
($1 : $3)
}
| caseStatement {
[$1]
}
;
caseStatement
: assignment {
AssignmentCaseStatement $1
}
| return {
ReturnCaseStatement $1
}
| switch {
SwitchCaseStatement $1
}
;
但我不断收到:
certa: user error (../examples/Certa/BooleanLogic.certa:16: Parse error at token 'case')
当我运行生成的解析器时。这里奇怪的是,它在“case”关键字的第二实例上失败,但在第一个实例上失败。到底为什么会这样呢?
So, I'm trying to parse code containg switch statements like this
function (a : Boolean) equals (b : Boolean) : Boolean {
switch (a) {
case true:
switch (b) {
case true:
return (true);
case false:
return (false);
}
case false:
switch (b) {
case true:
return (false);
case false:
return (true);
}
}
};
with
switch
: "switch" expression "{" cases "}" {
Switch $2 $4
}
;
cases
: case cases {
($1 : $2)
}
| case {
[$1]
}
;
case
: "case" pattern ":" caseStatements {
Case $2 $4
}
;
caseStatements
: caseStatement ";" caseStatements {
($1 : $3)
}
| caseStatement {
[$1]
}
;
caseStatement
: assignment {
AssignmentCaseStatement $1
}
| return {
ReturnCaseStatement $1
}
| switch {
SwitchCaseStatement $1
}
;
but I keep getting:
certa: user error (../examples/Certa/BooleanLogic.certa:16: Parse error at token 'case')
when I run the generated parser. The strange thing here is that it fails on the second instance of the "case" keyword, but not the first. Why in the world would that be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
caseStatements 的非递归部分不应该包含分号吗?
IE
Shouldn't your non-recursive leg of caseStatements include a semi-colon?
i.e.