Package: blifparser
BLIFPARSERThis is a simple Python library that parses BLIF (Berkeley Logic Interchange Format) files (used by SIS, Sequential Interactive Synthesis).And also evalutes if some keyword's parameters have syntactically correct values.Only the basic keywords are parsed (.model,.inputs,.outputs,.names, all the FSM keywords,.latch,.exdc,.end).More complex BLIF files with.clock,.gate,.mlatch,.clock_eventand delay constraits are only partially parsed.This is because the workflow I am supporting does not use these keywordsCurrently only one.modelkeyword per file is supported.You can see this as a "feature" because it forces the use of a good practise: use many files, one per each component.You can also use this library as a basic BLIF validator.Complex checks such as cross file definition checks and input-output names check are NOT implemented
because the primary intent of this library is to parse BLIF files.WARNINGThis parser DOES NOT use grammar files NORPEGfor parsing blif files:this means that the parsing could be "not perfect".If someone wants to contribute and change this feel free to make pull requests!If this library inspires you to write a better parser from scratch, please let me know by making a GH issueThis means that the library works because:the BLIF (format) is simple: most of the time parameters are on the same line of the keywords(some) unit and end to end tests were writtenIndexRequirementsInstallationUsageDescriptionChangelogAuthorRequirementspython 3 (>= 3.7)networkx library: needed to create the graph (get_graph()method)matplotlib library: needed for networkxInstallationFirst, install the library using PIP:pip install blifparserThen:if you want to use this library as a validation tool, check the "As a validator tool" sectionif you want to use this library inside your software, check the "As a library" sectionIf this is the only way you want to use this software, you can also install it using the installer on the Github Release pageUsageAs a validator toolExecute the script using this command:blifparser <input_path>You can also execute it this way:python -m blifparser <input_path>Replace<input_path>with the path to the BLIF file to validateWhen you have fixed the errors, execute the script until
you have fixed all the errors.Not all errors appear after the first script execution.As a libraryBasic/common usage:# import the os library: useful to get the absolute path to the input fileimportos# import this libraryimportblifparser.blifparserasblifparser# get the file path and pass it to the parserfilepath=os.path.abspath("example.blif")parser=blifparser.BlifParser(filepath)# get the object that contains the parsed data# from the parserblif=parser.blifNow you can:# get the name of the modelprint(blif.model.name)# get the list of the inputsprint(blif.inputs.inputs)# get the list of the outputsprint(blif.outputs.outputs)# get the list of .search keywordprint(blif.imports)# get the imported file name/path of the first .search keywordfirst_import=blif.imports[0]print(first_import.filepath)# get the list of subcircuits (.subckt)print(blif.subcircuits)# get data from the first subcircuit definitionfirst_subcircuit=blif.subcircuits[0]print(first_subcircuit.modelname)# name of the modelprint(first_subcircuit.params)# subcircuit's parameters# get the list of boolean functions (.names)print(blif.booleanfunctions)# get data from the first boolean function definitionfirst_boolfunc=blif.booleanfunctions[0]print(first_boolfunc.inputs)# list with the names of the inputsprint(first_boolfunc.output)# string with the name of the outputprint(first_boolfunc.truthtable)# list of lists (each row is a truth table row)# get the dictionary with the number of occurrencies of each keywordprint(blif.nkeywords)# get the list of problems/issuesprint(blif.problems)# get the list of the latchesprint(blif.latches)# get the data of the first latchfirst_latch=blif.latches[0]print(first_latch.input)# name of the inputprint(first_latch.output)# name of the outputprint(first_latch.type)# type of latch (like "re", ...)print(first_latch.control)# clock nameprint(first_latch.initval)# initial value# get the data of the FSM (Finite State Machine)print(blif.fsm.i.num)# number of inputsprint(blif.fsm.o.num)# number of outputsprint(blif.fsm.s.num)# number of statesprint(blif.fsm.p.num)# number of state transitionsprint(blif.fsm.r.name)# name of the reset stateprint(blif.fsm.transtable)# list of lists (contains the transition table)You can also obtain a networkx graph using theget_graph()method:# import the os library: useful to get the absolute path to the input fileimportos# import this libraryimportblifparser.blifparserasblifparser# get the file path and pass it to the parserfilepath=os.path.abspath("example.blif")parser=blifparser.BlifParser(filepath)# generate the graph and memorize some statisticsgraph_data=parser.get_graph()# retrive networkx graph: can be used to export it as an image or customize itnx_graph=graph_data.nx_graph# extract nodes from the graph: each node has inputs, outputs, a type and the color that can be used to fill the node# > by default .model inputs are red, .model outputs are blue, everything else is blackext_nodes=[nforninnx_graph.nodes]# retrive the length of the longest label: can be used to determine the height of the graph exported onto an imagelongest_label=nx_graph.longest_label# retrive the size of the "item" node that has the most number of inputs: can be used to determine the width of the graph exported onto an image# > where "item" is .inputs/.outputs/.names/.subckt/.latchmax_inputs=nx_graph.max_inputsDescriptionThese are the first steps to use this library:# import the os library: useful to get the absolute path to the input fileimportos# import this libraryimportblifparser.blifparserasblifparser# get the file path and pass it to the parserfilepath=os.path.abspath("example.blif")parser=blifparser.BlifParser(filepath)# get the object that contains the parsed data# from the parserblif=parser.blif# get an object that contains a networkx graph of the blif file and memorize some statisticsgraph_data=parser.get_graph()Theblifparser.BlifParser()object is the parser:
it prepares the file for parsing using theprepare_file()method
and then creates akeywords.generic.Blif()object that will contain
all the information parsed from the file.Theprepare_file()method copies the file and
then removes (on the copy):the newlines made with the backslash "\"the comments made with "#".Then each line is read and parsed:if the line contains a keyword, a "keyword" object is created and then
"linked" to thekeywords.generic.Blif()object (its parameters get parsed with the keyword)For example: if a.modelkeyword is found, akeywords.generic.Model()object
gets created and set in thekeywords.generic.Blif()object. (keywords.generic.Blif().model)if the line contains text and the line comes after the.nameskeyword
it is interpreted as the truth table of the latest boolean function (defined by.names)This behavior stops when the next keyword is foundif the line contains text and the line comes after the.start_kisskeyword
it is interpreted as the transition table of the Finite State Machine.This behavior stops when the next keyword is foundIf an unexpected text/keyword is found a "problem" or issue is collected inside thekeywords.generic.Blif().problemslist.At the end of the parsing:if an FSM was found, a validation step checks if it is syntactically correctif some boolean functions were found, a validation step checks if they are syntactically correctThe other validation steps are executed during object definitionNow you can use theblifobject to get the parsed dataCheck the "Usage >As a library" section for more detailsChangelog2023-03-01 2.0.1:fix dependencies: removed unused dependencies, made requirements.txt equal to setup.cfg dependenciesadded more comments, types and unit testsfix "Replacement index 1 out of range" error caused by missing string format parametercheck before accessing variables that might be None and lists that might be empty2022-12-12 2.0.0:Addedget_graph()method to theBlifParserclass: generates and returns an object containing a networkx graph with inputs, outputs, latches, subckts and boolean functions connected together.This object also contains statistics which can be used when exporting the graph to an image file to roughly determine the dimensions of the image.Dropped python < 3.7 support.2021-04-23 1.0.0:First commitAuthorZenaro Stefano (mario33881)