Parsing switch statements with Happy
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?
Shouldn't your non-recursive leg of caseStatements include a semi-colon?
i.e.
caseStatements
: caseStatement ";" caseStatements {
($1 : $3)
}
| caseStatement ";" {
[$1]
}
;
精彩评论