diff --git a/LL1/.idea/vcs.xml b/LL1/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/LL1/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LL1/src/analyzeTable.kt b/LL1/src/AnalyzeTable.kt similarity index 73% rename from LL1/src/analyzeTable.kt rename to LL1/src/AnalyzeTable.kt index 7979970..42e0bd9 100644 --- a/LL1/src/analyzeTable.kt +++ b/LL1/src/AnalyzeTable.kt @@ -1,4 +1,7 @@ -class analyzeTable { +import java.util.* +import kotlin.collections.ArrayDeque + +class AnalyzeTable { var V_n = mutableListOf() var V_t = mutableListOf("#") @@ -197,6 +200,8 @@ class analyzeTable { var copy = string var no_vt_now = true while (copy.isNotEmpty() && no_vt_now) { + val backup = copy + V_t.forEach { if (copy.startsWith(it)){ parsedString.add(it) @@ -220,7 +225,76 @@ class analyzeTable { } } copy = copy.substring(startVn.length) + + + if (backup == copy){ + throw RuntimeException(""" + 出现了未知符号${copy[0]} + at: $copy + ^---------This + """.trimIndent()) + } } return parsedString } + + public fun analyze(input: String,transform: (String) -> String) : List { + val analyzeDeque = ArrayDeque() + analyzeDeque.addLast("#") + analyzeDeque.addLast(start) + val inputDeque = ArrayDeque() + inputDeque.addLast("#") + + val result = mutableListOf() + splitToVn(transform(input),false).forEach { + inputDeque.addLast(it) + } + + while (true) { + // 在这个时候保存栈 + result.add( + "${result.size+1}: " + .padEnd(4) + + + "${analyzeDeque + .toString() + .replace("[","") + .replace("]","") + .replace(",","") + .replace(" ","") + .padEnd(10)}\t" + + "${inputDeque.reversed() + .toString() + .replace("[","") + .replace("]","") + .replace(",","") + .replace(" ","") + .padStart(6)}\t" + ) + + // 栈顶一样删除 + if (analyzeDeque.last() == inputDeque.last()) { + analyzeDeque.removeLast() + inputDeque.removeLast() + + if (analyzeDeque.isEmpty() && inputDeque.isEmpty()){ + result[result.size - 1] = "${result[result.size - 1]}成功" + return result + } + continue + } + + // 动作 + val action = table["${analyzeDeque.removeLast()},${inputDeque.last()}"] + if (action == null) { + result[result.size - 1] = "${result[result.size - 1]}错误" + return result + } else { + splitToVn(items[action].split("->")[1], false).reversed().forEach { + analyzeDeque.addLast(it) + } + result[result.size - 1] = "${result[result.size - 1]}${items[action]}" + } + } + } } \ No newline at end of file diff --git a/LL1/src/Main.kt b/LL1/src/Main.kt index ce3ceed..c86a00e 100644 --- a/LL1/src/Main.kt +++ b/LL1/src/Main.kt @@ -1,5 +1,5 @@ fun main() { - val analyzeTable = analyzeTable() + val analyzeTable = AnalyzeTable() println("输入的文法:") for (i in analyzeTable.items.indices) { println("$i: ${analyzeTable.items[i]}") @@ -8,4 +8,18 @@ fun main() { analyzeTable.table.forEach { println("${it.key}: ${analyzeTable.items[it.value]}") } + + println("分析过程:") + analyzeTable.analyze("98+99+80"){ + it.replace("\\d+".toRegex(),"i") + }.forEach { + println(it) + } + + analyzeTable.analyze("(106-80(*95)"){ + it.replace("\\d+".toRegex(),"i") + }.forEach { + println(it) + } + } \ No newline at end of file