Skip to main content

一个简单的 BLIF 解析器

项目描述

BLIFPARSER

这是一个简单的 Python 库,用于解析 BLIF(伯克利逻辑交换格式)文件(由 SIS 使用,Sequential Interactive Synthesis)。

并且还评估某些关键字的参数是否具有语法正确的值。

仅解析基本关键字(.model, .inputs, .outputs, .names, 所有 FSM 关键字, .latch, .exdc, .end)。

.clock具有、.gate、.mlatch和delay 约束的更复杂的 BLIF 文件.clock_event仅被部分解析。

这是因为我支持的工作流程不使用这些关键字

目前.model每个文件只支持一个关键字。

您可以将此视为“功能”,因为它强制使用一种良好的做法:使用许多文件,每个组件一个。

您还可以将此库用作基本的 BLIF 验证器。

没有实现复杂的检查,例如交叉文件定义检查和输入输出名称检查,因为这个库的主要目的是解析 BLIF 文件。

一个更完整/更复杂的验证器正在开发中(它将使用这个解析器)。



警告

此解析器不使用语法文件或PEG来解析 blif 文件:

这意味着解析可能“不完美”。

如果有人想贡献和更改,请随时提出拉取请求!

如果这个库启发您从头开始编写更好的解析器,请通过提出 GH 问题告诉我

这意味着该库可以正常工作,因为:

  • BLIF(格式)很简单:大部分时间参数都在关键字的同一行
  • (一些)单元和端到端测试被编写


指数

要求

  • 蟒蛇 3

安装

首先,使用 PIP 安装库:

pip install blifparser

然后:

  • 如果您想将此库用作验证工具,请查看“作为验证器工具”部分

  • 如果你想在你的软件中使用这个库,请检查“作为库”部分

    如果这是您想要使用此软件的唯一方式,您也可以使用 Github 发布页面上的安装程序进行安装

用法

作为验证工具

使用以下命令执行脚本:

blifparser <input_path>

你也可以这样执行:

python -m blifparser <input_path>

替换<input_path>为 BLIF 文件的路径以进行验证

修复错误后,执行脚本,直到修复所有错误。

第一次执行脚本后,并非所有错误都会出现。

作为图书馆

基本/常见用法:

# import the os library: useful to get the absolute path to the input file
import os
# import this library
import blifparser.blifparser as blifparser

# get the file path and pass it to the parser
filepath = os.path.abspath("example.blif")
parser = blifparser.BlifParser(filepath)

# get the object that contains the parsed data
# from the parser
blif = parser.blif

现在你可以:

# get the name of the model
print(blif.model.name)

# get the list of the inputs
print(blif.inputs.inputs)

# get the list of the outputs
print(blif.outputs.outputs)

# get the list of .search keyword
print(blif.imports)

# get the imported file name/path of the first .search keyword
first_import = blif.imports[0]
print(first_import.filepath)

# get the list of subcircuits (.subckt)
print(blif.subcircuits)

# get data from the first subcircuit definition
first_subcircuit = blif.subcircuits[0]
print(first_subcircuit.modelname)  # name of the model
print(first_subcircuit.params)     # subcircuit's parameters

# get the list of boolean functions (.names)
print(blif.booleanfunctions)

# get data from the first boolean function definition
first_boolfunc = blif.booleanfunctions[0]
print(first_boolfunc.inputs)      # list with the names of the inputs
print(first_boolfunc.output)      # string with the name of the output
print(first_boolfunc.truthtable)  # list of lists (each row is a truth table row)

# get the dictionary with the number of occurrencies of each keyword
print(blif.nkeywords)

# get the list of problems/issues
print(blif.problems)

# get the list of the latches
print(blif.latches)

# get the data of the first latch
first_latch = blif.latches[0]
print(first_latch.input)    # name of the input
print(first_latch.output)   # name of the output
print(first_latch.type)     # type of latch (like "re", ...)
print(first_latch.control)  # clock name
print(first_latch.initval)  # initial value

# get the data of the FSM (Finite State Machine)
print(blif.fsm.i.num)       # number of inputs
print(blif.fsm.o.num)       # number of outputs
print(blif.fsm.s.num)       # number of states
print(blif.fsm.p.num)       # number of state transitions
print(blif.fsm.r.name)      # name of the reset state
print(blif.fsm.transtable)  # list of lists (contains the transition table)

描述

这些是使用这个库的第一步:

# import the os library: useful to get the absolute path to the input file
import os
# import this library
import blifparser.blifparser as blifparser

# get the file path and pass it to the parser
filepath = os.path.abspath("example.blif")
parser = blifparser.BlifParser(filepath)

# get the object that contains the parsed data
# from the parser
blif = parser.blif

该blifparser.BlifParser()对象是解析器:它使用该prepare_file()方法准备文件以进行解析,然后创建一个keywords.generic.Blif()对象,该对象将包含从文件中解析的所有信息。

该prepare_file()方法复制文件,然后删除(在副本上):

  • 用反斜杠“ \”组成的换行符
  • 用“ #”发表的评论。

然后读取并解析每一行:

  • 如果该行包含关键字,则创建一个“关键字”对象,然后“链接”到该keywords.generic.Blif()对象(其参数使用关键字进行解析)

    例如:如果.model找到关键字,则在keywords.generic.Model()对象中创建和设置keywords.generic.Blif()对象。( keywords.generic.Blif().model)

  • 如果该行包含文本并且该行位于.names关键字之后,则将其解释为最新布尔函数的真值表(由 定义.names)

    找到下一个关键字时此行为停止

  • 如果该行包含文本并且该行位于.start_kiss关键字之后,则将其解释为有限状态机的转换表。

    找到下一个关键字时此行为停止

  • 如果发现意外的文本/关键字,则会在keywords.generic.Blif().problems列表中收集“问题”或问题。

在解析结束时:

  • 如果找到 FSM,则验证步骤检查它是否在语法上正确
  • 如果找到了一些布尔函数,验证步骤会检查它们在语法上是否正确

其他验证步骤在对象定义期间执行

现在您可以使用该blif对象来获取解析后的数据

查看“Usage > As a library ”部分了解更多详情

变更日志

2021-04-23 1.0.0:

第一次提交

作者

泽纳罗·斯特凡诺 (mario33881)

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

blifparser-1.0.0.tar.gz (15.9 kB 查看哈希)

已上传 source

内置分布

blifparser-1.0.0-py3-none-any.whl (14.9 kB 查看哈希)

已上传 py3