支持全扫描、基于状态的词法分析和前瞻的词法分析器的python实现
pylexer的Python项目详细描述
Pylexer
一个支持全扫描、基于状态的词法和前瞻性
的词法分析器的Python实现Warning This is not a GENERATOR like classical lexer is. It does not produce any python code. It's a simple plain scanner of the given input string and tokenizer into given set of tokens by matching regular expressions. Thus, at runtime you can change the token definition and use one same code for any token set
安装
使用pip安装到项目中:
pip install pylexer
用法
示例用法:
frompylexerimportPyLexerconfig={'\\s':'','\\d+':'number','\\+':'plus','-':'minus','\\*':'mul','/':'div',}#Static Scan method that returns list of tokenstokens=PyLexer.scan(config,'2 + 3')map(lambdax:x.get_name(),tokens)#PyLexer Config is a dict, so you can also use it likelexer=PyLexer()lexer.set_input('2 + 3')lexer.move_next()whilelexer.get_look_ahead():print(lexer.get_look_ahead().get_name())lexer.move_next()
令牌定义
令牌是用保存令牌名称和正则表达式的TokenDefinition
类定义的。令牌名称可以为空,在这种情况下,lexer将忽略/跳过此类令牌
Lexer配置
lexer配置包含所有令牌定义的列表。使用lexerdictconfig,可以很容易地从一个数组中创建它,其中键是正则表达式,值是令牌的名称
全扫描
pylexer的静态扫描方法可用于扫描给定的输入字符串并返回令牌列表,pylexer还可用于通过具有单个前瞻性的扫描令牌
许可证
麻省理工学院执照。有关详细信息,请参见LICENSE.md
。
贡献者
pylexer的灵感来自于php的lexer(https://github.com/tmilos/lexer),并大量使用了docternapi中的代码,所有这些都归功于milos tomic