词法分析器构建选项

参看下面这个例子:

import org.kiot.lexer.Lexer

val data = LexerData.buildSimple {
	"\\d" action 1
	"." action 2
}

/*
    org.kiot.automaton.MarksConflictException:
        FunctionMark(\d) conflicts with FunctionMark(.) under this pattern: [0..9]
*/

这个词法分析器能够将 [0..9] 同时识别为 "digit" 和 "any",所以我们得到了一个错误。在 kiot-lexer 中,我们推荐在你的词法分析器中避免冲突。然而,如果你想要你词法分析器中的规则依定义顺序被匹配,你可以使用:

import org.kiot.lexer.Lexer

val data = LexerData.buildSimple {
	options.strict = false
	"\\d" action 1
	"." action 2
}

class SimpleLexer(chars: CharSequence) : Lexer<Nothing>(data, chars) {
	override fun onAction(action: Int) {
		when (action) {
			1 -> println("a digit")
			2 -> println("a char")
		}
	}
}

SimpleLexer("1").lex()
// 输出: a digit

SimpleLexer("2").lex()
// 输出: a char

除了 strict ,还有其他的构建选项可供操作:

名字

类型

默认值

描述

strict

Boolean

true

一个词法分析器是否是严格的。

minimize

Boolean

false

是否最小化生成的 DFA。

compress

Boolean

true

是否压缩生成的 DFA。

和压缩的 DFA 相比,普通 DFA 需要额外的 O(logC)O(\log|C|) 的时间复杂度来将一个字符定位到其对应的字符类型,然而在压缩的 DFA 中这个过程是 O(1)O(1) 的。

Last updated

Was this helpful?