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