Remove Unused Variables

This commit is contained in:
Kagura 2024-06-17 13:06:46 +08:00
parent f0be209464
commit ab2f19e027

View file

@ -5,7 +5,6 @@ class AnalyzeTable {
var table = mutableMapOf<String, Int>() // k = E,i; v = 1
var items = mutableListOf<String>() // 文法
var first = mutableMapOf<String, MutableList<String>>() // First(E)
var start = "" // 开始符号
companion object {
@ -19,14 +18,15 @@ class AnalyzeTable {
T -> FT'
T' ::= *FT'|ε
F -> (E)|i
""".trimIndent())
""".trimIndent()
)
constructor(lang: String, _s: String? = null) {
start = _s?:lang.trim()[0].toString()
start = _s ?: lang.trim()[0].toString()
val lang = if ("::=" in lang){ // 换成一个符号方便
lang.replace("::=","->")
}else{
val lang = if ("::=" in lang) { // 换成一个符号方便
lang.replace("::=", "->")
} else {
lang
}
@ -70,7 +70,7 @@ class AnalyzeTable {
val vn = item.split("->")[0]
val right = item.split("->")[1]
val firsts = getFirst(right, vn)
val firsts = getFirst(right)
firsts.forEach {
if (it != NULL) {
table["$vn,$it"] = items.indexOf(item)
@ -90,7 +90,7 @@ class AnalyzeTable {
}
}
private fun getFirst(string: String, vn: String): MutableList<String> {
private fun getFirst(string: String): MutableList<String> {
val firsts = mutableListOf<String>()
// 以NULL开头
if (string == NULL) {
@ -101,11 +101,6 @@ class AnalyzeTable {
// 以终结符开头
V_t.forEach {
if (string.startsWith(it)) {
if (first[vn] != null) {
first[vn]!!.add(it)
} else {
first[vn] = mutableListOf(it)
}
if (!firsts.contains(it)) {
firsts.add(it)
}
@ -125,7 +120,7 @@ class AnalyzeTable {
for (line in items) {
if (line.split("->")[0] == item) {
val newLine = line.split("->")[1]
val nextFirst = getFirst(newLine, item) // 求 item的follow
val nextFirst = getFirst(newLine) // 求 item的follow
nextFirst.forEach {
if (it !in newFirst) {
newFirst.add(it)
@ -173,7 +168,7 @@ class AnalyzeTable {
}
else -> {
val upFirst = getFirst(split, l)
val upFirst = getFirst(split)
upFirst.forEach {
if (it != NULL) {
follows.add(it)
@ -312,5 +307,5 @@ class AnalyzeTable {
}
}
class UnknownSymbolException(msg: String) : Exception("\n"+msg)
class UnknownSymbolException(msg: String) : Exception("\n" + msg)
}