Skip to content

Commit c57477d

Browse files
committed
Pretty refactoring
1 parent a53ce2e commit c57477d

File tree

15 files changed

+771
-313
lines changed

15 files changed

+771
-313
lines changed

solver/build.gradle.kts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ repositories {
1111

1212
dependencies {
1313
implementation("org.jetbrains.kotlinx:kotlinx-cli:0.3.5")
14-
implementation("com.fasterxml.jackson.core:jackson-core:2.14.0")
15-
implementation("com.fasterxml.jackson.core:jackson-databind:2.14.0")
1614
implementation ("io.github.oshai:kotlin-logging-jvm:5.1.0")
15+
//needed for .dot parsing
16+
implementation("org.antlr:antlr4:4.13.1")
17+
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
18+
//needed for kotlin-logging
19+
implementation("org.slf4j:slf4j-api:2.0.17")
20+
implementation("ch.qos.logback:logback-classic:1.5.18")
1721

1822
testImplementation(kotlin("test"))
1923
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
20-
implementation("org.antlr:antlr4:4.13.1")
2124
}
2225

2326
kotlin { jvmToolchain(11) }

solver/src/main/kotlin/org/ucfs/sppf/node/RangeSppfNode.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ data class RangeSppfNode<VertexType>(
2424
val children = ArrayList<RangeSppfNode<VertexType>>()
2525
override fun toString(): String {
2626
return when (type) {
27-
is TerminalType<*> -> "Terminal $inputRange ${type.terminal}"
27+
is TerminalType<*> -> "Terminal `${type.terminal}` $inputRange"
2828
is Range -> "Range $inputRange $rsmRange"
2929
is NonterminalType -> "Nonterminal ${type.startState.nonterminal.name} $inputRange $rsmRange"
30-
is IntermediateType<*> -> "Intermediate i:${type.inputPosition} rsm:${type.grammarSlot.id}"
30+
is IntermediateType<*> -> "Intermediate input:${type.inputPosition} rsm:${type.grammarSlot.id}"
3131
is EpsilonNonterminalType -> "Epsilon ${type.startState.id}"
3232
is EmptyType -> "Empty node"
3333
else -> "Unknown sppf node type!"

solver/src/main/kotlin/org/ucfs/sppf/writeSppfToDot.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import org.ucfs.sppf.node.*
44
import java.nio.file.Files
55
import java.nio.file.Path
66

7+
val printWithId = false
78
fun <InputNode> writeSppfToDot(sppfNode: RangeSppfNode<InputNode>, filePath: String, label: String = "") {
89
val genPath = Path.of("gen", "sppf")
910
Files.createDirectories(genPath)
@@ -20,7 +21,7 @@ fun <InputNode> getSppfDot(sppfNodes: Set<RangeSppfNode<InputNode>>, label: Stri
2021
sb.appendLine("labelloc=\"t\"")
2122
sb.appendLine("label=\"$label\"")
2223
var idx = 0
23-
val results = sppfNodes.map { sppf -> getSppfDot(sppf, idx++.toString()) }
24+
val results = sppfNodes.sortedWith(compareBy { it.toString() }).map { sppf -> getSppfDot(sppf.children[0], idx++.toString()) }
2425
for (sppf in results.sorted()) {
2526
sb.appendLine(sppf)
2627
}
@@ -29,7 +30,7 @@ fun <InputNode> getSppfDot(sppfNodes: Set<RangeSppfNode<InputNode>>, label: Stri
2930
}
3031

3132
fun <InputNode> getSppfDot(sppfNode: RangeSppfNode<InputNode>, label: String): String {
32-
val prefix = sppfNode.hashCode().toString()
33+
val prefix = "_${label}_"
3334
val queue: ArrayDeque<RangeSppfNode<InputNode>> = ArrayDeque(listOf(sppfNode))
3435
val visited: HashSet<Int> = HashSet()
3536
var node: RangeSppfNode<InputNode>
@@ -79,9 +80,10 @@ enum class NodeShape(val view: String) {
7980
)
8081
}
8182

82-
fun getNodeView(node: RangeSppfNode<*>, id: String? = null): String {
83+
fun getNodeView(node: RangeSppfNode<*>, id: String): String {
8384
val shape = getNodeShape(node.type)
84-
return "[label = \"${id ?: ""}${shape.name} ${node.type}\", shape = ${shape.view}]"
85+
val idView = if (printWithId) "$id " else ""
86+
return "[label = \"$idView${node}\", shape = ${shape.view}]"
8587
}
8688

8789
fun getNodeShape(rangeType: RangeType): NodeShape {

test-shared/src/test/kotlin/solver/TreeCorrectnessTest.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ class TreeCorrectnessTest : AbstractCorrectnessTest() {
2525
if (expectedResult.isEmpty() || regenerate) {
2626
expectedFile.writeText(actualResult)
2727
} else {
28-
assertEquals(expectedResult, actualResult, "for grammar ${grammar.javaClass.simpleName} at ${testCasesFolder.name}")
28+
assertEquals(
29+
expectedResult,
30+
actualResult,
31+
"for grammar ${grammar.javaClass.simpleName} at ${testCasesFolder.name}"
32+
)
2933
}
3034
}
3135

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
digraph g {
22
labelloc="t"
33
label=""
4-
0 [label = "Nonterminal A, input: [0, 1]", shape = invtrapezium]
5-
1 [label = "Nonterminal B, input: [0, 1]", shape = invtrapezium]
6-
2 [label = "Nonterminal C, input: [0, 1]", shape = invtrapezium]
7-
3 [label = "Nonterminal S, input: [0, 1]", shape = invtrapezium]
8-
4 [label = "Range , input: [0, 1], rsm: [A_0, A_1]", shape = ellipse]
9-
5 [label = "Range , input: [0, 1], rsm: [B_0, B_1]", shape = ellipse]
10-
6 [label = "Range , input: [0, 1], rsm: [C_0, C_1]", shape = ellipse]
11-
7 [label = "Range , input: [0, 1], rsm: [S_0, S_1]", shape = ellipse]
12-
8 [label = "Terminal 'a', input: [0, 1]", shape = rectangle]
13-
0->4
14-
1->5
15-
2->6
16-
3->7
17-
4->8
18-
5->2
19-
6->8
20-
7->1
21-
7->0
4+
subgraph cluster_0{
5+
labelloc="t"
6+
0_0 [label = "26Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
7+
0_1 [label = "27Terminal TerminalType(terminal=a)", shape = rectangle]
8+
0_2 [label = "28Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
9+
0_3 [label = "29Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(C), isStart=true, isFinal=false, numId=0))", shape = invtrapezium]
10+
0_4 [label = "30Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
11+
0_5 [label = "31Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(B), isStart=true, isFinal=false, numId=0))", shape = invtrapezium]
12+
0_6 [label = "32Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
13+
0_7 [label = "33Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=false, numId=0))", shape = invtrapezium]
14+
0_8 [label = "34Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
15+
0_9 [label = "37Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(A), isStart=true, isFinal=false, numId=0))", shape = invtrapezium]
16+
0_0->0_1
17+
0_2->0_3
18+
0_3->0_0
19+
0_4->0_5
20+
0_4->0_9
21+
0_5->0_2
22+
0_6->0_7
23+
0_7->0_4
24+
0_8->0_1
25+
0_9->0_8
26+
}
27+
2228
}
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
digraph g {
22
labelloc="t"
33
label=""
4-
0 [label = "Epsilon RSM: S_0, input: [0, 0]", shape = invhouse]
5-
1 [label = "Nonterminal S, input: [0, 0]", shape = invtrapezium]
6-
2 [label = "Range , input: [0, 0], rsm: [S_0, S_0]", shape = ellipse]
7-
1->2
8-
2->0
4+
subgraph cluster_0{
5+
labelloc="t"
6+
0_0 [label = "63Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
7+
0_1 [label = "64Epsilon EpsilonNonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invhouse]
8+
0_2 [label = "65Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
9+
0_3 [label = "66Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invtrapezium]
10+
0_0->0_1
11+
0_2->0_3
12+
0_3->0_0
13+
}
14+
915
}
Lines changed: 94 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,98 @@
11
digraph g {
22
labelloc="t"
33
label=""
4-
0 [label = "Epsilon RSM: S_0, input: [0, 0]", shape = invhouse]
5-
1 [label = "Intermediate input: 0, rsm: S_1, input: [0, 0]", shape = plain]
6-
2 [label = "Intermediate input: 0, rsm: S_1, input: [0, 1]", shape = plain]
7-
3 [label = "Intermediate input: 0, rsm: S_2, input: [0, 1]", shape = plain]
8-
4 [label = "Intermediate input: 1, rsm: S_2, input: [0, 0]", shape = plain]
9-
5 [label = "Nonterminal S, input: [0, 0]", shape = invtrapezium]
10-
6 [label = "Nonterminal S, input: [0, 1]", shape = invtrapezium]
11-
7 [label = "Range , input: [0, 0], rsm: [S_0, S_0]", shape = ellipse]
12-
8 [label = "Range , input: [0, 0], rsm: [S_0, S_1]", shape = ellipse]
13-
9 [label = "Range , input: [0, 0], rsm: [S_0, S_2]", shape = ellipse]
14-
10 [label = "Range , input: [0, 0], rsm: [S_1, S_2]", shape = ellipse]
15-
11 [label = "Range , input: [0, 1], rsm: [S_0, S_0]", shape = ellipse]
16-
12 [label = "Range , input: [0, 1], rsm: [S_0, S_2]", shape = ellipse]
17-
13 [label = "Range , input: [0, 1], rsm: [S_1, S_2]", shape = ellipse]
18-
14 [label = "Range , input: [0, 1], rsm: [S_2, S_0]", shape = ellipse]
19-
15 [label = "Range , input: [1, 0], rsm: [S_2, S_0]", shape = ellipse]
20-
16 [label = "Terminal '(', input: [0, 0]", shape = rectangle]
21-
17 [label = "Terminal ')', input: [0, 1]", shape = rectangle]
22-
18 [label = "Terminal ')', input: [1, 0]", shape = rectangle]
23-
1->8
24-
1->10
25-
2->8
26-
2->13
27-
3->9
28-
3->14
29-
4->12
30-
4->15
31-
5->7
32-
6->11
33-
7->0
34-
7->4
35-
8->16
36-
9->1
37-
10->5
38-
11->3
39-
12->2
40-
13->6
41-
14->17
42-
15->18
4+
subgraph cluster_0{
5+
labelloc="t"
6+
0_0 [label = "107Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
7+
0_1 [label = "108Epsilon EpsilonNonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invhouse]
8+
0_2 [label = "111Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
9+
0_3 [label = "112Terminal TerminalType(terminal=()", shape = rectangle]
10+
0_4 [label = "114Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
11+
0_5 [label = "115Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invtrapezium]
12+
0_6 [label = "116Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
13+
0_7 [label = "117Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=1), inputPosition=0)", shape = plain]
14+
0_8 [label = "118Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
15+
0_9 [label = "119Terminal TerminalType(terminal=))", shape = rectangle]
16+
0_10 [label = "120Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
17+
0_11 [label = "121Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=2), inputPosition=0)", shape = plain]
18+
0_12 [label = "122Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
19+
0_13 [label = "123Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invtrapezium]
20+
0_14 [label = "124Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
21+
0_15 [label = "125Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invtrapezium]
22+
0_16 [label = "126Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
23+
0_17 [label = "127Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=1), inputPosition=0)", shape = plain]
24+
0_18 [label = "128Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
25+
0_19 [label = "129Terminal TerminalType(terminal=))", shape = rectangle]
26+
0_20 [label = "131Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=2), inputPosition=1)", shape = plain]
27+
0_0->0_1
28+
0_0->0_20
29+
0_2->0_3
30+
0_4->0_5
31+
0_5->0_0
32+
0_6->0_7
33+
0_7->0_2
34+
0_7->0_4
35+
0_8->0_9
36+
0_10->0_11
37+
0_11->0_6
38+
0_11->0_8
39+
0_12->0_13
40+
0_13->0_10
41+
0_14->0_15
42+
0_15->0_10
43+
0_16->0_17
44+
0_17->0_2
45+
0_17->0_14
46+
0_18->0_19
47+
0_20->0_16
48+
0_20->0_18
49+
}
50+
51+
subgraph cluster_1{
52+
labelloc="t"
53+
1_0 [label = "107Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
54+
1_1 [label = "108Epsilon EpsilonNonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invhouse]
55+
1_2 [label = "109Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
56+
1_3 [label = "110Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invtrapezium]
57+
1_4 [label = "111Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
58+
1_5 [label = "112Terminal TerminalType(terminal=()", shape = rectangle]
59+
1_6 [label = "114Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
60+
1_7 [label = "115Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invtrapezium]
61+
1_8 [label = "116Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
62+
1_9 [label = "117Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=1), inputPosition=0)", shape = plain]
63+
1_10 [label = "118Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
64+
1_11 [label = "119Terminal TerminalType(terminal=))", shape = rectangle]
65+
1_12 [label = "120Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
66+
1_13 [label = "121Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=2), inputPosition=0)", shape = plain]
67+
1_14 [label = "124Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
68+
1_15 [label = "125Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invtrapezium]
69+
1_16 [label = "126Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
70+
1_17 [label = "127Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=1), inputPosition=0)", shape = plain]
71+
1_18 [label = "128Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
72+
1_19 [label = "129Terminal TerminalType(terminal=))", shape = rectangle]
73+
1_20 [label = "131Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=2), inputPosition=1)", shape = plain]
74+
1_0->1_1
75+
1_0->1_20
76+
1_2->1_3
77+
1_3->1_0
78+
1_4->1_5
79+
1_6->1_7
80+
1_7->1_0
81+
1_8->1_9
82+
1_9->1_4
83+
1_9->1_6
84+
1_10->1_11
85+
1_12->1_13
86+
1_13->1_8
87+
1_13->1_10
88+
1_14->1_15
89+
1_15->1_12
90+
1_16->1_17
91+
1_17->1_4
92+
1_17->1_14
93+
1_18->1_19
94+
1_20->1_16
95+
1_20->1_18
96+
}
97+
4398
}
Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
digraph g {
22
labelloc="t"
33
label=""
4-
0 [label = "Epsilon RSM: S_0, input: [0, 0]", shape = invhouse]
5-
1 [label = "Intermediate input: 0, rsm: S_1, input: [0, 0]", shape = plain]
6-
2 [label = "Intermediate input: 0, rsm: S_2, input: [0, 0]", shape = plain]
7-
3 [label = "Nonterminal S, input: [0, 0]", shape = invtrapezium]
8-
4 [label = "Nonterminal S, input: [0, 0]", shape = invtrapezium]
9-
5 [label = "Range , input: [0, 0], rsm: [S_0, S_0]", shape = ellipse]
10-
6 [label = "Range , input: [0, 0], rsm: [S_0, S_1]", shape = ellipse]
11-
7 [label = "Range , input: [0, 0], rsm: [S_0, S_2]", shape = ellipse]
12-
8 [label = "Range , input: [0, 0], rsm: [S_1, S_2]", shape = ellipse]
13-
9 [label = "Range , input: [0, 0], rsm: [S_2, S_0]", shape = ellipse]
14-
10 [label = "Terminal '(', input: [0, 0]", shape = rectangle]
15-
11 [label = "Terminal ')', input: [0, 0]", shape = rectangle]
16-
1->6
17-
1->8
18-
2->7
19-
2->9
20-
5->0
21-
5->2
22-
6->10
23-
7->1
24-
8->3
25-
9->11
4+
subgraph cluster_0{
5+
labelloc="t"
6+
0_0 [label = "144Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
7+
0_1 [label = "145Epsilon EpsilonNonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invhouse]
8+
0_2 [label = "146Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
9+
0_3 [label = "147Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invtrapezium]
10+
0_4 [label = "148Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
11+
0_5 [label = "149Terminal TerminalType(terminal=()", shape = rectangle]
12+
0_6 [label = "151Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
13+
0_7 [label = "152Nonterminal NonterminalType(startState=RsmState(nonterminal=Nonterminal(S), isStart=true, isFinal=true, numId=0))", shape = invtrapezium]
14+
0_8 [label = "153Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
15+
0_9 [label = "154Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=1), inputPosition=0)", shape = plain]
16+
0_10 [label = "155Range org.ucfs.sppf.node.Range@6970140a", shape = ellipse]
17+
0_11 [label = "156Terminal TerminalType(terminal=))", shape = rectangle]
18+
0_12 [label = "158Intermediate IntermediateType(grammarSlot=RsmState(nonterminal=Nonterminal(S), isStart=false, isFinal=false, numId=2), inputPosition=0)", shape = plain]
19+
0_0->0_1
20+
0_0->0_12
21+
0_2->0_3
22+
0_3->0_0
23+
0_4->0_5
24+
0_6->0_7
25+
0_7->0_0
26+
0_8->0_9
27+
0_9->0_4
28+
0_9->0_6
29+
0_10->0_11
30+
0_12->0_8
31+
0_12->0_10
32+
}
33+
2634
}

0 commit comments

Comments
 (0)