Skip to content

Commit 82a0260

Browse files
committed
Fixed incomplete validation of production rules
1 parent 554d714 commit 82a0260

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/commonMain/kotlin/eu/yeger/cyk/parser/ProductionRuleParser.kt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ private fun String.splitIntoComponents(): Result<List<String>> {
4949
}
5050

5151
private fun List<String>.asProductionRule(startSymbol: StartSymbol): Result<ProductionRule> {
52-
val inputSymbol = when (val inputString = get(0)) {
53-
startSymbol.symbol -> startSymbol
54-
else -> RegularNonTerminalSymbol(inputString)
55-
}
56-
return when (size) {
57-
3 -> asNonTerminatingProductionRule(inputSymbol)
58-
2 -> asTerminatingProductionRule(inputSymbol)
59-
else -> fail("Invalid amount of symbols on the right side! Must be 1 for terminal or 2 for non terminal production rules. ($lastIndex)")
52+
return when (val inputString = get(0)) {
53+
startSymbol.symbol -> startSymbol.asSuccess()
54+
else -> inputString.parseNonTerminalSymbol()
55+
}.andThen { inputSymbol ->
56+
when (size) {
57+
3 -> asNonTerminatingProductionRule(inputSymbol)
58+
2 -> asTerminatingProductionRule(inputSymbol)
59+
else -> fail("Invalid amount of symbols on the right side! Must be 1 for terminal or 2 for non terminal production rules. ($lastIndex)")
60+
}
6061
}
6162
}
6263

src/commonTest/kotlin/eu/yeger/cyk/parser/ParserTests.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class ParserTests {
5454
"S -> a b c",
5555
"S -> A b",
5656
"S -> a B",
57+
"s -> A B",
5758
).forEach { productionRule ->
5859
assertTrue {
5960
grammar("S") {

0 commit comments

Comments
 (0)