Skip to content

Commit 114145b

Browse files
committed
remove duplication
1 parent d5d3421 commit 114145b

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

solver/src/main/kotlin/org/ucfs/parser/Gll.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@ class Gll<VertexType, LabelType : ILabel> private constructor(
7272
*/
7373
override fun handleDescriptor(descriptor: Descriptor<VertexType>) {
7474
ctx.descriptors.addToHandled(descriptor)
75-
println("input: ${descriptor.inputPosition}")
76-
println("gss: ${descriptor.gssNode.id}")
77-
println("rsm: ${descriptor.rsmState.id}")
78-
println("sppf: ${descriptor.sppfNode.id}")
7975
if (descriptor.rsmState.isFinal) {
8076
val matchedRange = if (descriptor.sppfNode.type is EmptyType) {
8177
val node = getEpsilonRange(descriptor)

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ open class SppfStorage<InputEdgeType> {
7979
if (!rangeNode.children.contains(valueNode)) {
8080
rangeNode.children.add(valueNode)
8181
}
82-
valueNode.children.addAll(children)
82+
for(child in children){
83+
if (!valueNode.children.contains(child)){
84+
valueNode.children.add(child)
85+
}
86+
}
8387
return rangeNode
8488
}
8589
}

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ fun <InputNode> writeSppfToDot(sppfNode: RangeSppfNode<InputNode>, filePath: Str
1616

1717
fun <InputNode> getSppfDot(sppfNode: RangeSppfNode<InputNode>, label: String = ""): String {
1818
val queue: ArrayDeque<RangeSppfNode<InputNode>> = ArrayDeque(listOf(sppfNode))
19-
val edges: HashMap<RangeSppfNode<InputNode>, HashSet<RangeSppfNode<InputNode>>> = HashMap()
2019
val visited: HashSet<Int> = HashSet()
2120
var node: RangeSppfNode<InputNode>
2221
val sb = StringBuilder()
@@ -32,32 +31,39 @@ fun <InputNode> getSppfDot(sppfNode: RangeSppfNode<InputNode>, label: String = "
3231

3332
node.children.forEach {
3433
queue.addLast(it)
35-
edges.getOrPut(node, { HashSet() }).add(it)
3634
}
3735
}
3836
val sortedViews = nodeViews.values.sorted()
3937
val nodeIds = HashMap<RangeSppfNode<InputNode>, Int>()
38+
val nodeById = HashMap<Int, RangeSppfNode<InputNode>>()
4039
for ((node, view) in nodeViews) {
41-
nodeIds[node] = sortedViews.indexOf(view)
40+
val id = sortedViews.indexOf(view)
41+
nodeIds[node] = id
42+
nodeById[id] = node
4243
}
44+
4345
for (i in sortedViews.indices) {
4446
sb.appendLine("$i ${sortedViews[i]}")
4547
}
4648

47-
val sortedEdges = ArrayList<String>()
48-
for ((head, tails) in edges) {
49-
for (tail in tails) {
50-
sortedEdges.add("${nodeIds[head]}->${nodeIds[tail]}")
49+
for (i in nodeById.keys) {
50+
val node = nodeById[i]
51+
for(child in node!!.children) {
52+
sb.appendLine("${nodeIds[node]}->${nodeIds[child]}")
5153
}
54+
// if(node.children.size < 2){
55+
// continue
56+
// }
57+
// val cs = node.children.map({nodeIds[it]}).joinToString("->")
58+
// sb.appendLine("{ rank = same; $cs [style=invis]}")
5259
}
53-
for (edge in sortedEdges.sorted()) {
54-
sb.appendLine(edge)
55-
}
60+
5661
sb.appendLine("}")
5762
return sb.toString()
5863

5964
}
6065

66+
6167
enum class NodeShape(val view: String) {
6268
Terminal("rectangle"), Nonterminal("invtrapezium"), Intermediate("plain"), Empty("ellipse"), Range("ellipse"), Epsilon(
6369
"invhouse"
@@ -70,7 +76,7 @@ fun fillNodeTemplate(
7076
val inputRangeView = if (inputRange != null) "input: [${inputRange.from}, ${inputRange.to}]" else null
7177
val rsmRangeView = if (rsmRange != null) "rsm: [${rsmRange.from.id}, ${rsmRange.to.id}]" else null
7278
val view = listOfNotNull(nodeInfo, inputRangeView, rsmRangeView).joinToString(", ")
73-
return "[label = \" ${id ?: ""} ${shape.name} $view\", shape = ${shape.view}]"
79+
return "[label = \"${id?: ""}${shape.name} $view\", shape = ${shape.view}]"
7480
}
7581

7682

@@ -91,7 +97,7 @@ fun <InputNode> getNodeView(node: RangeSppfNode<InputNode>, id: String? = null):
9197

9298
is IntermediateType<*> -> {
9399
fillNodeTemplate(
94-
id, "input: ${type.inputPosition}, rsm: ${type.grammarSlot.id}", null, NodeShape.Intermediate
100+
id, "input: ${type.inputPosition}, rsm: ${type.grammarSlot.id}", node.inputRange, NodeShape.Intermediate
95101
)
96102
}
97103

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class AbstractCorrectnessTest {
1414
abstract fun getRootDataFolder(): Path
1515

1616
val grammars = listOf(SimplifiedDyck(), ABGrammar(), SALang(), Epsilon(), LoopDyck())
17-
17+
val regenerate = false
1818
//@TestFactory
1919
//TODO make it abstract by used grammar
2020
@Test

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TreeCorrectnessTest : AbstractCorrectnessTest() {
2222
val input = inputFile.readText()
2323
val expectedResult = expectedFile.readText()
2424
val actualResult = runTest(input, grammar)
25-
if (expectedResult.isEmpty()) {
25+
if (expectedResult.isEmpty() || regenerate) {
2626
expectedFile.writeText(actualResult)
2727
} else {
2828
assertEquals(expectedResult, actualResult)

0 commit comments

Comments
 (0)