This commit is contained in:
Kagura 2024-06-16 10:17:06 +08:00
parent 74999d06b1
commit 978699086c
2 changed files with 104 additions and 99 deletions

View file

@ -1,11 +1,8 @@
import java.util.*
import kotlin.collections.ArrayDeque
class AnalyzeTable {
var V_n = mutableListOf<String>()
var V_t = mutableListOf<String>("#")
public var table = mutableMapOf<String, Int>() // k = E,i; v = 1
var table = mutableMapOf<String, Int>() // k = E,i; v = 1
var items = mutableListOf<String>() // 文法
var first = mutableMapOf<String, MutableList<String>>() // First(E)
@ -15,7 +12,8 @@ class AnalyzeTable {
val NULL = "ε"
}
constructor() : this ("""
constructor() : this(
"""
E -> TE'
E' -> +TE'|ε
T -> FT'
@ -169,6 +167,7 @@ class AnalyzeTable {
}
}
}
else -> {
val upFirst = getFirst(split, l)
upFirst.forEach {
@ -228,27 +227,29 @@ class AnalyzeTable {
if (backup == copy) {
throw RuntimeException("""
throw RuntimeException(
"""
出现了未知符号${copy[0]}
at: $copy
^---------This
""".trimIndent())
""".trimIndent()
)
}
}
return parsedString
}
public fun analyze(input: String,transform: (String) -> String) : List<String> {
fun analyze(input: String, transform: (String) -> String): List<String> {
val analyzeDeque = ArrayDeque<String>()
analyzeDeque.addLast("#")
analyzeDeque.addLast(start)
val inputDeque = ArrayDeque<String>()
inputDeque.addLast("#")
val result = mutableListOf<String>()
splitToVn(transform(input), false).forEach {
inputDeque.addLast(it)
inputDeque.addFirst(it)
}
inputDeque.addFirst("#")
while (true) {
// 在这个时候保存栈
@ -256,20 +257,24 @@ class AnalyzeTable {
"${result.size + 1}: "
.padEnd(4)
+
"${analyzeDeque
"${
analyzeDeque
.toString()
.replace("[", "")
.replace("]", "")
.replace(",", "")
.replace(" ", "")
.padEnd(10)}\t" +
"${inputDeque.reversed()
.padEnd(10)
}\t" +
"${
inputDeque.reversed()
.toString()
.replace("[", "")
.replace("]", "")
.replace(",", "")
.replace(" ", "")
.padStart(6)}\t"
.padStart(6)
}\t"
)
// 栈顶一样删除